birds/
lib.rs

1/// B combinator: Bxyz = x(yz)
2pub fn bluebird<A, B, C, X, Y>(x: X, y: Y, z: C) -> A
3where
4    X: Fn(B) -> A,
5    Y: Fn(C) -> B,
6{
7    x(y(z))
8}
9
10/// C combinator: Cxyz = xzy
11pub fn cardinal<A, X, Y, Z>(x: X, y: Y, z: Z) -> A
12where
13    X: Fn(Z, Y) -> A,
14{
15    x(z, y)
16}
17
18/// D combinator: Dxyzw = xy(zw)
19pub fn dove<A, B, X, Y, Z, W>(x: X, y: Y, z: Z, w: W) -> B
20where
21    X: Fn(Y, A) -> B,
22    Z: Fn(W) -> A,
23{
24    x(y, z(w))
25}
26
27/// E combinator: Exyzwv = xy(zwv)
28pub fn eagle<A, B, X, Y, Z, W, V>(x: X, y: Y, z: Z, w: W, v: V) -> A
29where
30    X: Fn(Y, B) -> A,
31    Z: Fn(W, V) -> B,
32{
33    x(y, z(w, v))
34}
35
36/// F combinator: Fxyz = zyx
37pub fn finch<A, X, Y, Z>(x: X, y: Y, z: Z) -> A
38where
39    Z: Fn(Y, X) -> A,
40{
41    z(y, x)
42}
43
44/// G combinator: Gxyzw = xw(yz)
45pub fn goldfinch<A, B, X, Y, Z, W>(x: X, y: Y, z: Z, w: W) -> A
46where
47    X: Fn(W, B) -> A,
48    Y: Fn(Z) -> B,
49{
50    x(w, y(z))
51}
52
53/// H combinator: Hxyz = xyzy
54pub fn hummingbird<A, X, Y, Z>(x: X, y: Y, z: Z) -> A
55where
56    X: Fn(Y, Z, Y) -> A,
57    Y: Clone, // TODO taking references at parameters seems better
58{
59    x(y.clone(), z, y)
60}
61
62/// I combinator: Ix = x
63pub fn identity_bird<X>(x: X) -> X {
64    x
65}
66
67/// J combinator: Jxyzw = xy(xwz)
68pub fn jay<X, Y, Z>(x: X, y: Y, z: Z, w: Y) -> Z
69where
70    X: Fn(Y, Z) -> Z,
71{
72    x(y, x(w, z))
73}
74
75/// K combinator: Kxy = x
76pub fn kestrel<X, Y>(x: X, _y: Y) -> X {
77    x
78}
79
80/// L combinator: Lxy = Lx(yy)
81pub fn lark<A, B, X, Y>(x: X, y: Y) -> A
82where
83    X: Fn(B) -> A,
84    Y: Fn(Y) -> B + Clone,
85{
86    x(y(y.clone()))
87}
88
89/// M combinator: Mx = xx
90pub fn mockingbird<A, X>(x: X) -> A
91where
92    X: Fn(X) -> A + Clone,
93{
94    x(x.clone())
95}
96
97/// O combinator: Oxy = y(xy)
98pub fn owl<A, B, X, Y>(x: X, y: Y) -> A
99where
100    X: Fn(Y) -> B,
101    Y: Fn(B) -> A + Clone,
102{
103    y(x(y.clone()))
104}
105
106/// Q combinator: Qxyz = y(xz)
107pub fn queer<A, B, X, Y, Z>(x: X, y: Y, z: Z) -> A
108where
109    X: Fn(Z) -> B,
110    Y: Fn(B) -> A,
111{
112    y(x(z))
113}
114
115/// Q1 combinator: Q1xyz = x(zy)
116pub fn quixotic<A, B, X, Y, Z>(x: X, y: Y, z: Z) -> A
117where
118    X: Fn(B) -> A,
119    Z: Fn(Y) -> B,
120{
121    x(z(y))
122}
123
124/// Q3 combinator: Q3xyz = z(xy)
125pub fn quirky<A, B, X, Y, Z>(x: X, y: Y, z: Z) -> A
126where
127    X: Fn(Y) -> B,
128    Z: Fn(B) -> A,
129{
130    z(x(y))
131}
132
133/// R combinator: Rxyz = yzx
134pub fn robin<A, X, Y, Z>(x: X, y: Y, z: Z) -> A
135where
136    Y: Fn(Z, X) -> A,
137{
138    y(z, x)
139}
140
141/// ϴ combinator: ϴx = x(ϴx)
142pub fn sage<A, X>(x: X) -> A
143where
144    X: Fn(A) -> A + Clone,
145{
146    x(sage(x.clone()))
147}
148
149/// S combinator: Sxyz = xz(yz)
150pub fn starling<A, B, X, Y, Z>(x: X, y: Y, z: Z) -> A
151where
152    X: Fn(Z, B) -> A,
153    Y: Fn(Z) -> B,
154    Z: Clone,
155{
156    x(z.clone(), y(z))
157}
158
159/// T combinator: Txy = yx
160pub fn thrush<A, X, Y>(x: X, y: Y) -> A
161where
162    Y: Fn(X) -> A,
163{
164    y(x)
165}
166
167/// U combinator: Uxy = y(xxy)
168pub fn turing<A, B, X, Y>(x: X, y: Y) -> A
169where
170    X: Fn(X, Y) -> B + Clone,
171    Y: Fn(B) -> A + Clone,
172{
173    y(x(x.clone(), y.clone()))
174}