Skip to content

Commit 81e8d35

Browse files
committed
issue iluwatar#335 review changes
1 parent 80ff7bb commit 81e8d35

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

monad/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ together step by step. Binding functions can be described as passing one's outpu
1717
basing on the 'same type' contract. Formally, monad consists of a type constructor M and two
1818
operations:
1919
bind - that takes monadic object and a function from plain object to monadic value and returns monadic value
20-
return - that takse plain type object and returns this object wrapped in a monadic value.
20+
return - that takes plain type object and returns this object wrapped in a monadic value.
2121

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

monad/src/main/java/com/iluwatar/monad/App.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
package com.iluwatar.monad;
22

33
import java.util.Objects;
4+
import java.util.function.Function;
5+
import java.util.function.Predicate;
46

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

723
/**

monad/src/test/java/com/iluwatar/monad/MonadTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ public void testForInvalidName() {
2424
public void testForInvalidAge() {
2525
thrown.expect(IllegalStateException.class);
2626
User john = new User("John", 17, Sex.MALE, "john@qwe.bar");
27-
Validator.of(tom).validate(User::getName, Objects::nonNull, "name cannot be null")
27+
Validator.of(john).validate(User::getName, Objects::nonNull, "name cannot be null")
2828
.validate(User::getAge, age -> age > 21, "user is underaged")
2929
.get();
3030
}
3131

3232
@Test
3333
public void testForValid() {
3434
User sarah = new User("Sarah", 42, Sex.FEMALE, "sarah@det.org");
35-
User validated = Validator.of(tom).validate(User::getName, Objects::nonNull, "name cannot be null")
35+
User validated = Validator.of(sarah).validate(User::getName, Objects::nonNull, "name cannot be null")
3636
.validate(User::getAge, age -> age > 21, "user is underaged")
3737
.validate(User::getSex, sex -> sex == Sex.FEMALE, "user is not female")
3838
.validate(User::getEmail, email -> email.contains("@"), "email does not contain @ sign")
3939
.get();
40-
Assert.assertSame(validated, tom);
40+
Assert.assertSame(validated, sarah);
4141
}
4242
}

0 commit comments

Comments
 (0)