Slide 6
Slide 6 text
#hackplusstartup
JOINED AT BIRTH
Map is functional
• In FP, map is a higher-order function that takes two
parameters: a function f, and a list of values.
• It applies f to each item in the list, and returns a new list
composed of the results.
Reduce is functional
• Reduce is a special case of the fold function.
• In FP, fold is a function that takes three parameters: a
function, a value, and a list of values.
• It combines all values in the list into a single value, using
f.
}
def map(f: Function, items: List): List = {
val newItems = new ListBuffer
items.foreach(item => {
newItems.add(f(item))
})
return newItems
}
def fold(f: Function, s: S, items: List): S = {
var retval = s
items.foreach(item => {
retval = f(retval, item)
})
return retval
}
def reduce(f: Function, items: List): S =
fold(f, items.head, items.tail)