forked from adambom/dictionary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_.jl
executable file
·50 lines (40 loc) · 998 Bytes
/
_.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module _
import Base.map
import Base.reduce
# Collection functions
function each(a::Array, iterator::Function)
for item in a
iterator(item)
end
end
# Array functions
function difference(a::Array, rest...)
rest = vcat(rest...)
filter((value) -> !in(value, rest), a)
end
without(a::Array, values...) = difference(a, values...)
# Function functions
partial(func::Function, args...) = (bargs...) -> func(args..., bargs...)
function memoize(func::Function, hasher)
memo = Dict()
function(args...)
key = hasher(args...)
has(memo, key) ? memo[key] : (memo[key] = func(args...))
end
end
function memoize(func::Function)
memo = Dict()
function(args...)
key = identity(args...)
has(memo, key) ? memo[key] : (memo[key] = func(args...))
end
end
function compose(funcs::Function...)
return function(args...)
for func in reverse(funcs)
args = func(args...)
end
args
end
end
end