Skip to content

Commit

Permalink
Array#count, initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ConorOBrien-Foxx committed Oct 29, 2016
1 parent cf7f1b5 commit 0440b9b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/stdlib/primitive/Array/lib.es6
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ export default new Map([
require('./lib/push')(api),
require('./lib/rect')(api),
require('./lib/moore')(api),
require('./lib/flat')(api)
require('./lib/flat')(api),
require('./lib/count')(api)
]);
22 changes: 22 additions & 0 deletions src/stdlib/primitive/Array/lib/count.es6
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export default (api) => ["count", api.var(new api.func(
[
["arg", {}]
],
function(scope, input){
let arg = input("arg");
let self = input("self").value;
if(arg.constructor.Name === "Function"){
let amount = 0;
for(let i = 0; i < self.length; i++){
// todo: use cheddar definition of truth
if(arg.exec([self[i], i, self], null).value){
amount++;
}
}
return api.init(api.number, 10, 0, amount);
} else {
// todo: use cheddar's ==
return "uhhh I cant do that, sorry.";
}
}
))];
14 changes: 11 additions & 3 deletions src/stdlib/primitive/Array/lib/flat.es6
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ export default (api) => ["flat", api.prop(new api.func(
[],
function(scope, input) {
// equiv. .reduce((+)).map(flat) or something
// todo: I need to know how to check which class
// let flat = (chedArr) =>
let flat = (arr) =>
api.init(api.array, ...arr.value.reduce(
(flt, toflt) => (
flt.concat(toflt.constructor.Name === "Array" ?
flat(toflt).value : toflt
)
),
[]
));
return flat(input("self"));
}
))]
))];
5 changes: 0 additions & 5 deletions src/stdlib/primitive/Array/lib/moore.es6
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@ export default (api) => ["moore", api.var(new api.func(

let grid = [];
for(let i = y - n; i <= y + n; i++){
<<<<<<< HEAD
if(typeof self[i] === "undefined") continue;
let row = [];
=======
let row = [];
if(typeof self[i] === "undefined") continue;
>>>>>>> bded84ad2b4a18951d60331637de762449e7d037
for(let j = x - m; j <= x + m; j++){
if(typeof self[i][j] === "undefined") continue;
row.push(self[i][j]);
Expand Down

0 comments on commit 0440b9b

Please sign in to comment.