odds/fix.rs
1
2/// Fixpoint combinator for rust closures, generalized over the return type.
3///
4/// **Note:** Use this best through the [`fix`](fn.fix.html) function.
5///
6/// In **Fix\<T, R\>**, **T** is the argument type, and **R** is the return type,
7/// **R** defaults to **T**.
8///
9/// Calling the `Fix` value only supports function call notation with the nightly
10/// channel and the crate feature ‘unstable’ enabled; use the .call() method otherwise.
11///
12/// ```
13/// use odds::Fix;
14/// use odds::fix;
15///
16/// let c = |f: Fix<i32>, x| if x == 0 { 1 } else { x * f.call(x - 1) };
17/// let fact = Fix(&c);
18/// assert_eq!(fact.call(5), 120);
19///
20/// let data = [true, false];
21/// assert!(!fix(&data[..], |f, x| {
22/// x.len() == 0 || x[0] && f.call(&x[1..])
23/// }));
24///
25/// ```
26#[cfg_attr(feature="unstable", doc="
27```
28// using feature `unstable`
29use odds::Fix;
30
31let c = |f: Fix<i32>, x| if x == 0 { 1 } else { x * f(x - 1) };
32let fact = Fix(&c);
33assert_eq!(fact(5), 120);
34```
35"
36)]
37pub struct Fix<'a, T: 'a, R: 'a = T>(pub &'a dyn Fn(Fix<T, R>, T) -> R);
38
39/// Fixpoint combinator for rust closures, generalized over the return type.
40///
41/// This is a wrapper function that uses the `Fix` type. The recursive closure
42/// has two arguments, `Fix` and the argument type `T`.
43///
44/// In **Fix\<T, R\>**, **T** is the argument type, and **R** is the return type,
45/// **R** defaults to **T**.
46///
47/// Calling the `Fix` value only supports function call notation with the nightly
48/// channel and the crate feature ‘unstable’ enabled; use the .call() method otherwise.
49///
50/// This helper function makes the type inference work out well.
51///
52/// ```
53/// use odds::fix;
54///
55/// assert_eq!(120, fix(5, |f, x| if x == 0 { 1 } else { x * f.call(x - 1) }));
56///
57/// let data = [true, false];
58/// assert!(!fix(&data[..], |f, x| {
59/// x.len() == 0 || x[0] && f.call(&x[1..])
60/// }));
61///
62/// ```
63#[cfg_attr(feature="unstable", doc="
64```
65// using feature `unstable`
66use odds::fix;
67
68assert_eq!(120, fix(5, |f, x| if x == 0 { 1 } else { x * f(x - 1) }));
69```
70"
71)]
72pub fn fix<T, R, F>(init: T, closure: F) -> R
73 where F: Fn(Fix<T, R>, T) -> R
74{
75 Fix(&closure).call(init)
76}
77
78impl<'a, T, R> Fix<'a, T, R> {
79 /// Call the fix using the argument `arg`
80 pub fn call(&self, arg: T) -> R {
81 let f = *self;
82 f.0(f, arg)
83 }
84}
85
86impl<'a, T, R> Clone for Fix<'a, T, R> {
87 fn clone(&self) -> Self { *self }
88}
89
90impl<'a, T, R> Copy for Fix<'a, T, R> { }
91
92#[cfg(feature="unstable")]
93impl<'a, T, R> FnOnce<(T,)> for Fix<'a, T, R> {
94 type Output = R;
95 #[inline]
96 extern "rust-call" fn call_once(self, x: (T,)) -> R {
97 self.call(x.0)
98 }
99}
100
101#[cfg(feature="unstable")]
102impl<'a, T, R> FnMut<(T,)> for Fix<'a, T, R> {
103 #[inline]
104 extern "rust-call" fn call_mut(&mut self, x: (T,)) -> R {
105 self.call(x.0)
106 }
107}
108
109#[cfg(feature="unstable")]
110impl<'a, T, R> Fn<(T,)> for Fix<'a, T, R> {
111 #[inline]
112 extern "rust-call" fn call(&self, x: (T,)) -> R {
113 self.call(x.0)
114 }
115}
116