introduction to functional programming
play

Introduction to Functional Programming Introduction to Functional - PowerPoint PPT Presentation

Introduction to Functional Programming Introduction to Functional Programming Practice Strategy 2018/19 What makes programming functional? Introduction to Functional Programming Imperative code Practice Strategy 2018/19 function


  1. Introduction to Functional Programming

  2. Introduction to Functional Programming Practice Strategy 2018/19 What makes programming functional?

  3. Introduction to Functional Programming Imperative code Practice Strategy 2018/19 function countGreaterThan10(numbers) { let total = 0; for (const num of numbers) { if (num > 10) { total++; } } return total; }

  4. Introduction to Functional Programming Functional code Practice Strategy 2018/19 function countGreaterThan10(numbers) { return numbers.filter(n => n > 10).length; }

  5. Introduction to Functional Programming Higher Order Functions Practice Strategy 2018/19 A function is data ● Therefore it can be used like data ● So a function can be passed to a function ● Let’s talk about map... ●

  6. Introduction to Functional Programming Imperative code Practice Strategy 2018/19 function sqrtAll(values) { for (const i in values) { values[i] = Math.sqrt(value); } return values; }

  7. Introduction to Functional Programming Functional code Practice Strategy 2018/19 function sqrtAll(values) { return values.map(x => Math.sqrt(x)); }

  8. Introduction to Functional Programming Practice Strategy 2018/19 What makes a function pure?

  9. Introduction to Functional Programming Category Theory and Composition Practice Strategy 2018/19 Given a function f that maps X -> Y ● And a function g that maps Y -> Z ● We know that applying f and g will turn X -> Z ●

  10. Introduction to Functional Programming Composition Practice Strategy 2018/19 const result = countGreaterThan10(sqrtAll(filterEven([ 1, 5, 10, 15, 20, 25]))); const numEvenAndSqrtGreaterThan10 = compose( filterEven, sqrtAll, countGreaterThan10 ); const result = numEvenAndSqrtGreaterThan10([ 1, 5, 10, 15, 20, 25]); function compose(...fns) { return function (arg) { return fns.reduceRight((lastResult, fn) => fn(lastResult), arg); } }

  11. Introduction to Functional Programming ESNext Magic Practice Strategy 2018/19 const result = [1, 5, 10, 15, 20, 25] |> filterEven |> sqrtAll |> countGreaterThan10;

  12. Introduction to Functional Programming Currying and Partial Application Practice Strategy 2018/19 function addToAll(amount, numbers) { return numbers.map(n => n + amount); } const add100ToAll = curry(addToAll, 100); const result = [1, 5, 10, 15, 20, 25] |> add100ToAll |> filterEven |> sqrtAll |> countGreaterThan10; function curry(fn, arguments) { return function (remainingArguments) { return fn(...arguments, ...remainingArguments) } }

  13. Introduction to Functional Programming Go forth and be functional Practice Strategy 2018/19 Higher Order Functions ● Pure Functions ● Category Theory ● Composition ● Curring ● … and monads ●

  14. ljn.io/posts/introduction-to-functional-programming

Recommend


More recommend