-
Notifications
You must be signed in to change notification settings - Fork 0
/
exyz.ex
54 lines (49 loc) · 1.13 KB
/
exyz.ex
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
51
52
53
54
defmodule Exyz do
@moddoc """
Exyz provides a z-combinator (but no y-combinator) macro and function so that
you can use recursion in anonymous functions with ease!
"""
defmacro __using__(_) do
quote do
import Exyz, only: :macros
require Exyz
end
end
@doc ~s"""
Z combinator macro, reference the function with `this`
## Examples
iex(1)> factorial = Exyz.z fn
...(1)> (1) -> 1
...(1)> (n) -> n * this.(n - 1)
...(1)> end
iex(2)> factorial.(5) == 120
true
"""
defmacro z g do
quote do
Exyz.z_combinator fn var!(this) ->
unquote(g)
end
end
end
@doc ~s"""
Z combinator, support recursion inside anonymous functions!
defined as:
λf. (λx. f (λy. x x y)) (λx. f (λy. x x y))
## Examples
iex(1)> factorial = Exyz.z_combinator fn(f) ->
...(1)> fn
...(1)> (1) -> 1
...(1)> (n) -> n * f.(n - 1)
...(1)> end
...(1)> end
iex(2)> factorial.(5) == 120
true
"""
def z_combinator f do
combinator = fn(x) ->
f.(fn(y) -> x.(x).(y) end)
end
combinator.(combinator)
end
end