Skip to content

Commit 4addb9f

Browse files
committed
FlatMap in scala
1 parent 30e5fff commit 4addb9f

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* 1. For Comprehensions are just map, flatMap, filter, withFilter and foreach behind the scenes.
3+
* 2. They look like for loops but are not.
4+
* 3. They returns collections like List, Set, Options etc.
5+
* 4. Any object with map, flatMap, withFilter and foreach can be used in For Comprehensions.
6+
**/
7+
8+
object ForComprehensions extends App
9+
{
10+
//Simple for loop
11+
for(i <- 1 to 10) print(i + " ")
12+
13+
println("")
14+
15+
//for loop with yield on range
16+
val res1 = for(i <- 1 to 10) yield ( i + 1)
17+
println("for(i <- 1 to 10) yield ( i + 1) : " + res1)
18+
19+
//using map function on range
20+
val res2 = (1 to 10).map(i => i+1)
21+
println("(1 to 10).map(i => i+1) : " + res2)
22+
23+
24+
//using for-yield on Options
25+
val res3 = for(i <- Some(100)) yield (i + 40)
26+
println("for(i <- Some(100)) yield (i + 40) : " + res3)
27+
28+
//using map function on Options
29+
val res4 = Some(100).map( i => i + 40)
30+
println("Some(100).map( i => i + 40) : " + res4)
31+
32+
33+
//using for-yield on 2 list at a time
34+
val res5 = for(i <- 1 to 4;
35+
j <- 5 to 8) yield(i,j)
36+
println("for(i <- 1 to 4; j <- 5 to 8) yield(i,j) : " + res5)
37+
38+
//using map and flatMap functions on 2 lists at a time
39+
val res6 = (1 to 4).flatMap(i => (5 to 8).map(j => (i,j)))
40+
println("(1 to 4).flatMap(i => (5 to 8).map(j => (i,j))) : " + res6)
41+
42+
43+
//using for-yield on 2 lists with conditions. Using only even numbers in 1st List
44+
val res7 = for( i <- List(1,2,3,4)
45+
if(i % 2 == 0);
46+
j <- List(5,6,7,8))
47+
yield (i, j)
48+
println("for(i <- List(1,2,3,4) if(i % 2 == 0); j <- List(5,6,7,8)) yield (i, j) : " + res7)
49+
50+
val res8 = for(i <- List(1,2,3,4);
51+
j <- List(5,6,7,8)
52+
if(j < 7)) yield (i,j)
53+
println("for(i <- List(1,2,3,4);j <- List(5,6,7,8) if(j < 7)) yield (i,j) : " + res8)
54+
55+
//using filter-flatMap on 2 lists with conditions
56+
val res9 = List(1,2,3,4)
57+
.filter(i => i%2 == 0)
58+
.flatMap(i => List(5,6,7,8).map(j => (i,j)))
59+
println("List(1,2,3,4).filter(i => i%2 == 0).flatMap(i => List(5,6,7,8).map(j => (i,j))) : " + res9 )
60+
61+
val res10 = List(1,2,3,4)
62+
.flatMap( i => List(5,6,7,8)
63+
.filter(j => j < 7)
64+
.map(j => (i,j)))
65+
println("List(1,2,3,4).flatMap( i => List(5,6,7,8).filter(j => j < 7).map(j => (i,j))) : " + res10)
66+
67+
//using withFilter-flatMap-map on 2 Lists with conditions
68+
val res11 = List(1,2,3,4)
69+
.withFilter(i => i%2 == 0)
70+
.flatMap(i => List(5,6,7,8).map(j => (i,j)))
71+
println("List(1,2,3,4).withFilter(i => i%2 == 0).flatMap(i => List(5,6,7,8).map(j => (i,j))) : " + res11 )
72+
73+
val res12 = List(1,2,3,4)
74+
.flatMap( i => List(5,6,7,8)
75+
.withFilter(j => j < 7)
76+
.map(j => (i,j)))
77+
println("List(1,2,3,4).flatMap( i => List(5,6,7,8).withFilter(j => j < 7).map(j => (i,j))) : " + res12)
78+
}
79+
80+
/**
81+
Sample Output
82+
83+
1 2 3 4 5 6 7 8 9 10
84+
for(i <- 1 to 10) yield ( i + 1) : Vector(2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
85+
(1 to 10).map(i => i+1) : Vector(2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
86+
for(i <- Some(100)) yield (i + 40) : Some(140)
87+
Some(100).map( i => i + 40) : Some(140)
88+
for(i <- 1 to 4; j <- 5 to 8) yield(i,j) : Vector((1,5), (1,6), (1,7), (1,8), (2,5), (2,6), (2,7), (2,8), (3,5), (3,6), (3,7), (3,8), (4,5), (4,6), (4,7), (4,8))
89+
(1 to 4).flatMap(i => (5 to 8).map(j => (i,j))) : Vector((1,5), (1,6), (1,7), (1,8), (2,5), (2,6), (2,7), (2,8), (3,5), (3,6), (3,7), (3,8), (4,5), (4,6), (4,7), (4,8))
90+
for(i <- List(1,2,3,4) if(i % 2 == 0); j <- List(5,6,7,8)) yield (i, j) : List((2,5), (2,6), (2,7), (2,8), (4,5), (4,6), (4,7), (4,8))
91+
for(i <- List(1,2,3,4);j <- List(5,6,7,8) if(j < 7)) yield (i,j) : List((1,5), (1,6), (2,5), (2,6), (3,5), (3,6), (4,5), (4,6))
92+
List(1,2,3,4).filter(i => i%2 == 0).flatMap(i => List(5,6,7,8).map(j => (i,j))) : List((2,5), (2,6), (2,7), (2,8), (4,5), (4,6), (4,7), (4,8))
93+
List(1,2,3,4).flatMap( i => List(5,6,7,8).filter(j => j < 7).map(j => (i,j))) : List((1,5), (1,6), (2,5), (2,6), (3,5), (3,6), (4,5), (4,6))
94+
List(1,2,3,4).withFilter(i => i%2 == 0).flatMap(i => List(5,6,7,8).map(j => (i,j))) : List((2,5), (2,6), (2,7), (2,8), (4,5), (4,6), (4,7), (4,8))
95+
List(1,2,3,4).flatMap( i => List(5,6,7,8).withFilter(j => j < 7).map(j => (i,j))) : List((1,5), (1,6), (2,5), (2,6), (3,5), (3,6), (4,5), (4,6))
96+
**/

0 commit comments

Comments
 (0)