@Test public void matchOption(){ int defaultValue =-1; Option<Integer> empty = Option.none(); int res = MatchType(empty).with(Case(value->value),Case(nil->defaultValue)); //res is -1 int res2 = empty.fold(v->v,n->-1); //res2 is -1 System.out.println("res is " + res); System.out.println("res2 is " + res2); }
public static <T1, T2> Matching.PatternMatching2<T1, T2> MatchType(Sealed2<T1, T2> value) { return new Matching.PatternMatching2<>(value); }
static <K,V> Tree<K,V> rootIsBlack(Tree<K,V> root){ return Api.MatchType(root).with(Case(node->node.withBlack(true)),Case(leaf->leaf)); } public static <K,V> Tree<K,V> fromStream(Comparator<? super K> comp, Stream<? extends Tuple2<? extends K, ? extends V>> stream){
@Override public T next() { return MatchType(current).with(Case(list->{ current = list.tail(); return list.head(); }), Case(nil->null)); } };
public static <T1, T2> Matching.PatternMatching2<T1, T2> MatchType(Sealed2<T1, T2> value) { return new Matching.PatternMatching2<>(value); }
static <K,V> Tree<K,V> rootIsBlack(Tree<K,V> root){ return Api.MatchType(root).with(Case(node->node.withBlack(true)),Case(leaf->leaf)); } public static <K,V> Tree<K,V> fromStream(Comparator<? super K> comp, Stream<? extends Tuple2<? extends K, ? extends V>> stream){
@Override public T next() { return MatchType(current).with(Case(list->{ current = list.tail(); return list.head(); }), Case(nil->null)); } };
@Test public void matchList(){ Seq<Integer> list = Seq.of(1,2,3); int first = MatchType(list).with(Case(nel->nel.head()),Case(empty->-1)); int first2 = list.fold(s->s.head(),e->-1); //first & first2 are 1 // list.head(); //no such method }