File tree Expand file tree Collapse file tree 3 files changed +95
-0
lines changed
java/src/main/java/v/o/i/d/monadsplain Expand file tree Collapse file tree 3 files changed +95
-0
lines changed Original file line number Diff line number Diff line change 1+ package v .o .i .d .monadsplain ;
2+
3+ public class One {
4+
5+ public Integer square (Integer i ) {
6+ return i * i ;
7+ }
8+ public Integer addOne (Integer i ) {
9+ return i + 1 ;
10+ }
11+
12+ public void run () {
13+ System .out .println (addOne (square (3 )));
14+ }
15+ public static void main (String [] args ) {
16+ new One ().run ();
17+ }
18+ }
Original file line number Diff line number Diff line change 1+ package v .o .i .d .monadsplain ;
2+
3+ import java .util .Arrays ;
4+ import java .util .stream .Stream ;
5+
6+ public class Three {
7+
8+ public static class IntegerWithHistory {
9+ public final Integer value ;
10+ public final String [] history ;
11+
12+ public IntegerWithHistory (Integer value , String [] history ) {
13+ this .value = value ;
14+ this .history = history ;
15+ }
16+
17+ public static IntegerWithHistory wrap (Integer i ) {
18+ return new IntegerWithHistory (i , new String [] {});
19+ }
20+ }
21+
22+ public IntegerWithHistory square (IntegerWithHistory i ) {
23+ return new IntegerWithHistory (
24+ i .value * i .value ,
25+ join (i .history , "square: " + i .value )
26+ );
27+ }
28+
29+ public IntegerWithHistory addOne (IntegerWithHistory i ) {
30+ return new IntegerWithHistory (
31+ i .value + 1 ,
32+ join (i .history , "addOne: " + i .value )
33+ );
34+ }
35+
36+ public void run () {
37+ IntegerWithHistory i = addOne (square (IntegerWithHistory .wrap (3 )));
38+ System .out .println (i .value );
39+ System .out .println (Arrays .toString (i .history ));
40+ }
41+
42+ public static void main (String [] args ) {
43+ new Three ().run ();
44+ }
45+
46+ private static String [] join (String [] array , String element ) {
47+ return Stream .concat (Arrays .stream (array ), Stream .of (element )).toArray (String []::new );
48+ }
49+
50+ }
Original file line number Diff line number Diff line change 1+ package v .o .i .d .monadsplain ;
2+
3+ public class Two {
4+ public static class IntegerWithHistory {
5+ public final Integer value ;
6+ public final String [] messages ;
7+
8+ public IntegerWithHistory (Integer value , String [] messages ) {
9+ this .value = value ;
10+ this .messages = messages ;
11+ }
12+ }
13+
14+ public IntegerWithHistory square (Integer i ) {
15+ return new IntegerWithHistory (
16+ i * i ,
17+ new String [] {"square: " + i }
18+ );
19+ }
20+ public IntegerWithHistory addOne (Integer i ) {
21+ return new IntegerWithHistory (
22+ i + 1 ,
23+ new String [] {"addOne: " + i }
24+ );
25+ }
26+
27+ }
You can’t perform that action at this time.
0 commit comments