Skip to content
johnmcclean-aol edited this page Feb 24, 2016 · 12 revisions

Cyclops has merged with simple-react. Please update your bookmarks (stars :) ) to https://github.com/aol/cyclops-react

All new develpoment on cyclops occurs in cyclops-react. Older modules are still available in maven central.

screen shot 2016-02-22 at 8 44 42 pm

The Matchable Interface

Implementing com.aol.cyclops.matcher.Matchable adds a Pattern Matching support to any object.

Coercing to Matchable

Any Object can be coerced to a Matchable via the following methods

  • Matchable.of
  • Matchable.listOfValues

When a match is called on coerced Matchable

  • The host object will be provided as an argument to the Pattern Matcher
  • If the host object implements Decomposable that will be used to recursively pattern match (if required)
  • If the host doesn't implement Decomposable and recursive pattern matching is being used, it will be coerced to Decomposable

Example : matchType

    FeatureToggle<Data> apply;

    Data d = apply.matches( 
		   c -> c.isType((Enabled<Data> e)-> e.get()),
		   c ->	c.isType( (Disabled<Data> d) -> Data.empty()
                           ));

Example : matchListOfValues

Matchable.listOfValues(1,2)
         .matches(
                   c->c.hasValues(1,3).then(i->2),
                   c->c.hasValues(1,2).then(i->3)
                  );

//returns 3

Example matchRecursive

String result = Matchable.listOfValues(1,new MyCase(4,5,6))
         .matches(
                   c->c.hasValues(Predicates.__,Predicates.hasValues(4,5,6)).then(i->"rec1"),
                   c->c.hasValues(2,Predicates.hasValues(1,2,3)).then(i->"rec2")
                  );

//"rec1"
		

Example mayMatch

Matchable.of(Optional.empty())
				            .mayMatch(
				            			o-> o.isEmpty().then(i->"hello"),
				            			o-> o.hasValues(1).then(i->""+2)
				            		)

Example Coerce to Matchable

With Lombok @Value

    As.asMatchable(new MyCase2(1,2,3)).match(this::cases),equalTo("hello")
    @Value static class MyCase { String key; int value;}
Clone this wiki locally