Skip to content

Commit 72e0836

Browse files
committed
issue iluwatar#335 documentation improvements
1 parent 3e526cb commit 72e0836

5 files changed

Lines changed: 77 additions & 8 deletions

File tree

monad/etc/monad.png

32.2 KB
Loading

monad/etc/monad.ucls

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagram version="1.1.9" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
3+
associations="true" dependencies="false" nesting-relationships="true" router="FAN">
4+
<class id="1" language="java" name="com.iluwatar.monad.App" project="monad"
5+
file="/monad/src/main/java/com/iluwatar/monad/App.java" binary="false" corner="BOTTOM_RIGHT">
6+
<position height="101" width="125" x="631" y="37"/>
7+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
8+
sort-features="false" accessors="true" visibility="true">
9+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
10+
<operations public="true" package="true" protected="true" private="true" static="true"/>
11+
</display>
12+
</class>
13+
<enumeration id="2" language="java" name="com.iluwatar.monad.Sex" project="monad"
14+
file="/monad/src/main/java/com/iluwatar/monad/Sex.java" binary="false" corner="BOTTOM_RIGHT">
15+
<position height="119" width="137" x="424" y="286"/>
16+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
17+
sort-features="false" accessors="true" visibility="true">
18+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
19+
<operations public="true" package="true" protected="true" private="true" static="true"/>
20+
</display>
21+
</enumeration>
22+
<class id="3" language="java" name="com.iluwatar.monad.User" project="monad"
23+
file="/monad/src/main/java/com/iluwatar/monad/User.java" binary="false" corner="BOTTOM_RIGHT">
24+
<position height="209" width="167" x="424" y="37"/>
25+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
26+
sort-features="false" accessors="true" visibility="true">
27+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
28+
<operations public="true" package="true" protected="true" private="true" static="true"/>
29+
</display>
30+
</class>
31+
<class id="4" language="java" name="com.iluwatar.monad.Validator" project="monad"
32+
file="/monad/src/main/java/com/iluwatar/monad/Validator.java" binary="false" corner="BOTTOM_RIGHT">
33+
<position height="191" width="343" x="41" y="37"/>
34+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
35+
sort-features="false" accessors="true" visibility="true">
36+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
37+
<operations public="true" package="true" protected="true" private="true" static="true"/>
38+
</display>
39+
</class>
40+
<association id="5">
41+
<end type="SOURCE" refId="3" navigable="false">
42+
<attribute id="6" name="sex"/>
43+
<multiplicity id="7" minimum="0" maximum="1"/>
44+
</end>
45+
<end type="TARGET" refId="2" navigable="true"/>
46+
<display labels="true" multiplicity="true"/>
47+
</association>
48+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
49+
sort-features="false" accessors="true" visibility="true">
50+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
51+
<operations public="true" package="true" protected="true" private="true" static="true"/>
52+
</classifier-display>
53+
<association-display labels="true" multiplicity="true"/>
54+
</class-diagram>

monad/index.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@ layout: pattern
33
title: Monad
44
folder: monad
55
permalink: /patterns/monad/
6-
categories: Presentation Tier
6+
categories: Other
77
tags:
88
- Java
99
- Difficulty-Advanced
10+
- Functional
1011
---
1112

1213
## Intent
1314

1415
Monad pattern based on monad from linear algebra represents the way of chaining operations
1516
together step by step. Binding functions can be described as passing one's output to another's input
16-
basing on the 'same type' contract.
17+
basing on the 'same type' contract. Formally, monad consists of a type constructor M and two
18+
operations:
19+
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.
1721

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

@@ -25,5 +29,7 @@ Use the Monad in any of the following situations
2529
* when you want to apply each function regardless of the result of any of them
2630

2731
## Credits
32+
2833
* [Design Pattern Reloaded by Remi Forax](https://youtu.be/-k2X7guaArU)
2934
* [Brian Beckman: Don't fear the Monad](https://channel9.msdn.com/Shows/Going+Deep/Brian-Beckman-Dont-fear-the-Monads)
35+
* [Monad (functional programming) on Wikipedia] (https://en.wikipedia.org/wiki/Monad_(functional_programming)

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ public class User {
88
private String email;
99

1010
/**
11-
*
12-
* @param name - name
13-
* @param age - age
14-
* @param sex - sex
15-
* @param email - email
11+
* @param name - name
12+
* @param age - age
13+
* @param sex - sex
14+
* @param email - email address
1615
*/
1716
public User(String name, int age, Sex sex, String email) {
1817
this.name = name;

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,22 @@
1111
* given object together step by step. In Validator each step results in either success or
1212
* failure indicator, giving a way of receiving each of them easily and finally getting
1313
* validated object or list of exceptions.
14+
*
1415
* @param <T> Placeholder for an object.
1516
*/
1617
public class Validator<T> {
18+
/**
19+
* Object that is validated
20+
*/
1721
private final T t;
22+
23+
/**
24+
* List of exception thrown during validation.
25+
*/
1826
private final List<Throwable> exceptions = new ArrayList<>();
1927

2028
/**
29+
* Creates a monadic value of given object.
2130
* @param t object to be validated
2231
*/
2332
private Validator(T t) {
@@ -52,6 +61,7 @@ public Validator<T> validate(Predicate<T> validation, String message) {
5261
/**
5362
* Extension for the {@link Validator#validate(Function, Predicate, String)} method,
5463
* dedicated for objects, that need to be projected before requested validation.
64+
*
5565
* @param projection function that gets an objects, and returns projection representing
5666
* element to be validated.
5767
* @param validation see {@link Validator#validate(Function, Predicate, String)}
@@ -65,7 +75,7 @@ public <U> Validator<T> validate(Function<T, U> projection, Predicate<U> validat
6575
}
6676

6777
/**
68-
* To receive validated object.
78+
* Receives validated object or throws exception when invalid.
6979
*
7080
* @return object that was validated
7181
* @throws IllegalStateException when any validation step results with failure

0 commit comments

Comments
 (0)