UNPKG

1.38 kBtext/coffeescriptView Raw
1@any = (list, fn) ->
2 for e in list
3 return yes if fn e
4 no
5
6@all = (list, fn) ->
7 for e in list
8 return no unless fn e
9 yes
10
11@foldl = foldl = (memo, list, fn) ->
12 for i in list
13 memo = fn memo, i
14 memo
15
16@foldl1 = (list, fn) -> foldl list[0], list[1..], fn
17
18@map = map = (list, fn) -> fn e for e in list
19
20@concat = concat = (list) -> [].concat list...
21
22@concatMap = (list, fn) -> concat map list, fn
23
24@intersect = (listA, listB) -> a for a in listA when a in listB
25
26@difference = (listA, listB) -> a for a in listA when a not in listB
27
28@nub = nub = (list) ->
29 result = []
30 result.push i for i in list when i not in result
31 result
32
33@union = (listA, listB) ->
34 listA.concat (b for b in (nub listB) when b not in listA)
35
36@flip = (fn) -> (b, a) -> fn.call this, a, b
37
38@owns = do (hop = {}.hasOwnProperty) -> (a, b) -> hop.call a, b
39
40@span = span = (list, f) ->
41 if list.length is 0 then [[], []]
42 else if f list[0]
43 [ys, zs] = span list[1..], f
44 [[list[0], ys...], zs]
45 else [[], list]
46
47@divMod = (a, b) ->
48 c = a % b
49 mod = if c < 0 then c + b else c
50 div = Math.floor a / b
51 [div, mod]
52
53# The partition function takes a list and predicate fn and returns the pair of lists
54# of elements which do and do not satisfy the predicate, respectively.
55@partition = (list, fn) ->
56 result = [[], []]
57 result[+!fn item].push item for item in list
58 result