|
1 | 1 | lambda |
2 | 2 | ====== |
3 | 3 |
|
4 | | -Functional patterns for Java 8 |
| 4 | +Functional patterns for Java 8 |
| 5 | + |
| 6 | +Installation |
| 7 | +------------ |
| 8 | + |
| 9 | +Add the following dependency to your: |
| 10 | + |
| 11 | +`pom.xml` ([Maven](https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html)): |
| 12 | + |
| 13 | + ```xml |
| 14 | + <dependency> |
| 15 | + <groupId>com.jnape.palatable</groupId> |
| 16 | + <artifactId>lambda</artifactId> |
| 17 | + <version>1.0</version> |
| 18 | + </dependency> |
| 19 | + ``` |
| 20 | + |
| 21 | + `build.gradle` ([Gradle](https://docs.gradle.org/current/userguide/dependency_management.html)): |
| 22 | + |
| 23 | + ```gradle |
| 24 | + compile group: 'com.jnape.palatable', name: 'lambda', version: '1.0' |
| 25 | + ``` |
| 26 | + |
| 27 | +Background |
| 28 | +---------- |
| 29 | + |
| 30 | +Lambda was born out of a desire to use some of the same canonical functions (e.g. `unfoldr`, `takeWhile`, `zipWith`) and functional patterns (e.g. `Functor` and friends) that are idiomatic in other languages and make them available for Java. |
| 31 | + |
| 32 | +Some things a user of lambda most likely values: |
| 33 | + |
| 34 | +- Lazy evaluation |
| 35 | +- Immutablility by design |
| 36 | +- Composition |
| 37 | +- Higher-level abstractions |
| 38 | +- Parametric polymorphism |
| 39 | + |
| 40 | +Generally, everything that lambda produces is lazily-evaluated (except for terminal operations like `reduce`), immutable (except for `Iterator`s, since it's effectively impossible), composable (even between different arities, where possible), foundational (maximally contravariant), and parametrically type-checked (even where this adds unnecessary constraints due to a lack of higher-kinded types). |
| 41 | + |
| 42 | +Although the library is currently (very) small, these values should always be the driving forces behind future growth. |
| 43 | + |
| 44 | +Examples |
| 45 | +-------- |
| 46 | + |
| 47 | +First, the obligatory `map`/`filter`/`reduce` example: |
| 48 | +```Java |
| 49 | + Integer sumOfEvenIncrements = |
| 50 | + reduceLeft((x, y) -> x + y, |
| 51 | + filter(x -> x % 2 == 0, |
| 52 | + map(x -> x + 1, asList(1, 2, 3, 4, 5)))); |
| 53 | + //-> 12 |
| 54 | +``` |
| 55 | + |
| 56 | +Every function in lambda is [curried](https://www.wikiwand.com/en/Currying), so we could have also done this: |
| 57 | +```Java |
| 58 | + MonadicFunction<Iterable<Integer>, Integer> sumOfEvenIncrementsFn = |
| 59 | + map((Integer x) -> x + 1) |
| 60 | + .then(filter(x -> x % 2 == 0)) |
| 61 | + .then(reduceLeft((x, y) -> x + y)); |
| 62 | + |
| 63 | + Integer sumOfEvenIncrements = sumOfEvenIncrementsFn.apply(asList(1, 2, 3, 4, 5)); |
| 64 | + //-> 12 |
| 65 | +``` |
| 66 | + |
| 67 | +How about the positive squares below 100: |
| 68 | + |
| 69 | +```Java |
| 70 | + Iterable<Integer> positiveSquaresBelow100 = |
| 71 | + takeWhile(x -> x < 100, map(x -> x * x, iterate(x -> x + 1, 1))); |
| 72 | + //-> [1, 4, 9, 16, 25, 36, 49, 64, 81] |
| 73 | +``` |
| 74 | + |
| 75 | +We could have also used `unfoldr`: |
| 76 | + |
| 77 | +```Java |
| 78 | + Iterable<Integer> positiveSquaresBelow100 = unfoldr(x -> { |
| 79 | + int square = x * x; |
| 80 | + return square < 100 ? Optional.of(tuple(square, x + 1)) : Optional.empty(); |
| 81 | + }, 1); |
| 82 | + //-> [1, 4, 9, 16, 25, 36, 49, 64, 81] |
| 83 | +``` |
| 84 | + |
| 85 | +What if we want the cross product of a domain and codomain: |
| 86 | + |
| 87 | +```Java |
| 88 | + Iterable<Tuple2<Integer, String>> crossProduct = |
| 89 | + take(10, cartesianProduct(asList(1, 2, 3), asList("a", "b", "c"))); |
| 90 | + //-> (1,"a"), (1,"b"), (1,"c"), (2,"a"), (2,"b"), (2,"c"), (3,"a"), (3,"b"), (3,"c") |
| 91 | +``` |
| 92 | + |
| 93 | +Let's compose two functions: |
| 94 | + |
| 95 | +```Java |
| 96 | + MonadicFunction<Integer, Integer> add = x -> x + 1; |
| 97 | + MonadicFunction<Integer, Integer> subtract = x -> x -1; |
| 98 | + |
| 99 | + MonadicFunction<Integer, Integer> noOp = add.then(subtract); |
| 100 | + // same as |
| 101 | + MonadicFunction<Integer, Integer> alsoNoOp = subtract.fmap(add); |
| 102 | +``` |
| 103 | + |
| 104 | +And partially apply some: |
| 105 | + |
| 106 | +```Java |
| 107 | + DyadicFunction<Integer, Integer, Integer> add = (x, y) -> x + y; |
| 108 | + |
| 109 | + MonadicFunction<Integer, Integer> add1 = add.apply(1); |
| 110 | + add1.apply(2); |
| 111 | + //-> 3 |
| 112 | +``` |
| 113 | + |
| 114 | +And have fun with 3s: |
| 115 | + |
| 116 | +```Java |
| 117 | + Iterable<Iterable<Integer>> multiplesOf3InGroupsOf3 = |
| 118 | + take(10, inGroupsOf(3, unfoldr(x -> Optional.of(tuple(x * 3, x + 1)), 1))); |
| 119 | + //-> [[3, 6, 9], [12, 15, 18], [21, 24, 27]] |
| 120 | +``` |
| 121 | + |
| 122 | +Or check out [the tests](https://github.com/palatable/lambda/tree/master/src/test/java/com/jnape/palatable/lambda/functions/builtin) for more examples. |
| 123 | + |
| 124 | +license |
| 125 | +------- |
| 126 | + |
| 127 | +_lambda_ is part of [palatable](http://www.github.com/palatable), which is distributed under [The MIT License](http://choosealicense.com/licenses/mit/). |
0 commit comments