aboutsummaryrefslogtreecommitdiff
path: root/primrose/crates/primrose-library/src/proptest/mod.rs
blob: 7807854743ff592c84fbf9ef438a28f2f765c093 (plain)
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
pub mod strategies;

pub use im::conslist;
use im::conslist::ConsList;
use std::sync::Arc;

pub fn contains<T: PartialEq>(list: &ConsList<T>, elem: &T) -> bool {
    list.iter().any(|x| x.as_ref() == elem)
}

pub fn clear<T>(_list: &ConsList<T>) -> ConsList<T> {
    ConsList::<T>::new()
}

pub fn remove<T: PartialEq + Clone>(list: &ConsList<T>, a: T) -> (ConsList<T>, Option<T>) {
    if contains(list, &a) {
        let mut result = ConsList::<T>::new();
        let mut found = false;
        for i in list.iter() {
            if i.as_ref() == &a && !found {
                found = true;
                continue;
            } else {
                result = result.append(conslist![i.clone()]);
            }
        }
        (result, Some(a))
    } else {
        (list.clone(), None)
    }
}

pub fn first<T>(list: &ConsList<T>) -> Option<&T> {
    list.head().map(|x| unsafe { &*Arc::into_raw(x) })
}

pub fn last<T>(list: &ConsList<T>) -> Option<&T> {
    list.reverse().head().map(|x| unsafe { &*Arc::into_raw(x) })
}

pub fn nth<T>(list: &ConsList<T>, n: usize) -> Option<&T> {
    list.iter().nth(n).map(|x| unsafe { &*Arc::into_raw(x) })
}

pub fn push<T>(list: &ConsList<T>, a: T) -> ConsList<T> {
    list.append(conslist![a])
}

pub fn pop<T>(list: &ConsList<T>) -> (ConsList<T>, Option<Arc<T>>) {
    if list.is_empty() {
        (ConsList::<T>::new(), None)
    } else {
        let (elem, result) = list.reverse().uncons().unwrap();
        (result.reverse(), Some(elem))
    }
}

pub fn unique<T: PartialEq>(list: &ConsList<T>) -> ConsList<T> {
    let mut result = ConsList::<T>::new();
    for i in list.iter() {
        if contains(&result, &i) {
            continue;
        } else {
            result = result.append(conslist![i.clone()]);
        }
    }
    result
}