Skip to content

Commit

Permalink
issue iluwatar#335 review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Crossy147 committed Feb 21, 2016
1 parent 80ff7bb commit 81e8d35
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion monad/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ together step by step. Binding functions can be described as passing one's outpu
basing on the 'same type' contract. Formally, monad consists of a type constructor M and two
operations:
bind - that takes monadic object and a function from plain object to monadic value and returns monadic value
return - that takse plain type object and returns this object wrapped in a monadic value.
return - that takes plain type object and returns this object wrapped in a monadic value.

![alt text](./etc/monad.png "Monad")

Expand Down
16 changes: 16 additions & 0 deletions monad/src/main/java/com/iluwatar/monad/App.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
package com.iluwatar.monad;

import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;

/**
* The Monad pattern defines a monad structure, that enables chaining operations
* in pipelines and processing data step by step.
* Formally, monad consists of a type constructor M and two operations:
* <br>bind - that takes monadic object and a function from plain object to the
* monadic value and returns monadic value.
* <br>return - that takes plain type object and returns this object wrapped in a monadic value.
* <p>
* In the given example, the Monad pattern is represented as a {@link Validator} that takes an instance
* of a plain object with {@link Validator#of(Object)}
* and validates it {@link Validator#validate(Function, Predicate, String)} against given predicates.
* <p>As a validation result {@link Validator#get()} it either returns valid object {@link Validator#t}
* or throws a list of exceptions {@link Validator#exceptions} collected during validation.
*/
public class App {

/**
Expand Down
6 changes: 3 additions & 3 deletions monad/src/test/java/com/iluwatar/monad/MonadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ public void testForInvalidName() {
public void testForInvalidAge() {
thrown.expect(IllegalStateException.class);
User john = new User("John", 17, Sex.MALE, "[email protected]");
Validator.of(tom).validate(User::getName, Objects::nonNull, "name cannot be null")
Validator.of(john).validate(User::getName, Objects::nonNull, "name cannot be null")
.validate(User::getAge, age -> age > 21, "user is underaged")
.get();
}

@Test
public void testForValid() {
User sarah = new User("Sarah", 42, Sex.FEMALE, "[email protected]");
User validated = Validator.of(tom).validate(User::getName, Objects::nonNull, "name cannot be null")
User validated = Validator.of(sarah).validate(User::getName, Objects::nonNull, "name cannot be null")
.validate(User::getAge, age -> age > 21, "user is underaged")
.validate(User::getSex, sex -> sex == Sex.FEMALE, "user is not female")
.validate(User::getEmail, email -> email.contains("@"), "email does not contain @ sign")
.get();
Assert.assertSame(validated, tom);
Assert.assertSame(validated, sarah);
}
}

0 comments on commit 81e8d35

Please sign in to comment.