Functional Programming

Part I

 Free Resources

News

Javascript Overview

  • angular - JS MVC / MVVM Framework
  • react - View in MVC
  • bower - nuget for client-side modules
  • grunt/gulp - task runners, build
  • node - JS server-side uses Chrome V8 JS Engine
  • npm - nuget for node modules

Future Topics

  • Schedule
    • March 19 - ESLint
    • April 16 -
    • May 7 -
    • June 25 -

 

  • Topics
    • Docker
    • Hapi.js
    • TDD
    • Go
    • Erlang/Elixir
    • D3
    • Angular

Programming Paradigms

  • Procedural
  • Object Oriented Programming
  • Functional Programming

Procedural

  • Imperative
  • Top Down / Bottom Up
  • Procedures / Data
  • Modules

OOP

  • Classes
  • Inheritance
  • Encapsulation
  • Polymorphism
  • Object Instance (State)
  • Messages

Functional

  • Immutable Data
  • Stateless Functions
  • Pure Functions
  • First Class Functions
  • High Order Functions

Functional Languages

  • Lisp (1958)
  • Erlang (1986)
  • JavaScript (1996)
  • Haskell (2000) - Pure
  • R (2000)
  • Microsoft F# (2005)
  • Scala (2006) JVM
  • D (2007)
  • C# (2007) Lambda, LINQ
  • Elm (2012) Pure, Erlang
  • Java8 (2014)

JavaScript Language

  • Procedural
  • Object Oriented
    • Prototypical Inheritance
  • Functional

Prototypical Inheritance

var Person = function (firstname, lastname) {
    this.firstname = firstname
    this.lastname = lastname
    this.printName = function (){
        console.log('My name is ', this.firstname, this.lastname);
    }
};

Person.prototype.myName = function() {
    console.log('My full name is ', this.firstname, this.lastname);
};

var brian = new Person('Brian', 'Chirgwin');
var john = new Person('John', 'Doe');

brian.printName(); // My name is Brian Chirgwin
john.printName();  // My name is John Doe
brian.myName();    // My full name is Brian Chirgwin

Prototypical Inheritance


brian.printName = function(){
    console.log('call me Brian');
};

brian.myName = function(){
    console.log('Mr. ', this.lastname);
};

brian.printName();  // call me Brian
brian.myName();  // Mr. Chirgwin (object version)
john.myName();   // My full name is John Doe (Protoype)

JavaScript

  • Immutable.js
  • Ramda.js
  • bacon.js
  • rx.js
  • lodash
  • underscore
  • cycle.js
  • Kefir.js
  • fn.js

Functional

  • lamda 
    • function expression
  • currying
    • partial functions
  • recursion (instead of loop iteration)
    • tail recursion not in JS
  • composable
  • generic
// Procedural
var getPopulation(){
    
    var zips = loadData();
    var count = zips.length;
    var population;

    for(var i = 0; i < count; i++){
        population += zips[i].population; 
    }
    return population;
} 

console.log(getPopulation);

forEach

  • iterate over a collection visiting every element

filter

  • iterate over a collection returning a 0 to length elements that match criteria

[1,2,3,4,5]

[1,3,5]

Map

  • iterate over a collection and return a new collection
  • ability to include or omit data from collection

[1,2,3,4,5]

[2,4,6,8,10]

Reduce

  • Iterate a collection and return a value

[1,2,3,4,5]

[15]

groupBy

  • Creates an object composed of keys with arrays of the collection values.

[1, 1, 2, 3]

{ 1: [1,1]

2: [2],

3: [3] }

memoize

  • wrap a function so results are stored in a cache

currying

  • Calling a function with fewer arguments than it expects and returning a function that takes the remaining arguments
  • Ability to pre-load a function with an argument or two in order two receive a new function that remembers those arguments

Erlang Factorial Function

 factorial(0) ->
   1;
 factorial(X) when X > 0 ->
   X * factorial(X-1).

Why

  • Simplify your problem (What, not how)
  • Readable code
  • Reusability
  • Testability
  • Microservice Architecture
    • function self contained - no state

Resources

Made with Slides.com