2. Lack of etiquette and manners is a huge turn off.
KnolX Etiquettes
Punctuality
Respect Knolx session timings, you
are requested not to join sessions
after a 5 minutes threshold post
the session start time.
Feedback
Make sure to submit a constructive
feedback for all sessions as it is
very helpful for the presenter.
Mute
Please keep your window on mute.
Avoid Disturbance
Avoid leaving your window
unmuted after asking a question..
3. Agenda
01 What is Functional Programming
02 What are Higher Order Functions
03 How do we define them in Scala
04 Commonly used Higher Order Functions
05 Demo
5. Functional Programming
● Functional Programming is a style of programming that emphasizes
writing applications using only pure functions and immutable values.
● It is driven mainly by two ideas:
○ Functions are the first-class citizens.
○ Functions should map input values to output values rather than change
data in place.
7. Higher Order Functions
● A function is called a Higher Order Function if it meets one or both of the
following conditions:
○ it takes other functions as parameters
○ it returns a function as a result
● Higher Order Functions are possible because of the fact that functions are
first-class citizens in Scala.
● Higher Order Functions let you raise the level of abstraction.
12. map
● map is a higher-order function that transforms a collection by
applying a function to each of its element.
● The type of the collection remains same but the element type may
change.
def map[B] (f: A => B): Traversable[B]
13. flatMap
● flatMap is identical to the map i.e. it also transforms a collection by
applying a function to each element.
● The only difference is that in flatMap, the inner grouping of an item is
removed and a sequence is generated.
● flatMap acts as a shorthand to map a collection and then immediately
flatten it.
def flatMap[B](f: A => Traversable[B]):Traversable[B]
14. filter
● filter selects elements which satisfy a predicate
● A predicate is a unary function that returns a boolean value
● filterNot selects elements which does not satisfy a predicate.
def filter(f: A => Boolean): Traversable[A]
15. reduce
● reduce is bit different compare to the previous higher-order functions.
● It takes all the elements in a collection and combines them using a binary
operation to produce a single value.
● In other words, It combines multiple elements of the type T into a single
one of type T.
def reduce[B >: A](op: (B, B) => B): B
16. fold
● fold is very similar to reduce.
● It also takes a binary operation which merges all the elements from the
collection and returns a single value.
● The only difference is that fold allows us to define an initial value.
● Due to this property, fold can also manage empty collections.
def fold[B >: A](z: B)(op: (B, B) => B): B