4.12 Fundamentals of Functional Programming Flashcards
Main Features of Functional Programming (6)
- Functions used as building blocks
- Statements written as a series of functions
- Each accepts input data as arguments and returns an output
- Statelessness and immutability
- Can’t overwrite/edit
- All can do is perform cals + return a value
- Inputs/outputs can be values or functions
Benefits of Functional Programming (4)
- Easy to write, understand, predict behaviour
- Less prone to bugs as no overwriting/editing
- Allows parallel processing
- Data can’t be altered by previous function so no worries about execution order
- More than 1 processor can work on different parts of large data set at once
- Won’t affect any other part of data
- Helps deal with big data
A First Class Object is one that may (4)
- Be assigned to a variable
- Appear in an expression
- Be assigned to an argument
- Be returned in a function call
Function Application (2, Example)
- Process of giving particular inputs to a function
- Apply/call function by using name followed by argos needed separated by spaces
addThreeInputs(x, y, z) = x + y + z
Integer → Integer → Integer → Integer ( x → y → z → output)
Partial Function Application (3, Example)
- Uses a function + applies it partially with another argument
- Reduces num of args passed to function
- Can perform multiple times to further reduce
E.g. Area(A,B) → AB
Partially apply Area to the arguments 3 & 5:
Area5(A) → A5 (only need to apply 1 argument)
Composition of Functions (1, Example)
- Process results in combining two functions to get a new function
E.g. f: A → B f(x) = x + 2
g: B → C g(x) = x2
Function: g𐩑f = (x+2)2
Domain = A, Co-Domain = C
Function (definition)
A rule that for each element of some set A of inputs, assigns an output from a chosen set B but without necessarily using every member of set B
Domain and Co-Domain (definition) (1)
- Domain = set of inputs
- Co-Domain = set of outputs
- Can have different set/data types of domain & co domains
Higher Order Functions (definition)
- Takes a function as an arg or returns a function as a result or both
MAP (def, recursion process, example)
- Applies given function to each element in list and returns a list
Recursive: take Head of list, apply function, recursive through Tail - E.g. Square(x) = x2
- Map Square([4, 7, 1, 2]) Outputs: [16, 49, 1, 4]
FILTER (def, example)
- Processes a data structure to produce new structure containing exactly elements of original that match a given condition
- E.g. Filter(>10): [8. 9, 10, 11, 12] Outputs: [11, 12]
FOLD OR REDUCE (def, example)
- Reduces list of values to single value by repeatedly applying a combining function to list
- E.g. Fold 1 (+) [4, 7, 9, 1] Outputs: 1+4+7+9+1: 22
Head and Tails (4)
- Head: Tail
- The Head is always a value
- The Tail is always a list
- Can use head and tail to write functions that use recursion