Intro to functional programming
-
Upload
assaf-gannon -
Category
Software
-
view
184 -
download
1
Transcript of Intro to functional programming
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
Why Functional?!
Mathematical approach to solving problems
More simple and less abstract
Easy Reuse, Test and handle Concurrency
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
There are no “Variables” in Functional programming - all “variables” should
be considered as constants
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
In Functional Programming, a function is a first-class citizen of the
language. In other words:A function is just another value
A Problem
Write a composed function that does text Capitalization:
“the FORCE is strong with YOU” —> “The Force Is Strong With You”
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”
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