Skip to content
This repository was archived by the owner on Feb 10, 2021. It is now read-only.

Commit 44e143a

Browse files
committed
Add functional basics tutorial
1 parent c77c2f6 commit 44e143a

File tree

6 files changed

+138
-0
lines changed

6 files changed

+138
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>Java 8, 9 functional features tutorial
22
The list of tutorials on functional programming techniques and features in Java
3+
* [Functional programming basics](https://github.com/bobocode-projects/java-functional-features-tutorial/tree/master/functional-programming-basics)
34
* [Lambdas](https://github.com/bobocode-projects/java-functional-features-tutorial/tree/master/lambdas)
45
* [Stream API](https://github.com/bobocode-projects/java-functional-features-tutorial/tree/master/stream-api)
56
* [Optional](https://github.com/bobocode-projects/java-functional-features-tutorial/tree/master/optional)
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/> Functional programming tutorial
2+
3+
This is the tutorial on functional programming basics
4+
### Pre-conditions :heavy_exclamation_mark:
5+
You're supposed to be familiar with OOP, have basic knowledge of JDK, and be able to write Java code.
6+
7+
### See also :point_down:
8+
* [Tutorial on Lambdas](https://github.com/bobocode-projects/java-functional-features-tutorial/tree/master/lambdas)
9+
* [Tutorial on Stream API](https://github.com/bobocode-projects/java-8-tutorial/tree/master/stream-api)
10+
* [Tutorial on Optional](https://github.com/bobocode-projects/java-functional-features-tutorial/tree/master/optional)
11+
##
12+
The concept of **functional programming** is based on [λ-calculus](https://en.wikipedia.org/wiki/Lambda_calculus). Which
13+
brings the idea of program that **does not use mutable variables**
14+
15+
Various functional languages like [Clojure](https://clojure.org/) have been using this approach for the long time. **Java**
16+
was initially created as pure OO language, but since **version 8** it offers an ability to use either **OO and functional
17+
approaches.**
18+
19+
Consider an program that calculates **the sum of first 20 prime numbers**.
20+
21+
Here's an OO-based implementation:
22+
23+
```java
24+
public class OOSumOfPrimes {
25+
public static void main(String[] args) {
26+
int sumOfPrimes = 0;
27+
for (int i = 0, primes = 0; primes < 20; i++) {
28+
if (isPrime(i)) {
29+
sumOfPrimes += i;
30+
primes++;
31+
}
32+
}
33+
34+
System.out.println("Sum of first 20 primes: " + sumOfPrimes);
35+
}
36+
37+
private static boolean isPrime(int n) {
38+
for (int i = 2; i < n; i++) {
39+
if (n % i == 0) {
40+
return false;
41+
}
42+
}
43+
return true;
44+
}
45+
}
46+
```
47+
48+
And this is the functional-based implementation:
49+
```java
50+
public class FunctionalSumOfPrimes {
51+
public static void main(String[] args) {
52+
IntStream.iterate(0, i -> i + 1)
53+
.filter(FunctionalSumOfPrimes::isPrime)
54+
.limit(20)
55+
.reduce((a, b) -> a + b)
56+
.ifPresent(sum -> System.out.println("Sum of first 20 primes: " + sum));
57+
}
58+
59+
private static boolean isPrime(int n) {
60+
return IntStream.range(2, n)
61+
.noneMatch(i -> n % i == 0);
62+
}
63+
}
64+
```
65+
66+
As you can see there is **no mutable variables** in the second example

functional-programming-basics/pom.xml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>java-functional-features-tutorial</artifactId>
7+
<groupId>com.bobocode</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>functional-programming-basics</artifactId>
13+
14+
15+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.bobocode;
2+
3+
import java.util.stream.IntStream;
4+
5+
/**
6+
* This example demonstrates how to use functional approach in Java. The program calculates the sum of first 20 prime
7+
* numbers without using mutable variables
8+
*
9+
* See {@link OOSumOfPrimes}
10+
*/
11+
public class FunctionalSumOfPrimes {
12+
public static void main(String[] args) {
13+
IntStream.iterate(0, i -> i + 1)
14+
.filter(FunctionalSumOfPrimes::isPrime)
15+
.limit(20)
16+
.reduce((a, b) -> a + b)
17+
.ifPresent(sum -> System.out.println("Sum of first 20 primes: " + sum));
18+
}
19+
20+
private static boolean isPrime(int n) {
21+
return IntStream.range(2, n)
22+
.noneMatch(i -> n % i == 0);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.bobocode;
2+
3+
/**
4+
* This examples demonstrates how to calculate the sum of prime numbers using OO approach. The point is that to perform
5+
* this calculation in OOP-style we have to use mutable variables (e.g. sumOfPrimes, i, primes)
6+
*
7+
* See {@link FunctionalSumOfPrimes}
8+
*/
9+
public class OOSumOfPrimes {
10+
public static void main(String[] args) {
11+
int sumOfPrimes = 0;
12+
for (int i = 0, primes = 0; primes < 20; i++) {
13+
if (isPrime(i)) {
14+
sumOfPrimes += i;
15+
primes++;
16+
}
17+
}
18+
19+
System.out.println("Sum of first 20 primes: " + sumOfPrimes);
20+
}
21+
22+
private static boolean isPrime(int n) {
23+
for (int i = 2; i < n; i++) {
24+
if (n % i == 0) {
25+
return false;
26+
}
27+
}
28+
return true;
29+
}
30+
31+
}

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<module>stream-api</module>
1515
<module>optional</module>
1616
<module>account-data</module>
17+
<module>functional-programming-basics</module>
1718
</modules>
1819

1920
<properties>

0 commit comments

Comments
 (0)