Skip to content

Commit 211aa31

Browse files
committed
filter, foreach and map functions in scala
1 parent c602da7 commit 211aa31

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/***
2+
* 1. filter filters out elements that meet predicates criteria
3+
* 2. filterNot filters out elements that do not meed predicates criteria.
4+
* 3. exists tests if something exists in a collection based on a function
5+
* 4. These methods are available on List, Range, Set, Map, String, Option etc.
6+
***/
7+
object FilterFunction extends App
8+
{
9+
val a = 1 to 10
10+
11+
//filter function
12+
println("a.filter(x => x % 2 == 0) : " + a.filter(x => x % 2 == 0)) //Filter even numbers
13+
14+
//filterNot function
15+
println("a.filterNot(x => x % 2 == 0) : " + a.filterNot(x => x % 2 == 0)) //Filter odd numbers
16+
17+
//exists
18+
println("a.exists(_ % 2 == 0) : " + a.exists(_ % 2 == 0)) //does a contain even numbers
19+
20+
//filter function using a string
21+
def filterVowels(s:String) = s.toLowerCase().filter(c => Set('a', 'e', 'i', 'o', 'u').contains(c))
22+
println("filterVowels(\"Orange\") :" + filterVowels("Orange"))
23+
24+
//filter function on a set
25+
val b = Set("Red", "Green", "Purple", "Blue", "Orange");
26+
27+
//filter colours containing no. vowels > 1
28+
println("b.filter(s => filterVowels(s).size > 1) : " + b.filter(s => filterVowels(s).size > 1))
29+
30+
//filter function on a map
31+
val c = Map(1 -> "One", 2 -> "Two", 3 -> "Three", 4 -> "Four")
32+
println("c.filterKeys( _ % 2 == 0) : " + c.filterKeys( _ % 2 == 0)) //filter even keys
33+
34+
//filter function on Option
35+
println("Some(5).filter(_ % 2 == 0) : " + Some(5).filter(_ % 2 == 0))
36+
println("Some(4).filter(_ % 2 == 0) : " + Some(4).filter(_ % 2 == 0))
37+
38+
}
39+
40+
/**
41+
Sample Output
42+
a.filter(x => x % 2 == 0) : Vector(2, 4, 6, 8, 10)
43+
a.filterNot(x => x % 2 == 0) : Vector(1, 3, 5, 7, 9)
44+
a.exists(_ % 2 == 0) : true
45+
filterVowels("Orange") :oae
46+
b.filter(s => filterVowels(s).size > 1) : Set(Blue, Green, Purple, Orange)
47+
c.filterKeys( _ % 2 == 0) : Map(2 -> Two, 4 -> Four)
48+
Some(5).filter(_ % 2 == 0) : None
49+
Some(4).filter(_ % 2 == 0) : Some(4)
50+
**/
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
1. foreach is a method that takes a function which takes each element and returns unit
3+
2. Useful if you want to print to screen
4+
3. foreach available on List, Range, Set, Map, String, Option, Streams etc.
5+
4. Difference between map and foreach is map must return something
6+
**/
7+
object ForEachFunction extends App
8+
{
9+
val a = 1 to 10
10+
a.foreach( x => println(x))
11+
12+
//above line can be reduced to
13+
/* a foreach( println _)
14+
a foreach println
15+
*/
16+
}
17+
18+
/**
19+
Sample Output
20+
1
21+
2
22+
3
23+
4
24+
5
25+
6
26+
7
27+
8
28+
9
29+
10
30+
**/

Special_Functions/MapFunction.scala

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
object MapFunction extends App
2+
{
3+
//Applying map function on a list
4+
val a = List(1,2,3,4,5,6)
5+
val f = (x:Int)=> x+1
6+
7+
println("a.map(f) :" + a.map(f))
8+
println("a.map((x:Int) => x+1) : " + a.map((x:Int) => x+1))
9+
println("a.map(x => x+1):" + a.map(x => x+1))
10+
println("a.map(_ + 1):" + a.map(_ + 1))
11+
println("a.map(1 + _):" + a.map(1 + _))
12+
13+
//This import is required to remove compilation warning as-
14+
//warning: there was one feature warning; re-run with -feature for details
15+
import scala.language.postfixOps
16+
println("a.map(1+):" + a.map(1+)) //since we have removed _ for 1+ above warning is displayed
17+
18+
//Applying map function on a Set
19+
var b = Set("Brown", "Red", "Green", "Purple", "Grey", "Yellow")
20+
println("b.map(x => x.size):" + b.map(x => x.size)) //Remember, Set contains unique values only.
21+
println("b.map(_.size):" + b.map(_.size))
22+
println("b.map(x => (x, x.size)): " + b.map(x => (x, x.size)))
23+
24+
//Applying map function on a Map
25+
val fifaTeams = Map('Germany -> 4, 'Brazil -> 5 , 'Italy -> 4, 'Argentina -> 2 )
26+
println("fifaTeams.map(t => (Symbol.apply(\"Team \" + t._1.name), t._2)) : " + fifaTeams.map(t => (Symbol.apply("Team " + t._1.name), t._2)))
27+
28+
//Applying map function on a String
29+
println("\"Hello!\".map(x => (x+1)).toChar:" + "Hello!".map(x => (x+1).toChar))
30+
31+
//Applying map function on Some
32+
println("Some(4).map(1+) :" + Some(4).map(1+))
33+
34+
//Applying map function on None
35+
println("None.asInstanceOf[Option[Int]].map(1+) :" + None.asInstanceOf[Option[Int]].map(1+))
36+
}
37+
38+
/**
39+
Sample Output
40+
a.map(f) :List(2, 3, 4, 5, 6, 7)
41+
a.map((x:Int) => x+1) : List(2, 3, 4, 5, 6, 7)
42+
a.map(x => x+1):List(2, 3, 4, 5, 6, 7)
43+
a.map(_ + 1):List(2, 3, 4, 5, 6, 7)
44+
a.map(1 + _):List(2, 3, 4, 5, 6, 7)
45+
a.map(1+):List(2, 3, 4, 5, 6, 7)
46+
b.map(x => x.size):Set(6, 3, 5, 4)
47+
b.map(_.size):Set(6, 3, 5, 4)
48+
b.map(x => (x, x.size)): Set((Green,5), (Brown,5), (Red,3), (Yellow,6), (Grey,4), (Purple,6))
49+
fifaTeams.map(t => (Symbol.apply("Team " + t._1.name), t._2)) : Map('Team Germany -> 4, 'Team Brazil -> 5, 'Team Italy -> 4, 'Team Argentina -> 2)
50+
"Hello!".map(x => (x+1)).toChar:Ifmmp"
51+
Some(4).map(1+) :Some(5)
52+
None.asInstanceOf[Option[Int]].map(1+) :None
53+
54+
**/

0 commit comments

Comments
 (0)