diff --git a/monad/index.md b/monad/index.md index 9ede8e1b4..82deba922 100644 --- a/monad/index.md +++ b/monad/index.md @@ -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") diff --git a/monad/src/main/java/com/iluwatar/monad/App.java b/monad/src/main/java/com/iluwatar/monad/App.java index 2ab376201..e330cfa64 100644 --- a/monad/src/main/java/com/iluwatar/monad/App.java +++ b/monad/src/main/java/com/iluwatar/monad/App.java @@ -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: + *
bind - that takes monadic object and a function from plain object to the + * monadic value and returns monadic value. + *
return - that takes plain type object and returns this object wrapped in a monadic value. + *

+ * 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. + *

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 { /** diff --git a/monad/src/test/java/com/iluwatar/monad/MonadTest.java b/monad/src/test/java/com/iluwatar/monad/MonadTest.java index 2c18e25e7..ae78572f8 100644 --- a/monad/src/test/java/com/iluwatar/monad/MonadTest.java +++ b/monad/src/test/java/com/iluwatar/monad/MonadTest.java @@ -24,7 +24,7 @@ public class MonadTest { public void testForInvalidAge() { thrown.expect(IllegalStateException.class); User john = new User("John", 17, Sex.MALE, "john@qwe.bar"); - 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(); } @@ -32,11 +32,11 @@ public class MonadTest { @Test public void testForValid() { User sarah = new User("Sarah", 42, Sex.FEMALE, "sarah@det.org"); - 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); } }