Adding source code for article tracked under BAEL-4576 (#10203)
Co-authored-by: CHANDRAKANT Kumar <kumar.chandrakant@soprabanking.com>
This commit is contained in:
parent
170ab9123d
commit
0794a1ad1b
|
@ -0,0 +1 @@
|
|||
## Relevant articles:
|
|
@ -0,0 +1,18 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-functional</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-functional</name>
|
||||
<packaging>jar</packaging>
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.functional;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public class Currying {
|
||||
|
||||
private static Function<Double, Function<Double, Double>> weight = mass -> gravity -> mass * gravity;
|
||||
|
||||
private static Function<Double, Double> weightOnEarth = weight.apply(9.81);
|
||||
|
||||
private static Function<Double, Double> weightOnMars = weight.apply(3.75);
|
||||
|
||||
public static Double weightOnEarth(Double mass) {
|
||||
return weightOnEarth.apply(mass);
|
||||
}
|
||||
|
||||
public static Double weightOnMars(Double mass) {
|
||||
return weightOnMars.apply(mass);
|
||||
}
|
||||
|
||||
public static Function<Double, Double> weightOnEarth() {
|
||||
final double gravity = 9.81;
|
||||
return mass -> mass * gravity;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.functional;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class FirstClassFunctions {
|
||||
|
||||
public static List<Integer> sortWithoutLambda(List<Integer> numbers) {
|
||||
Collections.sort(numbers, new Comparator<Integer>() {
|
||||
@Override
|
||||
public int compare(Integer n1, Integer n2) {
|
||||
return n1.compareTo(n2);
|
||||
}
|
||||
});
|
||||
return numbers;
|
||||
}
|
||||
|
||||
public static List<Integer> sortWithLambda(List<Integer> numbers) {
|
||||
Collections.sort(numbers, (n1, n2) -> n1.compareTo(n2));
|
||||
return numbers;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.functional;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public class FunctionComposition {
|
||||
|
||||
private static Function<Double, Double> log = (value) -> Math.log(value);
|
||||
private static Function<Double, Double> sqrt = (value) -> Math.sqrt(value);
|
||||
|
||||
public static Double logThenSqrt(Double number) {
|
||||
Function<Double, Double> logThenSqrt = sqrt.compose(log);
|
||||
return (logThenSqrt.apply(3.14));
|
||||
}
|
||||
|
||||
public static Double sqrtThenLog(Double number) {
|
||||
Function<Double, Double> sqrtThenLog = sqrt.andThen(log);
|
||||
return (sqrtThenLog.apply(3.14));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.functional;
|
||||
|
||||
public class ImmutableData {
|
||||
|
||||
private final String someData;
|
||||
|
||||
private final AnotherImmutableData anotherImmutableData;
|
||||
|
||||
public ImmutableData(final String someData, final AnotherImmutableData anotherImmutableData) {
|
||||
this.someData = someData;
|
||||
this.anotherImmutableData = anotherImmutableData;
|
||||
}
|
||||
|
||||
public String getSomeData() {
|
||||
return someData;
|
||||
}
|
||||
|
||||
public AnotherImmutableData getAnotherImmutableData() {
|
||||
return anotherImmutableData;
|
||||
}
|
||||
|
||||
public class AnotherImmutableData {
|
||||
|
||||
private final Integer someOtherData;
|
||||
|
||||
public AnotherImmutableData(final Integer someData) {
|
||||
this.someOtherData = someData;
|
||||
}
|
||||
|
||||
public Integer getSomeOtherData() {
|
||||
return someOtherData;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.functional;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class Monads {
|
||||
|
||||
public static Optional<Integer> add(Optional<Integer> val1, Optional<Integer> val2) {
|
||||
return val1.flatMap(first -> val2.flatMap(second -> Optional.of(first + second)));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.functional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PureFunctions {
|
||||
|
||||
public static Integer sum(List<Integer> numbers) {
|
||||
return numbers.stream()
|
||||
.collect(Collectors.summingInt(Integer::intValue));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.functional;
|
||||
|
||||
public class Recursion {
|
||||
|
||||
public static Integer headRecursion(Integer number) {
|
||||
|
||||
return (number == 1) ? 1 : number * headRecursion(number - 1);
|
||||
|
||||
}
|
||||
|
||||
public static Integer tailRecursion(Integer number, Integer result) {
|
||||
|
||||
return (number == 1) ? result : tailRecursion(number - 1, result * number);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.baeldung.functional;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class ReferentialTransparency {
|
||||
|
||||
private static Logger logger = Logger.getGlobal();
|
||||
|
||||
public void main() {
|
||||
|
||||
String data = new SimpleData().setData("Baeldung")
|
||||
.getData();
|
||||
logger.log(Level.INFO, new SimpleData().setData("Baeldung")
|
||||
.getData());
|
||||
logger.log(Level.INFO, data);
|
||||
logger.log(Level.INFO, "Baeldung");
|
||||
}
|
||||
|
||||
public class SimpleData {
|
||||
|
||||
private Logger logger = Logger.getGlobal();
|
||||
|
||||
private String data;
|
||||
|
||||
public String getData() {
|
||||
logger.log(Level.INFO, "Get data called for SimpleData");
|
||||
return data;
|
||||
}
|
||||
|
||||
public SimpleData setData(String data) {
|
||||
logger.log(Level.INFO, "Set data called for SimpleData");
|
||||
this.data = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.functional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class CurryingUnitTest {
|
||||
|
||||
@Test
|
||||
public void testWeightOnEarth() {
|
||||
|
||||
assertEquals(588.6, Currying.weightOnEarth(60.0), 0.1);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWeightOnMars() {
|
||||
|
||||
assertEquals(225.0, Currying.weightOnMars(60.0), 0.1);
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.functional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class FirstClassFunctionsUnitTest {
|
||||
|
||||
@Test
|
||||
public void testSortingWithoutLambda() {
|
||||
|
||||
assertEquals(new Integer(8), FirstClassFunctions.sortWithoutLambda(Arrays.asList(new Integer(10), new Integer(8)))
|
||||
.get(0));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSortingWithLambda() {
|
||||
|
||||
assertEquals(new Integer(8), FirstClassFunctions.sortWithLambda(Arrays.asList(new Integer(10), new Integer(8)))
|
||||
.get(0));
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.functional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class FunctionCompositionUnitTest {
|
||||
|
||||
@Test
|
||||
public void testLogThenSqrt() {
|
||||
|
||||
assertEquals(1.07, FunctionComposition.logThenSqrt(3.14), 0.01);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSqrtThenLog() {
|
||||
|
||||
assertEquals(0.57, FunctionComposition.sqrtThenLog(3.14), 0.01);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.functional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class MonadsUnitTest {
|
||||
|
||||
@Test
|
||||
public void testOptionalAdd() {
|
||||
|
||||
assertEquals(5, Monads.add(Optional.of(new Integer(2)), Optional.of(new Integer(3)))
|
||||
.get());
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.functional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class PureFunctionsUnitTets {
|
||||
|
||||
@Test
|
||||
public void testSortingWithoutLambda() {
|
||||
|
||||
assertEquals(new Integer(18), PureFunctions.sum(Arrays.asList(new Integer(10), new Integer(8))));
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.functional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class RecursionUnitTest {
|
||||
|
||||
@Test
|
||||
public void testHeadRecursion() {
|
||||
|
||||
assertEquals(120, Recursion.headRecursion(5));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTailRecursion() {
|
||||
|
||||
assertEquals(120, Recursion.tailRecursion(5, 1));
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -60,6 +60,7 @@
|
|||
<module>core-java-exceptions-2</module>
|
||||
<module>core-java-exceptions-3</module>
|
||||
<module>core-java-function</module>
|
||||
<module>core-java-functional</module>
|
||||
|
||||
<module>core-java-io</module>
|
||||
<module>core-java-io-2</module>
|
||||
|
|
Loading…
Reference in New Issue