Last active
August 31, 2018 21:14
-
-
Save kingsleyh/985691f9949254345f015c4c93e47bb0 to your computer and use it in GitHub Desktop.
Array.Extra
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Array.Extra { | |
/* | |
Map over a nested array and then flatten: | |
[[1,2],[1,5]] | |
Array.Extra.flatMap((a : Array(Number) : Array(Number) => { [Array.max(n)] }) == [2,5] | |
*/ | |
fun flatMap (func : Function(a, Array(b)), array : Array(a)) : Array(b) { | |
Array.map(func, array) | |
|> Array.Extra.concat() | |
} | |
/* | |
Put two lists together: | |
Array.Extra.append([1,1,2] [3,5,8]) == [1,1,2,3,5,8] | |
*/ | |
fun append (array1 : Array(a), array2 : Array(a)) : Array(a) { | |
`concat(array1, array2)` | |
} | |
/* | |
Concatenate a bunch of arrays into a single array: | |
Array.Extra.concat([[1,2],[3],[4,5]]) == [1,2,3,4,5] | |
*/ | |
fun concat (arrays : Array(Array(a))) : Array(a) { | |
Array.Extra.foldr(Array.Extra.append, [], arrays) | |
} | |
/* | |
Reduce a list from the left. | |
Array.Extra.foldl((acc : Number, n : Number) : Number => { acc + n}, 0, [1,2,3,4,5]) == 15 | |
*/ | |
fun foldl (func : Function(a, b, b), initial : b, array : Array(a)) : b { | |
`array.reduce(func, initial)` | |
} | |
/* | |
Reduce a list from the right. | |
Array.Extra.foldr((acc : Number, n : Number) : Number => { acc + n}, 0, [1,2,3,4,5]) == 15 | |
*/ | |
fun foldr (func : Function(a, b, b), initial : b, array : Array(a)) : b { | |
`array.reduceRight(func, initial)` | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
suite "Array foldl" { | |
test "folds left" { | |
Array.Extra.foldl( | |
(acc : Number, n : Number) : Number => { acc + n }, | |
0, | |
[ | |
1, | |
2, | |
3, | |
4, | |
5 | |
]) == 15 | |
} | |
} | |
suite "Array foldr" { | |
test "folds right" { | |
Array.Extra.foldr( | |
(acc : Number, n : Number) : Number => { acc + n }, | |
0, | |
[ | |
1, | |
2, | |
3, | |
4, | |
5 | |
]) == 15 | |
} | |
} | |
suite "Array append" { | |
test "appends array b to array a" { | |
Array.Extra.append([ | |
1, | |
2, | |
3 | |
], [ | |
4, | |
5, | |
6 | |
]) == [ | |
1, | |
2, | |
3, | |
4, | |
5, | |
6 | |
] | |
} | |
} | |
suite "Array concat" { | |
test "concats an array of arrays into a single array " { | |
Array.Extra.concat( | |
[ | |
[ | |
1, | |
2 | |
], | |
[ | |
3, | |
4 | |
], | |
[ | |
5, | |
6 | |
] | |
]) == [ | |
1, | |
2, | |
3, | |
4, | |
5, | |
6 | |
] | |
} | |
} | |
suite "Array flatMap" { | |
test "maps over a nested array and flattens" { | |
try { | |
result = | |
[ | |
[ | |
3, | |
1 | |
], | |
[ | |
2, | |
0 | |
], | |
[5] | |
] | |
|> Array.Extra.flatMap( | |
(n : Array(Number)) : Array(Number) => { [Array.max(n)] }) | |
(result == [ | |
3, | |
2, | |
5 | |
]) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function concat(a, b) { | |
return a.reduceRight(function (coll, item) { | |
coll.unshift(item); | |
return coll; | |
}, b); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment