Introduction to Functional Programming Introduction to Functional - - PowerPoint PPT Presentation

introduction to functional programming
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Introduction to Functional Programming

slide-2
SLIDE 2

Introduction to Functional Programming

Practice Strategy 2018/19

What makes programming functional?

slide-3
SLIDE 3

Introduction to Functional Programming

Practice Strategy 2018/19 Imperative code

function countGreaterThan10(numbers) { let total = 0; for (const num of numbers) { if (num > 10) { total++; } } return total; }

slide-4
SLIDE 4

Introduction to Functional Programming

Practice Strategy 2018/19 Functional code

function countGreaterThan10(numbers) { return numbers.filter(n => n > 10).length; }

slide-5
SLIDE 5

Introduction to Functional Programming

Practice Strategy 2018/19 Higher Order Functions

  • 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...
slide-6
SLIDE 6

Introduction to Functional Programming

Practice Strategy 2018/19 Imperative code

function sqrtAll(values) { for (const i in values) { values[i] = Math.sqrt(value); } return values; }

slide-7
SLIDE 7

Introduction to Functional Programming

Practice Strategy 2018/19 Functional code

function sqrtAll(values) { return values.map(x => Math.sqrt(x)); }

slide-8
SLIDE 8

Introduction to Functional Programming

Practice Strategy 2018/19

What makes a function pure?

slide-9
SLIDE 9

Introduction to Functional Programming

Practice Strategy 2018/19 Category Theory and Composition

  • 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
slide-10
SLIDE 10

Introduction to Functional Programming

Practice Strategy 2018/19 Composition

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); } }

slide-11
SLIDE 11

Introduction to Functional Programming

Practice Strategy 2018/19 ESNext Magic

const result = [1, 5, 10, 15, 20, 25] |> filterEven |> sqrtAll |> countGreaterThan10;

slide-12
SLIDE 12

Introduction to Functional Programming

Practice Strategy 2018/19 Currying and Partial Application

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) } }

slide-13
SLIDE 13

Introduction to Functional Programming

Practice Strategy 2018/19 Go forth and be functional

  • Higher Order Functions
  • Pure Functions
  • Category Theory
  • Composition
  • Curring
  • … and monads
slide-14
SLIDE 14

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