src/FunctionalProgramming.jl

Tue, 07 Dec 2021 11:41:07 +0200

author
Tuomo Valkonen <tuomov@iki.fi>
date
Tue, 07 Dec 2021 11:41:07 +0200
changeset 34
22a64e826ee7
parent 33
a60d2f12ef93
child 37
f8be66557e0f
permissions
-rw-r--r--

New logger and iteration interface

33
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1 ###################################
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
2 # Tools for functional programming
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
3 ###################################
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
4
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
5 """
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
6
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
7 `module AlgTools.FunctionalProgramming`
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
8
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
9 This module implements:
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
10 - `curry`
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11 - `curryflip`
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
12 """
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
13 module FunctionalProgramming
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15 ##############
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
16 # Our exports
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
17 ##############
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
19 export curry,
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20 curryflip
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22 ###########
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23 # Currying
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24 ###########
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26 """
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27 `curry(f)` and `curry(f, x)`
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29 From a function `f` of parameters `(x, y...; kwargs...)` construct a function `g`
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
30 of parameter `x`, returning a function of parameter `(y...; kwargs...)`.
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
31
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
32 The version with parameter `x` already applies the first argument, returning a function
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33 of `(y...; kwargs...)`.
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
34 """
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35 function curry(f::Function)
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36 return x -> (y...; kwargs...)-> f(x, y...; kwargs...)
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37 end
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
38
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
39 function curry(f::Function, x)
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
40 return (y...; kwargs...)-> f(x, y...; kwargs...)
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
41 end
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43 """
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44 `curryflip(f)` and `curryflip(f, y...; kwargs...)
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 From a function `f` of parameters `(x, y...; kwargs...)` construct a function `g`
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47 of parameter `(y...; kargs...)`, returning a function of parameter `x`
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
48
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
49 The version with parametesr `(y...; kwargs...)` already applies these parameters,
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
50 returning a function of `x`.
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
51 """
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
52 function curryflip(f::Function)
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
53 return (y...; kwargs...) -> x -> f(x, y...; kwargs...)
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
54 end
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
55
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
56 function curryflip(f::Function, y...; kwargs...)
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
57 return x ->f(x, y...; kwargs...)
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
58 end
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
59
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
60 end # module

mercurial