pa 0.1.5

Functional Programming tools and ADTs
Documentation
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Copyright 2018 KaguyaRs Developers
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(trace_macros)]

#[macro_use]
extern crate pa;

#[test]
/// macro compose!
fn compose() {
    let f = compose! {
        |x| x+1, |x| x*2
    };
    assert_eq!(f(3), 7);
    assert_eq!(f(-1), -1);

    fn add_one(x: i64) -> i64 {
        x + 1
    }
    fn multi_two(x: i64) -> i64 {
        x * 2
    }
    let f2 = compose!(add_one, multi_two);
    
    assert_eq!(f2(3), 7);
    assert_eq!(f2(-1), -1);
}

#[test]
/// macro compose! for type projection
fn compose_type_projection() {
    // f :: &str -> (&str -> Vec String -> usize) -> usize
    let f = compose! {
        |x: Vec<&str>| x.len(),
        |x: &'static str| x.split(' ').collect()
    };
    assert_eq!(f("Houraisan Kaguya"), 2);
}

#[test]
/// macro pipe!
fn pipe() {
    let f = pipe! {
        |x| x+1, |x| x*2
    };
    assert_eq!(f(3), 8);
    assert_eq!(f(-1), 0);

    fn add_one(x: i64) -> i64 {
        x + 1
    }
    fn multi_two(x: i64) -> i64 {
        x * 2
    }
    let f2 = pipe!(add_one, multi_two);
    
    assert_eq!(f2(3), 8);
    assert_eq!(f2(-1), 0);
}

#[test]
/// macro pipe! for type projection
fn pipe_type_projection() {
    // f :: &str -> (&str -> Vec String -> usize) -> usize
    let f = pipe! {
        |x: &'static str| x.split(' ').collect(),
        |x: Vec<&str>| x.len()        
    };
    assert_eq!(f("Houraisan Kaguya"), 2);
}

#[test]
// fn map and curry map
fn map() {
    use pa::basic_fn::fun::map;
    let v = vec![1,2,3];
    let result: Vec<i32> = map(|x| x+1, v.iter()).collect();
    assert_eq!(result, vec![2,3,4]);

    let curry = map!(|x| x+1);
    let result2: Vec<i32> = curry(v.iter()).collect();
    assert_eq!(result2, vec![2,3,4]);
}

#[test]
// fn filter and curry filter
fn filter() {
    use pa::basic_fn::fun::filter;
    let v = vec![1,2,3];
    let odd = filter(|&x| x & 1 == 1, v.iter()).map(|&x| x).collect::<Vec<i32>>();
    assert_eq!(odd, vec![1,3]);

    let curry = filter!(|&x| x&1 == 0);
    let even = curry(v.iter()).map(|&x| x).collect::<Vec<i32>>();
    assert_eq!(even, vec![2]);
}

#[test]
// fn filter_not and curry filter_not
fn filter_not() {
    use pa::basic_fn::fun::filter_not;
    let v = vec![1,2,3];
    let even = filter_not(|&x| x & 1 == 1, v.iter()).map(|&x| x).collect::<Vec<i32>>();
    assert_eq!(even, vec![2]);

    let curry = filter_not!(|&x| x&1 == 0);
    let odd = curry(v.iter()).map(|&x| x).collect::<Vec<i32>>();
    assert_eq!(odd, vec![1,3]);
}

#[test]
// fn foldl and curry foldl
fn foldl() {
    use pa::basic_fn::fun::foldl;
    let v = vec![1,2,3];
    let result = foldl(4, |x,y| x*y, v.iter());
    assert_eq!(result, 24);

    let curry1 = foldl!(5);
    let c_result1 = curry1(|x,y| x*y, v.iter());
    assert_eq!(c_result1, 30);

    let curry2 = foldl!(6, |x,y| x-y);
    let c_result2 = curry2(v.iter());
    assert_eq!(c_result2, 0);

    let step_curry = foldl!(0=>);
    let step_curry_2 = step_curry(|x,y| x+y);
    assert_eq!(step_curry_2(v.iter()), 6);
}

#[test]
// fn foldr and curry foldr
fn foldr() {
    use pa::basic_fn::fun::foldr;
    let v = vec!["Houraisan","Kaguya"];
    let result = foldr("".to_string(), |x,y| x + "<|>" + y, v.iter());
    assert_eq!(result, "<|>Kaguya<|>Houraisan");

    let curry1 = foldr!("This is:".to_string());
    let c_result1 = curry1(|x,&y| x+" "+y, v.iter());
    assert_eq!(c_result1, "This is: Kaguya Houraisan");
    
    let curry2 = foldr!("すごい!".to_string(), |x,&y| x+" "+y);
    let c_result2 = curry2(v.iter());
    assert_eq!(c_result2, "すごい! Kaguya Houraisan");

    let step_curry = foldr!("楽しい〜".to_string()=>);
    let step_curry_2 = step_curry(|x,&y| x+" "+y);
    assert_eq!(step_curry_2(v.iter()), "楽しい〜 Kaguya Houraisan");
}

#[test]
// fn sum and macro sum
fn sum() {
    use pa::basic_fn::fun::sum;
    let result = 10;
    assert_eq!(sum(1..=4), result);
    assert_eq!(sum!(1;4), result);
    assert_eq!(sum!(1,2,3,4), result);
}

#[test]
// macro ls: list comprehension
fn ls() {
    // 1. function on item;iter=>function to check condition
    assert_eq!(ls![|x| x+1; 1..=5 => |x| x&1==0], vec![3,5]);
    // 2. iter=>function to check condition
    assert_eq!(ls![0..=4 => |x| x&1==0], vec![0,2,4]);
    // 3. function on item;iter
    assert_eq!(ls![|x| x*x; 0..=4], vec![0,1,4,9,16]);
    // 4. iter
    assert_eq!(ls![0..=4], vec![0,1,2,3,4]);
}

#[test]
// fn head
fn head() {
    use pa::basic_fn::fun::head;
    let empty_vec = Vec::new() as Vec<i8>;
    let vec = vec![1,2,3];

    assert_eq!(None, head(empty_vec.iter()));
    assert_eq!(Some(&1), head(vec.iter()));
}

#[test]
// fn tail
fn tail() {
    use pa::basic_fn::fun::tail;
    let empty_vec = Vec::new() as Vec<i8>;
    let vec = vec![1, 2, 3];

    assert_eq!(None, tail(empty_vec.iter()));
    assert_eq!(Some(vec![&2, &3]), tail(vec.iter()));
}

#[test]
// fn last
fn last() {
    use pa::basic_fn::fun::last;
    let empty_vec = Vec::new() as Vec<i8>;
    let vec = vec![1, 2, 3];

    assert_eq!(None, last(empty_vec.iter()));
    assert_eq!(Some(&3), last(vec.iter()));
}

#[test]
// fn init
fn init() {
    use pa::basic_fn::fun::init;
    let empty_vec = Vec::new() as Vec<i8>;
    let vec = vec![1,2,3];

    assert_eq!(None, init(empty_vec.iter()));
    assert_eq!(Some(vec![&1,&2]), init(vec.iter()));
}

#[test]
// fn and macro skip
fn skip() {
    use pa::basic_fn::fun::skip;
    let empty_vec = Vec::new() as Vec<i8>;
    let vec = vec![1,2,3];

    assert_eq!(Vec::new() as Vec<&i8>, skip(1,empty_vec.iter()));
    assert_eq!(vec![&2,&3], skip(1, vec.iter()));

    let curry = skip!(1);
    assert_eq!((Vec::new() as Vec<&i8>), curry(empty_vec.iter()));
    assert_eq!(vec![&2, &3], curry(vec.iter()));
}

#[test]
// fn and macro take
fn take() {
    use pa::basic_fn::fun::take;
    let empty_rec = Vec::new() as Vec<i8>;
    let vec = vec![1,2,3];

    assert_eq!(Vec::new() as Vec<&i8>, take(2, empty_rec.iter()));
    assert_eq!(vec![&1,&2], take(2, vec.iter()));

    let curry = take!(2);
    assert_eq!(Vec::new() as Vec<&i8>, curry(empty_rec.iter()));
    assert_eq!(vec![&1,&2], curry(vec.iter()));
}

#[test]
// fn and macro product
fn product() {
    use pa::basic_fn::fun::product;
    assert_eq!(product(1..=5), 120);

    assert_eq!(product!(1;5), 120);
    assert_eq!(product!(1,2,3,4,5), 120);
}

#[test]
// fn length
fn length() {
    use pa::basic_fn::fun::length;
    assert_eq!(length(vec![1,2,3,4,5].iter()), 5);
    assert_eq!(length(0..0), 0);
}

#[test]
// fn reverse
fn reverse() {
    use pa::basic_fn::fun::reverse;
    assert_eq!(reverse(1..=5), vec![5,4,3,2,1]);
}

#[test]
// fn and macro concat
fn concat() {
    use pa::basic_fn::fun::concat;
    assert_eq!(concat(0..1,1..2), vec![0,1]);
    assert_eq!(
        concat!(0..1;1..2;2..3;3..=4;vec![5,6].iter()),
        vec![0,1,2,3,4,5,6]
    );
}

#[test]
// fun id
fn id() {
    use pa::basic_fn::fun::id;
    let vec = vec![1,2,3];
    assert_eq!(id(vec.clone()), vec);
}

#[test]
// macro fst
fn fst() {
    assert_eq!(fst!(("a", 1)), "a");
    let f = fst!(>i8,f32,char);
    assert_eq!(f((1,1.2,'3')), 1);
    let a = (1,1.2,'3');
    assert_eq!(fst!(a), 1);
}

#[test]
// macro snd
fn snd() {
    assert_eq!(snd!((1.1, "Bravo")), "Bravo");
    let f = snd!(>String,String,String);
    assert_eq!(f(("Houraisan".to_string(), "Kaguya".to_string(), "美しい".to_string())), "Kaguya");
    let a = (1,3,2.5);
    assert_eq!(snd!(a), 3);
}

#[test]
// fn neg
fn neg() {
    use pa::basic_fn::fun::neg;
    assert_eq!(neg(1), -1);
    assert_eq!(neg(-1), 1);
}

#[test]
// macro abs
fn abs() {
    assert_eq!(abs!(-1_i32), 1);
    assert_eq!(abs!(1_i32), 1);

    let f = abs!(>i32);
    assert_eq!(f(-1), 1);
    assert_eq!(f(1), 1);
}

#[test]
// macro signum
fn signum() {
    assert_eq!(signum!(-100_i32), -1);
    assert_eq!(signum!(0_i32), 0);
    assert_eq!(signum!(5_i32), 1);

    let f = signum!(>i32);
    assert_eq!(f(-100), -1);
    assert_eq!(f(0), 0);
    assert_eq!(f(5), 1);
}

#[test]
// fn and macro rem
fn rem() {
    use pa::basic_fn::fun::rem;
    assert_eq!(rem(3, 2), 1);
    assert_eq!(rem!(3, 2), 1);
    let f = rem!(3_i32);
    assert_eq!(f(2), 1);
}

#[test]
// macro odd/even
fn odd_and_even() {
    assert!(even!(2));
    assert!(odd!(1));

    let f1 = even!(>i32);
    let f2 = odd!(>i32);
    assert!(f1(2));
    assert!(f2(1));
}

#[test]
// macro always
fn always() {
    assert_eq!(always!(1, 'a'), 1);
    assert_eq!(always!(1)('a'), 1);

    let f = always!();
    assert_eq!(f(1, 'a'), 1);

    let f2 = always!(=>);
    assert_eq!(f2(1)('a'), 1);
}

#[test]
// fn and macro add/sub/mul/div
fn add_sub_mul_div() {
    use pa::basic_fn::fun::{add, sub, mul, div};
    // add
    assert_eq!(add(1,2), 1 + 2);
    assert_eq!(add!(1, 2), 1+2);
    assert_eq!(add!(1)(2), 1+2);

    // sub
    assert_eq!(sub(9,1),9-1);
    assert_eq!(sub!(9,1),9-1);
    assert_eq!(sub!(9)(1), 9-1);

    //mul
    assert_eq!(mul(2,3),2*3);
    assert_eq!(mul!(2,3),2*3);
    assert_eq!(mul!(2)(3),2*3);

    // div
    assert_eq!(div(10,5), 10/5);
    assert_eq!(div!(10,5), 10/5);
    assert_eq!(div!(10)(5), 10/5);
}

#[test]
// fn and macro find
fn find() {
    use std::collections::HashMap;
    use pa::basic_fn::fun::find;
    let mut m = HashMap::new();
    m.insert('a', 1);
    m.insert('b', 2);
    m.insert('c', 3);
    assert_eq!(find(&'a', m.iter()), Some((&'a', &1)));
    let f = find!(&'b');
    assert_eq!(f(m.iter()), Some((&'b', &2)));
    assert_eq!(find!(&'d', m.iter()), None);
}

#[test]
// fn sorted
fn sorted() {
    use pa::basic_fn::fun::sorted;
    let vec = vec![1,5,3,7,8,3,9,3,2];
    assert_eq!(sorted(vec.iter()).map(|x| *x).collect::<Vec<i32>>(), vec![1,2,3,3,3,5,7,8,9]);
}

#[test]
// fn and macro sorted_by
fn sorted_by() {
    use pa::basic_fn::fun::sorted_by;
    use std::cmp::Ordering;
    let vec = vec![1,5,3,4,2];
    assert_eq!(vec![5,4,3,2,1], sorted_by(|x,y| y.cmp(x), vec.iter()).map(|x| *x).collect::<Vec<i32>>());

    fn cmp<'a, 'b, T: Ord>(x: &'a T, y: &'b T) -> Ordering {
        x.cmp(y)
    }
    let f = sorted_by!(cmp);
    assert_eq!(vec![1,2,3,4,5], f(vec.iter()).map(|x| *x).collect::<Vec<i32>>());
}

// #[test]
// // macro sorted_with
// fn sorted_with() {
//     let ls = vec![(1,'a'), (1,'b'), (2,'a')];
//     trace_macros!(true);
//     let f = sorted_with!(
//         |x: (&i32, &char),y: (&i32, &char)| x.0.cmp(y.0),
//         |x: (&i32, &char),y: (&i32, &char)| y.1.cmp(x.1)
//     );
//     assert_eq!(vec![(1,'b'), (1,'a'), (2,'a')], f(ls.iter()));
//     trace_macros!(false);
// }

#[test]
// fn and macro zip
fn zip() {
    use pa::basic_fn::fun::zip;
    let ls1 = vec![1,2,3];
    let ls2 = vec!['a','b','c'];
    assert_eq!(zip(ls1.iter(), ls2.iter()).map(|(x,y)| (*x,*y)).collect::<Vec<_>>(), vec![(1,'a'), (2,'b'), (3,'c')]);

    let it = ls1.iter();
    let mac = zip!(it);
    assert_eq!(mac(ls2.iter()).map(|(x,y)| (*x,*y)).collect::<Vec<_>>(), vec![(1,'a'), (2,'b'), (3,'c')]);
}

#[test]
// fn and macro zip_with
fn zip_with() {
    use pa::basic_fn::fun::zip_with;
    let ls1 = vec![1,2,3];
    let ls2 = vec![1,0,1];
    assert_eq!(zip_with(move |(x,y)| x&y == 0, ls1.iter(), ls2.iter()).collect::<Vec<_>>(), vec![false, true, false]);

    let mac1 = zip_with!(move |(x,y)| x&y == 0);
    let mac2 = zip_with!(move |(x,y)| {x&y == 0} =>);
    let it = ls1.iter();
    let mac3 = zip_with!(move |(x,y)| x&y == 0, it);
    assert_eq!(mac1(ls1.iter(), ls2.iter()).collect::<Vec<_>>(), vec![false, true, false]);
    assert_eq!(mac2(ls1.iter())(ls2.iter()).collect::<Vec<_>>(), vec![false, true, false]);
    assert_eq!(mac3(ls2.iter()).collect::<Vec<_>>(), vec![false, true, false]);
}