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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use {List, Queue};

#[cfg(not(feature="nightly"))]
use F as Fn;

/// Functor over heterogenous list
///
/// # Example
/// ```rust
/// #![cfg_attr(feature="nightly", feature(unsize, fn_traits, unboxed_closures))]
/// #[macro_use]
/// extern crate hetseq;
/// 
/// use hetseq::{Functor, Queue};
/// #[cfg(not(feature="nightly"))]
/// use hetseq::F;
/// 
/// use std::fmt::Display;
/// lambda![ let Formatter = |const arg: Display| -> String { format!("{}", arg) } ];
/// fn main() {
///     let queue = hqueue![1, 2.5];
///     let strings = queue.fmap(&Formatter);
///     assert_eq!(strings, hqueue!["1".to_owned(), "2.5".to_owned()]);
/// }
/// ```
pub trait Functor<F> {
    /// Result of mapping
    type Output;

    /// Map sequence using `F`unction
    fn fmap(self, F) -> Self::Output;
}

impl<F> Functor<F> for List<()> {
    type Output = List<()>;
    fn fmap(self, _: F) -> List<()> {
        self
    }
}

impl<F> Functor<F> for Queue<()> {
    type Output = Queue<()>;
    fn fmap(self, _: F) -> Queue<()> {
        self
    }
}

impl<X, H, T, O, U> Functor<X> for List<(H, List<T>)>
    where List<T>: Functor<X, Output=List<U>>,
          X: Fn<(H,), Output=O>,
{
    type Output = List<(O, List<U>)>;
    fn fmap(self, f: X) -> Self::Output {
        let List((head, tail)) = self;
        let head = f.call((head,));
        tail.fmap(f).push(head)
    }
}

impl<X, H, T, O, U> Functor<X> for Queue<(Queue<H>, T)>
    where Queue<H>: Functor<X, Output=Queue<U>>,
          X: Fn<(T,), Output=O> + Clone,
{
    type Output = Queue<(Queue<U>, O)>;
    fn fmap(self, f: X) -> Self::Output {
        let Queue((head, tail)) = self;
        head.fmap(f.clone()).push(f.call((tail,)))
    }
}