summaryrefslogtreecommitdiff
path: root/src/main/java/au/id/zancanaro/RoseTree.java
blob: 458d441273e77d2bdf383e238b1ca4ee9c0a6794 (about) (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
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
package au.id.zancanaro;

import java.io.IOException;
import java.io.Writer;
import java.util.Collections;
import java.util.Iterator;
import java.util.function.Function;
import java.util.function.Predicate;

public class RoseTree<T> {
    private final T value;
    private final Iterable<RoseTree<T>> children;

    public RoseTree(T value, Iterable<RoseTree<T>> children) {
        this.value = value;
        this.children = children;
    }

    public T getValue() {
        return value;
    }

    public Iterator<RoseTree<T>> getChildren() {
        return children.iterator();
    }

    public static <T> RoseTree<T> pure(T value) {
        return new RoseTree<>(value, Collections.emptyList());
    }

    public static <T> RoseTree<T> join(RoseTree<RoseTree<T>> tree) {
        return new RoseTree<>(
                tree.getValue().getValue(),
                () -> Iterators.concat(
                        Iterators.mappingIterator(RoseTree::join, tree.children.iterator()),
                        tree.getValue().children.iterator()));
    }

    public static <T> Iterator<RoseTree<T>[]> permutations(RoseTree<T>[] trees) {
        return Iterators.flatten(Iterators.rangeIterator(trees.length, index ->
                        Iterators.mappingIterator(child -> {
                            @SuppressWarnings("unchecked")
                            RoseTree<T>[] result = (RoseTree<T>[]) new RoseTree[trees.length];
                            for (int i = 0; i < trees.length; ++i) {
                                result[i] = (i == index ? child : trees[i]);
                            }
                            return result;
                        }, trees[index].getChildren())
        ));
    }

    public static <T, R> RoseTree<R> zip(Function<T[], R> fn, RoseTree<T>[] trees) {
        @SuppressWarnings("unchecked")
        T[] heads = (T[]) new Object[trees.length];
        for (int i = 0; i < trees.length; ++i) {
            heads[i] = trees[i].getValue();
        }
        return new RoseTree<>(
                fn.apply(heads),
                () -> Iterators.mappingIterator(
                        roses -> RoseTree.zip(fn, roses),
                        RoseTree.permutations(trees)));
    }

    public <R> RoseTree<R> fmap(Function<T, R> f) {
        return new RoseTree<>(
                f.apply(this.value),
                () -> Iterators.mappingIterator(tree -> tree.fmap(f), this.children.iterator()));
    }

    public <R> RoseTree<R> flatmap(Function<T, RoseTree<R>> f) {
        return RoseTree.join(this.fmap(f));
    }

    public RoseTree<T> filter(Predicate<T> predicate) {
        if (predicate.test(this.getValue())) {
            return new RoseTree<>(
                    this.getValue(),
                    () -> Iterators.mappingIterator(tree -> tree.filter(predicate),
                            Iterators.filteringIterator(
                                    tree -> predicate.test(tree.getValue()),
                                    this.getChildren())));
        } else {
            throw new IllegalArgumentException("Current value doesn't match predicate: whoops!");
        }
    }

    @SuppressWarnings("unused")
    public void print(Writer output) throws IOException {
        print(output, Object::toString);
    }

    @SuppressWarnings("unused")
    public void print(Writer output, Function<T, String> toString) throws IOException {
        output.write(toString.apply(this.getValue()));
        output.write('[');
        for (RoseTree<T> child : children) {
            child.print(output, toString);
        }
        output.write(']');
        output.flush();
    }
}