From 3c12edb5f640ed5eaf6a8a1c57f2707addc6da01 Mon Sep 17 00:00:00 2001 From: Thiago dos Santos Hora Date: Fri, 13 Jan 2023 03:23:34 +0100 Subject: [PATCH] BAEL-5959: Monad in Java (#13278) * BAEL-5959: Monad in Java * Fix test name --- .../java/com/baeldung/monad/MonadSamples.java | 113 ++++++++++++++++++ .../baeldung/monad/MonadSampleUnitTest.java | 39 ++++++ 2 files changed, 152 insertions(+) create mode 100644 core-java-modules/core-java-8/src/main/java/com/baeldung/monad/MonadSamples.java create mode 100644 core-java-modules/core-java-8/src/test/java/com/baeldung/monad/MonadSampleUnitTest.java diff --git a/core-java-modules/core-java-8/src/main/java/com/baeldung/monad/MonadSamples.java b/core-java-modules/core-java-8/src/main/java/com/baeldung/monad/MonadSamples.java new file mode 100644 index 0000000000..3ccbac2a3c --- /dev/null +++ b/core-java-modules/core-java-8/src/main/java/com/baeldung/monad/MonadSamples.java @@ -0,0 +1,113 @@ +package com.baeldung.monad; + +import java.util.Optional; +import java.util.function.Function; + +class MonadBaseExample { + + public double multiplyBy2(double n) { + return n * 2; + } + + public double divideBy2(double n) { + return n / 2; + } + + public double add3(double n) { + return n + 3; + } + + public double subtract1(double n) { + return n - 1; + } + +} + +class MonadSample1 extends MonadBaseExample { + + public double apply(double n) { + return subtract1(add3(divideBy2(multiplyBy2(multiplyBy2(2))))); + } + + public static void main(String[] args) { + final MonadSample1 test = new MonadSample1(); + System.out.println(test.apply(2)); + //6.0 + } + +} + +class MonadSample2 extends MonadBaseExample { + public double apply(double n) { + final double n1 = multiplyBy2(n); + final double n2 = multiplyBy2(n1); + final double n3 = divideBy2(n2); + final double n4 = add3(n3); + return subtract1(n4); + } + + public static void main(String[] args) { + final MonadSample2 test = new MonadSample2(); + System.out.println(test.apply(2)); + //6.0 + } + +} + +class MonadSample3 extends MonadBaseExample { + + public double apply(double n) { + return Optional.of(n) + .flatMap(value -> Optional.of(multiplyBy2(value))) + .flatMap(value -> Optional.of(multiplyBy2(value))) + .flatMap(value -> Optional.of(divideBy2(value))) + .flatMap(value -> Optional.of(add3(value))) + .flatMap(value -> Optional.of(subtract1(value))) + .get(); + } + + public static void main(String[] args) { + final MonadSample3 test = new MonadSample3(); + System.out.println(test.apply(2)); + //6.0 + } +} + + class MonadSample4 extends MonadBaseExample { + public boolean leftIdentity() { + Function> mapping = value -> Optional.of(value + 1); + return Optional.of(3).flatMap(mapping).equals(mapping.apply(3)); + } + + public boolean rightIdentity() { + return Optional.of(3).flatMap(Optional::of).equals(Optional.of(3)); + } + + public boolean associativity() { + Function> mapping = value -> Optional.of(value + 1); + Optional leftSide = Optional.of(3).flatMap(mapping).flatMap(Optional::of); + Optional rightSide = Optional.of(3).flatMap(v -> mapping.apply(v).flatMap(Optional::of)); + return leftSide.equals(rightSide); + } + + public static void main(String[] args) { + final MonadSample4 test = new MonadSample4(); + + System.out.println(test.leftIdentity()); //true + System.out.println(test.rightIdentity()); //true + System.out.println(test.associativity()); //true + } + } + +class MonadSample5 extends MonadBaseExample { + public boolean fail() { + Function> mapping = value -> Optional.of(value == null ? -1 : value + 1); + return Optional.ofNullable((Integer) null).flatMap(mapping).equals(mapping.apply(null)); + } + + public static void main(String[] args) { + final MonadSample5 test = new MonadSample5(); + System.out.println(test.fail()); + } +} + diff --git a/core-java-modules/core-java-8/src/test/java/com/baeldung/monad/MonadSampleUnitTest.java b/core-java-modules/core-java-8/src/test/java/com/baeldung/monad/MonadSampleUnitTest.java new file mode 100644 index 0000000000..c851f5f750 --- /dev/null +++ b/core-java-modules/core-java-8/src/test/java/com/baeldung/monad/MonadSampleUnitTest.java @@ -0,0 +1,39 @@ +package com.baeldung.monad; + +import org.junit.Assert; +import org.junit.Test; + +public class MonadSampleUnitTest { + + @Test + public void whenNotUsingMonad_shouldBeOk() { + MonadSample1 test = new MonadSample1(); + Assert.assertEquals(6.0, test.apply(2), 0.000); + } + + @Test + public void whenNotUsingMonadButUsingTempVars_shouldBeOk() { + MonadSample2 test = new MonadSample2(); + Assert.assertEquals(6.0, test.apply(2), 0.000); + } + + @Test + public void whenUsingMonad_shouldBeOk() { + MonadSample3 test = new MonadSample3(); + Assert.assertEquals(6.0, test.apply(2), 0.000); + } + + @Test + public void whenTestingMonadProperties_shouldBeOk() { + MonadSample4 test = new MonadSample4(); + Assert.assertEquals(true, test.leftIdentity()); + Assert.assertEquals(true, test.rightIdentity()); + Assert.assertEquals(true, test.associativity()); + } + + @Test + public void whenBreakingMonadProperties_shouldBeFalse() { + MonadSample5 test = new MonadSample5(); + Assert.assertEquals(false, test.fail()); + } +}