if typeof! xs is \Object for , x of xs then f x else for x in xs then f x xs export map = (f, xs) --> f = objToFunc f if typeof! f isnt \Function type = typeof! xs if type is \Object {[key, f x] for key, x of xs} else result = [f x for x in
xs] if type is \String then result * '' else result export filter = (f, xs) --> f = objToFunc f if typeof! f isnt \Function type = typeof! xs if type is \Object {[key, x] for key, x of xs when f x} else result = [x for x in xs when f x] if
type is \String then result * '' else result export reject = (f, xs) --> f = objToFunc f if typeof! f isnt \Function type = typeof! xs if type is \Object {[key, x] for key, x of xs when not f x} else result = [x for x in xs when not f x] if
type is \String then result * '' else result export partition = (f, xs) --> f = objToFunc f if typeof! f isnt \Function type = typeof! xs if type is \Object passed = {} failed = {} for key, x of xs (if f x then passed else failed)[key] = x
else passed = [] failed = [] for x in xs (if f x then passed else failed)push x if type is \String passed *= '' failed *= '' [passed, failed] export find = (f, xs) --> f = objToFunc f if typeof! f isnt \Function if typeof! xs is \Object for
, x of xs when f x then return x else for x in xs when f x then return x void export head = export first = (xs) -> return void if not xs.length xs.0 export tail = (xs) -> return void if not xs.length xs.slice 1 export last = (xs) -> return
void if not xs.length xs[*-1] export initial = (xs) -> return void if not xs.length xs.slice 0 xs.length - 1 export empty = (xs) -> if typeof! xs is \Object for x of xs then return false return yes not xs.length export values = (obj) -> [x
for , x of obj] export keys = (obj) -> [x for x of obj] export len = (xs) -> xs = values xs if typeof! xs is \Object xs.length export cons = (x, xs) --> if typeof! xs is \String then x + xs else [x] ++ xs export append = (xs, ys) --> if typeof!
ys is \String then xs + ys else xs ++ ys export join = (sep, xs) --> xs = values xs if typeof! xs is \Object xs.join sep export reverse = (xs) -> if typeof! xs is \String then (xs / '')reverse! * '' else xs.slice!reverse! export fold = export
foldl = (f, memo, xs) --> if typeof! xs is \Object for , x of xs then memo = f memo, x else for x in xs then memo = f memo, x memo export fold1 = export foldl1 = (f, xs) --> fold f, xs.0, xs.slice 1 export foldr = (f, memo, xs) --> fold f,
memo, xs.slice!reverse! export foldr1 = (f, xs) --> xs.=slice!reverse! fold f, xs.0, xs.slice 1 export unfoldr = export unfold = (f, b) --> if (f b)? [that.0] ++ unfoldr f, that.1 else [] export andList = (xs) -> for x in xs when not x return
false true export orList = (xs) -> for x in xs when x return true false export any = (f, xs) --> f = objToFunc f if typeof! f isnt \Function for x in xs when f x return yes no export all = (f, xs) --> f = objToFunc f if typeof! f isnt \Function
for x in xs when not f x return no yes export unique = (xs) -> result = [] if typeof! xs is \Object for , x of xs when x not in result then result.push x else for x in xs when x not in result then result.push x if typeof! xs is \String then
result * '' else result export sort = (xs) -> xs.concat!sort (x, y) -> | x > y => 1 | x
< y=> -1 | _ => 0 export sortBy = (f, xs) --> return [] unless xs.length xs.concat!sort f export compare = (f, x, y) --> | (f x) > (f y) => 1 | (f x)
< (f y)=> -1 | otherwise => 0 export sum = (xs) -> result = 0 if typeof! xs is \Object for , x of xs then result += x else for x in xs then result += x result export product = (xs) -> result = 1 if typeof! xs is \Object for , x of xs then result
*= x else for x in xs then result *= x result export mean = export average = (xs) -> (sum xs) / len xs export concat = (xss) -> fold append, [], xss export concatMap = (f, xs) --> fold ((memo, x) -> append memo, f x), [], xs export
listToObj = (xs) -> {[x.0, x.1] for x in xs} export maximum = (xs) -> fold1 (>?), xs export minimum = (xs) -> fold1 (
export scan = export scanl = (f, memo, xs) -->
then [memo] ++ [last = f last, x for , x of xs]
else [memo] ++ [last = f last, x for x in xs]
export scan1 = export scanl1 = (f, xs) --> scan f, xs.0, xs.slice 1
export scanr = (f, memo, xs) -->
scan f, memo, xs .reverse!
export scanr1 = (f, xs) -->
scan f, xs.0, xs.slice 1 .reverse!
export replicate = (n, x) -->
while i < n, ++i then result.push x
export take = (n, xs) -->
if typeof! xs is \String then '' else []
| otherwise => xs.slice 0, n
export drop = (n, xs) -->
| otherwise => xs.slice n
export splitAt = (n, xs) --> [(take n, xs), (drop n, xs)]
export takeWhile = (p, xs) -->
return xs if not xs.length
p = objToFunc p if typeof! p isnt \Function
if typeof! xs is \String then result * '' else result
export dropWhile = (p, xs) -->
return xs if not xs.length
p = objToFunc p if typeof! p isnt \Function
export span = (p, xs) --> [(takeWhile p, xs), (dropWhile p, xs)]
export breakIt = (p, xs) --> span (not) << p, xs
export zip = (xs, ys) -->
export zipWith = (f,xs, ys) -->
f = objToFunc f if typeof! f isnt \Function
if not xs.length or not ys.length
[f.apply this, zs for zs in zip.call this, xs, ys]
export zipAll = (...xss) ->
export zipAllWith = (f, ...xss) ->
f = objToFunc f if typeof! f isnt \Function
if not xss.0.length or not xss.1.length
[f.apply this, xs for xs in zipAll.apply this, xss]
export compose = (...funcs) ->
args = [f.apply this, args]
export flip = (f, x, y) --> f y, x
( (g, x) -> -> f(g g) ...arguments ) do
(g, x) -> -> f(g g) ...arguments
return [] if not str.length
export unlines = (strs) -> strs * \\n
return [] if not str.length
export unwords = (strs) -> strs * ' '
export negate = (x) -> -x
export quot = (x, y) --> ~~(x / y)
export div = (x, y) --> Math.floor x / y
export atan2 = (x, y) --> Math.atan2 x, y
export truncate = (x) -> ~~x
export round = Math.round
export ceiling = Math.ceil
export floor = Math.floor
export isItNaN = (x) -> x isnt x
export even = (x) -> x % 2 == 0
export odd = (x) -> x % 2 != 0
Math.abs Math.floor (x / (gcd x, y) * y)
export installPrelude = !(target) ->
unless target.prelude?isInstalled
target <<< target.prelude.isInstalled = true