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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/// uwors: UnWrap Or Return None
/// # Example
/// ```ignore
/// fn test() -> Option<bool>{
///     let x = Some(1);
///     let y = uworn!(x);
///     return Some(y == 0);
/// }
/// assert_eq!(test(), Some(false));
/// ```
#[macro_export]
macro_rules! uworn{
    ($inp:expr) => {
        if let Some(value_inside_macro_some) = $inp { value_inside_macro_some }
        else { return Option::None; };
    }
}

/// Aplies function over each element in slice and returns a vector of the results in order.
/// # Example
/// ```
/// assert_eq!(fnrs::map(&vec![2,3,4], &|x| x + 1), vec![3,4,5]);
/// ```
pub fn map<T,F>(inp: &[T], f: &F) -> Vec<T>
    where
        F: Fn(&T) -> T,
{
    let mut res = Vec::new();
    for x in inp{
        res.push(f(x));
    }
    res
}

pub trait MutFunc<T>{
    fn map<F>(self, f: F) -> Self
        where F: FnMut(&mut T);
    fn mmap<F>(&mut self, f: F)
        where F: FnMut(&mut T);
}

impl<T> MutFunc<T> for Vec<T>{
    /// ```
    /// use fnrs::MutFunc;
    /// assert_eq!(vec![3,6,9],vec![1,2,3].map(|x| *x *= 3));
    /// ```
    fn map<F>(mut self, mut f: F) -> Self
    where F: FnMut(&mut T){
        for x in &mut self{
            f(x);
        }
        self
    }
    /// ```
    /// use fnrs::MutFunc;
    /// let mut x = vec![0,1,2];
    /// x.mmap(|x| *x += 1);
    /// assert_eq!(x, vec![1,2,3]);
    /// ```
    fn mmap<F>(&mut self, mut f: F)
    where F: FnMut(&mut T){
        for x in self{
            f(x);
        }
    }
}

// /// Trait with some fucntions common in functional programming.
// /// Also contains mutable versions.
// pub trait Func<T>{
//     /// Apply a function on each element in the collection.
//     /// When called on collection, will mutate each element in place.
//     fn map_mut<F>(&mut self, f: &F)
//         where F: Fn(T) -> T;
//     /// Apply a function on each element in the collection.
//     /// Consumes the collection, mutates each element in place, then returns the collection.
//     fn map<F>(self, f: &F) -> Self
//         where F: Fn(T) -> T;
//     /// Only keeps elements that the function returns true for.
//     /// Will filter the collection in place, should retain order.
//     fn filter_mut<F>(&mut self, f: &F)
//         where F: Fn(&T) -> bool;
//     /// Only keeps elements that the function returns true for.
//     /// Consumes the collection, mutates it in place, then returns it.
//     fn filter<F>(self, f: &F) -> Self
//         where F: Fn(&T) -> bool;
//     /// Only keeps elements that the function returns true for.
//     /// Will filter the collection in place.
//     /// Works in O(n), but does not retain the order.
//     fn filter_swap_mut<F>(&mut self, f: &F)
//         where F: Fn(&T) -> bool;
//     fn filter_swap<F>(self, f: &F) -> Self
//         where F: Fn(&T) -> bool;
//     /// Folds a collections elements with a function, returning a single value.
//     fn fold<F,R>(&self, f: &F, v: R) -> R
//         where F: Fn(R,T) -> R;
//     /// Returns true when all elements return true for the predicate.
//     fn all<F>(&self, f: &F) -> bool
//         where F: Fn(&T) -> bool;
// }
//
// /// Implementation of ```Func``` for ```Vec<T: Copy>```.
// impl<T: Copy> Func<T> for Vec<T>{
//     /// # Example
//     /// ```
//     /// use fnrs::Func;
//     /// let mut v = vec![0,1,2];
//     /// v.map_mut(&|x| x + 1);
//     /// assert_eq!(v,vec![1,2,3]);
//     /// ```
//     fn map_mut<F>(&mut self, f: &F)
//     where F: Fn(T) -> T{
//         let len = self.len();
//         for i in 0..len{
//             self[i] = f(self[i]);
//         }
//     }
//     /// # Example
//     /// ```
//     /// use fnrs::Func;
//     /// assert_eq!(vec![0,1,2].map(&|x| x + 1), vec![1,2,3]);
//     /// ```
//     fn map<F>(mut self, f: &F) -> Self
//     where F: Fn(T) -> T{
//         self.map_mut(f);
//         self
//     }
//     /// # Example
//     /// ```
//     /// use fnrs::Func;
//     /// assert_eq!(vec![1,2,3,4].fold(&|r,x| r * x, 1), 24);
//     /// ```
//     fn fold<F,R>(&self, f: &F, mut v: R) -> R
//     where F: Fn(R,T) -> R{
//         for x in self{
//             v = f(v,*x);
//         }
//         v
//     }
//     /// # Example
//     /// ```
//     /// use fnrs::Func;
//     /// assert_eq!(vec![3,4,5].all(&|x| x > &2), true);
//     /// assert_eq!(vec![5,3,4].all(&|x| x > &3), false);
//     /// ```
//     fn all<F>(&self, f: &F) -> bool
//     where F: Fn(&T) -> bool{
//         for x in self{
//             if !f(x) { return false; }
//         }
//         true
//     }
//     /// # Example
//     /// ```
//     /// use fnrs::Func;
//     /// let mut v = vec![3,4,2,1,9,2,3];
//     /// v.filter_mut(&|x| x >= &3);
//     /// assert_eq!(v, vec![3,4,9,3]);
//     /// ```
//     fn filter_mut<F>(&mut self, f: &F)
//     where F: Fn(&T) -> bool{
//         if self.is_empty() { return; }
//         let mut i = 0;
//         while i < self.len(){
//             if f(&self[i]) {
//                 i += 1;
//                 continue;
//             }
//             self.remove(i);
//         }
//     }
//     /// # Example
//     /// ```
//     /// use fnrs::Func;
//     /// assert_eq!(vec![0,1,2,3,4,5].filter(&|x| x % 2 == 0), vec![0,2,4]);
//     /// ```
//     fn filter<F>(mut self, f: &F) -> Self
//     where F: Fn(&T) -> bool{
//         self.filter_mut(f);
//         self
//     }
//     /// # Example
//     /// ```
//     /// use fnrs::Func;
//     /// let mut v = vec![true,false,false,true,false];
//     /// v.filter_swap_mut(&|x| *x);
//     /// assert_eq!(v, vec![true,true]);
//     /// ```
//     fn filter_swap_mut<F>(&mut self, f: &F)
//     where F: Fn(&T) -> bool{
//         if self.is_empty() { return; }
//         let mut i = 0;
//         while i < self.len(){
//             if f(&self[i]) {
//                 i += 1;
//                 continue;
//             }
//             self.swap_remove(i);
//         }
//     }
//     /// # Example
//     /// ```
//     /// use fnrs::Func;
//     /// assert_eq!(vec![true,false,false,true,true,false].filter_swap(&|x| !*x), vec![false; 3])
//     /// ```
//     fn filter_swap<F>(mut self, f: &F) -> Self
//     where F: Fn(&T) -> bool{
//         self.filter_mut(f);
//         self
//     }
//}

/// Trait for working with sequences.
/// I only kept Vector in mind.
pub trait Sequence{
    /// Does it contain a sub sequence?
    fn has_seq(&self, seq: &Self) -> bool;
}

impl<T: PartialEq> Sequence for Vec<T>{
    /// # Example
    /// ```
    /// use fnrs::Sequence;
    /// assert_eq!(vec![1,2,1,3,5,9,0].has_seq(&vec![1,3,5]), true);
    /// assert_eq!(vec![1,2,1,3,5,9,0].has_seq(&vec![1,3,5,8]), false);
    /// ```
    fn has_seq(&self, seq: &Self) -> bool{
        let slen = seq.len();
        let mut j = 0;
        for a in self{
            if j == slen { return true; }
            let b = &seq[j];
            if a == b{
                j += 1;
            }else{
                j = 0;
            }
        }
        if j == slen { true }
        else { false }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
    #[test]
    fn test_uworn(){
        fn test() -> Option<bool>{
            let x = Some(1);
            let y = uworn!(x);
            return Some(y == 0);
        }
        assert_eq!(test(), Some(false));
    }
    // #[test]
    // fn test_map(){
    //     assert_eq!(map(&vec![2,3,4], &|x| x + 1), vec![3,4,5]);
    // }
    // // Func tests
    // #[test]
    // fn test_func_map_mut(){
    //     let mut v = vec![0,1,2];
    //     v.map_mut(&|x| x + 1);
    //     assert_eq!(v,vec![1,2,3]);
    // }
    // #[test]
    // fn test_func_map(){
    //     assert_eq!(vec![0,1,2].map(&|x| x + 1), vec![1,2,3]);
    // }
    // #[test]
    // fn test_func_fold(){
    //     assert_eq!(vec![1,2,3,4].fold(&|r,x| r * x, 1), 24);
    // }
    // #[test]
    // fn test_func_all(){
    //     assert_eq!(vec![3,4,5].all(&|x| x > &2), true);
    //     assert_eq!(vec![5,3,4].all(&|x| x > &3), false);
    // }
    // #[test]
    // fn test_func_filter_mut(){
    //     let mut v = vec![3,4,2,1,9,2,3];
    //     v.filter_mut(&|x| x >= &3);
    //     assert_eq!(v, vec![3,4,9,3]);
    // }
    // #[test]
    // fn test_func_filter(){
    //     assert_eq!(vec![0,1,2,3,4,5].filter(&|x| x % 2 == 0), vec![0,2,4]);
    // }
    // #[test]
    // fn test_func_filter_swap_mut(){
    //     let mut v = vec![true,false,false,true,false];
    //     v.filter_swap_mut(&|x| *x);
    //     assert_eq!(v, vec![true,true]);
    // }
    // #[test]
    // fn test_func_filter_swap(){
    //     assert_eq!(vec![true,false,false,true,true,false].filter_swap(&|x| !*x), vec![false; 3])
    // }
    // // Sequence tests
    // #[test]
    // fn test_sequence_has_seq(){
    //     assert_eq!(vec![1,2,1,3,5,9,0].has_seq(&vec![1,3,5]), true);
    //     assert_eq!(vec![1,2,1,3,5,9,0].has_seq(&vec![1,3,5,8]), false);
    // }
}