1 /*
2 Copyright (c) 2011-2021 Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license (the "Software") to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 /**
30  * Functions that return other functions
31  *
32  * Copyright: Timur Gafarov 2011-2021.
33  * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0).
34  * Authors: Timur Gafarov
35  */
36 module dlib.math.hof;
37 
38 /**
39  * Functional composition. 
40  * Description:
41  * Returns a function that applies function f to the return value of function g
42  */
43 T delegate(S) compose(T, U, S)(T function(U) f, U function(S) g)
44 {
45     return (S s) => f(g(s));
46 }
47 
48 /**
49  Y combinator
50  Description:
51  We're all familiar with the idea of a function as something that takes some
52  input value and returns some output value. Say, the function for squaring numbers:
53  ---
54    f(x) = x*x;
55  ---
56  The fixed points of a function are any input values for which f(x) is equal to x.
57  So, the fixed points of f(x) = x*x are 0 and 1.
58 
59  Now, we have things called higher-order functions. These are functions that take another
60  function as input, or return a function as output, or both.
61 
62  The fixed point of a higher-order function f is another function p such that f(p) = p.
63  It may be more helpful to think in terms of functions actually being executed.
64  The previous statement is equivalent to the statement that f(p)(x) = p(x) for all values of x.
65 
66  Y (the Y combinator) is a special function that returns the fixed points of higher-order
67  functions, that is to say:
68  ---
69    f(Y(f)) = Y(f)
70  ---
71  Y combinator is commonly use to allow anonymous recursion without assuming your host
72  language supports it.
73 */
74 auto Y(R, P...) (R delegate(P) delegate(R delegate(P)) lambda)
75 {
76     struct RFunc {R delegate(P) delegate(RFunc) f;}
77     auto r = RFunc( (RFunc w) => lambda((P x) => w.f(w)(x)) );
78     return r.f(r);
79 }
80 
81 ///
82 unittest
83 {
84     import std.algorithm;
85     import std.range;
86 
87     auto factorial = Y((int delegate(int) self) =>
88         (int n) => 0 == n ? 1 : n * self(n - 1));
89 
90     auto ackermann = Y((ulong delegate(ulong, ulong) self) =>
91         (ulong m, ulong n) => (!m && n)?
92             (n+1) : (m && !n)? self(m-1, 1) : self(m-1, self(m, n-1)));
93 
94     auto qsort = Y((int[] delegate(int[]) self) =>
95         (int[] arr) => arr.length?
96             self(arr.filter!(a => a < arr[0]).array)
97           ~ arr[0]
98           ~ self(arr.filter!(a => a > arr[0]).array) : []);
99 
100     assert(factorial(6) == 720);
101     assert(ackermann(3, 5) == 253);
102     assert(qsort([8, 5, 10, 2, 16, 9, 1, 100, 3])
103               == [1, 2, 3, 5, 8, 9, 10, 16, 100]);
104 }