src/FunctionalProgramming.jl

Wed, 22 Dec 2021 11:13:38 +0200

author
Tuomo Valkonen <tuomov@iki.fi>
date
Wed, 22 Dec 2021 11:13:38 +0200
changeset 37
f8be66557e0f
parent 33
a60d2f12ef93
permissions
-rw-r--r--

Planar finite elements, simple linear solvers for fixed dimensions

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,
37
f8be66557e0f Planar finite elements, simple linear solvers for fixed dimensions
Tuomo Valkonen <tuomov@iki.fi>
parents: 33
diff changeset
20 curryflip,
f8be66557e0f Planar finite elements, simple linear solvers for fixed dimensions
Tuomo Valkonen <tuomov@iki.fi>
parents: 33
diff changeset
21 maybe
33
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 ###########
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24 # Currying
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 """
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28 `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
29
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
30 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
31 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
32
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33 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
34 of `(y...; kwargs...)`.
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35 """
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36 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
37 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
38 end
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
39
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
40 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
41 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
42 end
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 """
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45 `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
46
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47 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
48 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
49
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
50 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
51 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
52 """
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
53 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
54 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
55 end
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
56
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
57 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
58 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
59 end
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
60
37
f8be66557e0f Planar finite elements, simple linear solvers for fixed dimensions
Tuomo Valkonen <tuomov@iki.fi>
parents: 33
diff changeset
61 """
f8be66557e0f Planar finite elements, simple linear solvers for fixed dimensions
Tuomo Valkonen <tuomov@iki.fi>
parents: 33
diff changeset
62 `maybe(f, x)`
f8be66557e0f Planar finite elements, simple linear solvers for fixed dimensions
Tuomo Valkonen <tuomov@iki.fi>
parents: 33
diff changeset
63
f8be66557e0f Planar finite elements, simple linear solvers for fixed dimensions
Tuomo Valkonen <tuomov@iki.fi>
parents: 33
diff changeset
64 Returns `nothing` if `x` is `nothing, otherwise `f(x)`.
f8be66557e0f Planar finite elements, simple linear solvers for fixed dimensions
Tuomo Valkonen <tuomov@iki.fi>
parents: 33
diff changeset
65 """
f8be66557e0f Planar finite elements, simple linear solvers for fixed dimensions
Tuomo Valkonen <tuomov@iki.fi>
parents: 33
diff changeset
66 maybe( :: Function, :: Nothing) = nothing
f8be66557e0f Planar finite elements, simple linear solvers for fixed dimensions
Tuomo Valkonen <tuomov@iki.fi>
parents: 33
diff changeset
67 maybe(f :: Function, x :: T) where T = f(x)
f8be66557e0f Planar finite elements, simple linear solvers for fixed dimensions
Tuomo Valkonen <tuomov@iki.fi>
parents: 33
diff changeset
68
33
a60d2f12ef93 Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
69 end # module

mercurial