memoization & hof

7
HOF & Memoization Surendra Shukla

Upload: surendra-shukla

Post on 18-Feb-2017

36 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Memoization & HOF

HOF & MemoizationSurendra Shukla

Page 2: Memoization & HOF

HOF (Higher Order Function) & Memoization Page 2

• Memoization–Memoizing result

• HOF (Higher Order Functions)– Partial Application• Partial Left• Partial Right• Partial Any

–Currying

Topics

Page 3: Memoization & HOF

HOF (Higher Order Function) & Memoization Page 3

• Functions can use objects to remember the results of previous operations.

• This optimization is called memoization.• Objects and arrays are very convenient for this.

Memoization

Page 4: Memoization & HOF

HOF (Higher Order Function) & Memoization Page 4

• In mathematics and computer science, a higher-order function is a function that does at least one of the following: • Takes one or more functions as an input • Outputs a function

• E.g.: h(x) = f(g(x));

HOF (Higher Order Function)

Page 5: Memoization & HOF

HOF (Higher Order Function) & Memoization Page 5

• Allows you to apply a function partially, What this means is that, • You have a function that takes more than one parameter.• You pass it fewer parameters than it wants,

– and it returns a new function.• That will take the remaining parameters

– and it returns a result.

• E.g.: f: X * Y -> R

• f`: Y -> R – (X × Y → R) × X) → (Y → R)

HOF – Partial Application / Partially applied functions

Page 6: Memoization & HOF

HOF (Higher Order Function) & Memoization Page 6

• Currying is the technique of:• Translating the evaluation of a function that takes multiple

arguments (or a tuple of arguments) • Into evaluating a sequence of functions, each with a single

argument (partial application)

• E.g.: (X × Y → R) → (X → (Y → R))

• h (x, y, z) = f(x)(y)(z);

HOF – Currying

Page 7: Memoization & HOF

THANKS

Page 7