Skip to content

johnlcox/motif

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Motif

A pattern matching library for Java 8. Motif provides scala-like pattern matching in Java 8.

Examples

FizzBuzz

IntStream.range(0, 101).forEach(
    n -> System.out.println(
        match(Tuple2.of(n % 3, n % 5))
            .when(tuple2(eq(0), eq(0))).get(() -> "FizzBuzz")
            .when(tuple2(eq(0), any())).get(y -> "Fizz")
            .when(tuple2(any(), eq(0))).get(x -> "Buzz")
            .orElse(String.valueOf(n))
            .getMatch()
    )
);

Optional

Optional<Person> personOpt = getPerson();
match(personOpt)
    .when(some(any())).then(person -> doStuff(person))
    .when(none()).then(() -> System.out.println("Person not found"))
    .doMatch();

Factorial

public long factorial(long i) {
  return match(i)
      .when(eq(0)).get(() -> 1l)
      .when(any()).get(x -> x * factorial(x - 1))
      .getMatch();
}

Nested Matching

Optional<Tuple2<String, String>> opt = Optional.of(Tuple2.of("first", "second"));
match(opt)
    .when(some(tuple2(eq("third"), any()))).then(b -> doStuff(b))
    .when(some(tuple2(any(), eq("second")))).then(a -> doStuff(a))
    .when(none()).then(() -> System.out.println("Tuple not found"))
    .doMatch();

List Cons Matching

List<String> list = Arrays.asList("a", "b", "c", "d");
match(list)
    .when(nil()).then(() -> System.out.println("Empty List"))
    .when(headNil(eq("b"))).then(() -> System.out.println("Singleton List of 'b'"))
    .when(headNil(any())).then(head -> System.out.println("Singleton List of " + head))
    .when(headTail(any(), any())).then(
        (head, tail) -> System.out.println("head: " + head + " Remaining: " + tail))
    .doMatch();

Download

Add the Sonatype snapshot repository:

<repository>
  <id>ossrh</id>
  <name>Sonatype OSSRH</name>
  <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
  <snapshots>
    <enabled>true</enabled>
  </snapshots>
</repository>

Download the latest snapshot JAR via Maven:

<dependency>
  <groupId>com.leacox.motif</groupId>
  <artifactId>motif</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

License

Copyright 2015 John Leacox

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.