4.12.2.1 Functional language programs Flashcards
What is a higher order function?
A function is higher-order if it takes a function as an argument or returns a function as a result, or does both.
What is the map function?
A higher-order function that applies a given function to each element of a list,returning a list of results.
items = [1,2,3,4,5] squared = list(map(lambda x: x**2, items)) # returns [1, 4, 9, 16, 25]
What is the filter function?
A higher-order function that processes a data structure, typically a list, in some order to produce a new data structure containing exactly those elements of the original data structure that match a given condition.
number_list = [-3,-2,-1,0,1,2,3] print (list(filter(lambda x: x < 0, number_list))) # returns [-3,-2,-1]
What is the reduce / fold function?
A higher-order function which reduces a list of values to a single value by repeatedly applying a combining function to the list values.
product = functools.reduce((lambda x, y: x * y), [1,2,3,4]) # returns 1 * 2 * 3 * 4 = 24