Intro to functional programming

45
Functional Programming by Assaf Gannon

Transcript of Intro to functional programming

Functional Programmingby Assaf Gannon

Why Do We Need to Change The Paradigm?!

Object Oriented Programming is Awesome!

OOP is not cutting it

Testability

Require lots of mocking

Extensive environmental setup

Hard to maintain as the objects evolve

Complexity

Tendency to over-design

Re-Use is often complicated and requires frequent refactoring

Often Objects DON’T represent the problem correctly

Concurrency

Objects naturally live in a shared state environment

Multiple Objects in a shared state environment handle concurrency poorly

Enter Functional Programming!

Why Functional?!

Mathematical approach to solving problems

More simple and less abstract

Easy Reuse, Test and handle Concurrency

FP Principles

Purity

Immutability

High Order

Composition

Currying

Purity

Pure FunctionPure functions act on their parameters

Are not efficient if not returning anything

Will always produce the same output for the given parameters

Have NO side affects

Pure Function

ImPure Functions

Immutability

There are no “Variables” in Functional programming - all “variables” should

be considered as constants

When Do we mutate variables?

Short living “local” variables

Loop flow structures

State objects

Javascript is Not a pure functional programming

language - it allows for mutable

structures. We need to “tweak” it…

Simple Immutable tweaks

arr.push(val) —> [].concat(arr, val)

obj[‘key’] = val —> Object.assign({key: val}, obj)

ImmutableObj = Object.freeze(obj) ** Object assign is limited to shallow cloning and may produce strange outcomes. see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create

For “real” projects use an immutable library such as ImmutableJS or

seamless-immutable

Higher Order Functions

In Functional Programming, a function is a first-class citizen of the

language. In other words:A function is just another value

Higher Order Functions

Take functions as parameters

Return functions

Both

A closure is a function’s scope that’s kept alive by a reference to that

function.

Function Composition

newFunc = f(g(x))

Application of one function to the result of another to produce a third

function

A Problem

Write a composed function that does text Capitalization:

“the FORCE is strong with YOU” —> “The Force Is Strong With You”

Composition h = f(g(x))

Enhanced Capitalize Problem

I want to ignore a list of words:a, is, for, etc. so: “the FORCE is strong with YOU” —> “The Fore is Strong With You”

This sucks…

Currying

A Curried Function is a function that only takes a single parameter at a

time.

Much better :)

Common Functional Functions

Map

Creates a new array with the results of calling a provided function on every element in the array

Filter

Creates a new array with all elements that pass the test implemented by the provided function

Reduce

Applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value

SummaryUse “Pure” functions to create predictable & stable code

Use Immutable structs to avoid side effects

Compose complex functionality from simple building blocks

Use currying for simple composition

Avoid “boilerplate” code and loops by using functional functions