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
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//! # A set of useful simple additional methods
//!
//! Examples:
//!
//! ```rust
//! use moreops::*;
//!
//! // Simple wrapping into Option
//! let some_num = 123.some();
//! let none_num = none::<i32>();
//!
//! // Simple wrapping into Result
//! let ok = 123.ok();
//! let err = "Error!".to_owned().err();
//!
//! // If-like operations with Option
//! let x = 42;
//! let answer = (x % 2 == 0).option("even").unwrap_or("odd");
//!
//! // Tap into some result (like `<|` and `|>` operators from Scalaz)
//! fn f() -> i32 {
//!     123
//! }
//! assert_eq!(f().tap(|x| println!("{:?}", x)), 123);
//! assert_eq!(f().pipe(|x| x * 2), 246);
//!
//! // Swap result
//! assert_eq!(123.ok().swap(), 123.err());
//! assert_eq!(123.ok().swap().swap(), 123.ok());
//!
//! // Wrap into tuple
//! let one = 123.once();
//! let two = 123.twice();
//! let three = 123.thrice();
//!
//! // Apply functions to tuples of args directly
//! let x = (2, 3, 4).apply(|a, b, c| a * b * c);
//! assert_eq!(x, 24);
//!
//! // Use twice() to build map
//! let map = (1..10).map(twice).collect::<::std::collections::BTreeMap<_, _>>();
//! ```

pub trait OptionOps {
    fn some(self) -> Option<Self> where Self: Sized {
        Some(self)
    }
}

impl<T> OptionOps for T {}

pub fn none<T>() -> Option<T> {
    None
}

pub trait ResultOps<T = ()> {
    fn ok(self) -> Result<Self, T> where Self: Sized {
        Ok(self)
    }

    fn err(self) -> Result<T, Self> where Self: Sized {
        Err(self)
    }
}

impl<T> ResultOps for T {}

pub trait BoolOps {
    fn option<T>(self, x: T) -> Option<T>;
}

impl BoolOps for bool {
    fn option<T>(self, x: T) -> Option<T> {
        if self {
            Some(x)
        } else {
            None
        }
    }   
}

pub trait TapOps {
    fn tap<R, F: Fn(&Self) -> R>(self, f: F) -> Self where Self: Sized {
        let _ = f(&self);
        self
    }

    fn pipe<R, F: FnOnce(Self) -> R>(self, f: F) -> R where Self: Sized {
        f(self)
    }
}

impl<T> TapOps for T {}

pub trait TupleOps: Sized {
    fn once(self) -> (Self,) {
        (self,)
    }

    fn twice(self) -> (Self, Self) where Self: Clone {
        (self.clone(), self)
    }

    fn thrice(self) -> (Self, Self, Self) where Self: Clone {
        (self.clone(), self.clone(), self)
    }
}

impl<T> TupleOps for T {}

pub fn once<A>(a: A) -> (A,) { (a,) }
pub fn twice<A: Clone>(a: A) -> (A, A) { (a.clone(), a) }
pub fn thrice<A: Clone>(a: A) -> (A, A, A) { (a.clone(), a.clone(), a) }

pub trait ResultExt<T, E>: Sized {
    fn swap(self) -> Result<E, T>;
}

impl<T, E> ResultExt<T, E> for Result<T, E> {
    fn swap(self) -> Result<E, T> {
        match self {
            Ok(v) => Err(v),
            Err(e) => Ok(e),
        }
    }
}

macro_rules! impl_apply_ops {
    ($name:ident, $($t:ident),+) => {
        pub trait $name<$($t),*> {
            fn apply<R, F: FnOnce($($t),*) -> R>(self, f: F) -> R;
        }
        impl<$($t),*> $name<$($t),*> for ($($t),*,) {
            #[allow(non_snake_case)]
            fn apply<R, F: FnOnce($($t),*) -> R>(self, f: F) -> R {
                let ($($t),*,) = self;
                f($($t),*)
            }
        }
    }
}
impl_apply_ops!(Apply1Ops, A1);
impl_apply_ops!(Apply2Ops, A1, A2);
impl_apply_ops!(Apply3Ops, A1, A2, A3);
impl_apply_ops!(Apply4Ops, A1, A2, A3, A4);
impl_apply_ops!(Apply5Ops, A1, A2, A3, A4, A5);
impl_apply_ops!(Apply6Ops, A1, A2, A3, A4, A5, A6);
impl_apply_ops!(Apply7Ops, A1, A2, A3, A4, A5, A6, A7);
impl_apply_ops!(Apply8Ops, A1, A2, A3, A4, A5, A6, A7, A8);
impl_apply_ops!(Apply9Ops, A1, A2, A3, A4, A5, A6, A7, A8, A9);
impl_apply_ops!(Apply10Ops, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10);
impl_apply_ops!(Apply11Ops, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11);
impl_apply_ops!(Apply12Ops, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12);

#[test]
fn it_works() {
    assert_eq!(1.some(), Some(1));
    assert_eq!(none::<i32>(), None);

    assert_eq!(true.option(1), Some(1));
    assert_eq!(false.option(1), None);

    assert_eq!(1.tap(|&n| {
        assert_eq!(n, 1);
        2
    }), 1);

    assert_eq!(123.ok().swap(), 123.err());

    assert_eq!(123.once(), (123,));
    assert_eq!(123.twice(), (123, 123));
    assert_eq!(123.thrice(), (123, 123, 123));

    assert_eq!((2, 3).apply(::std::ops::Mul::mul), 6);
}