100 top projects
@luftyoav
Yoav Luft
?
100 top projects
Analyzing the Code
=
JavaScript Becomes Functional Declarative

Yoav Luft

Web Dev - Games - Embedded



Why should we care about JavaScript?
What "adopting functional programming" even means?
The Quest for Data
What’s next?



Imperative
Functions are 1st class citizens
Universally typed
Prototypical Inheritance
var object =
  { name: "Object1",
    action: function() { },
    data: [1, 2, 3]
  }
JSON.stringify(object) ==
  "{\"name\":\"Object1\",\"data\":[1,2,3]}"function Dog() {
  this.voice = "Woof!"
  this.makeSound = function() {
    return this.voice
  }
}
var dog = new Dog
var cat = {voice: "Prrrr",
           makeSound: dog.makeSound}
cat.makeSound() == "Prrrr"




Javascript has:
anonymous functions
closures
uses continuation passing
structured modeling of async (almost monads!)

anonymous functions shorthand (2015)
destructuring assignments (2015)
generator functions (2015)
tail-call optimization (pending)
pipe-operator (pending)
partial application syntax (pending)
pattern matching (pending)

Adopting functional programming
Functional programming was not popular, or formally accepted in Javascript
It is now becoming more popular, or being formally accepted
Computation as the evaluation of mathematical functions
Avoids changing-state and mutable-data
Declarative programming paradigm
Mathematical
function append(array, x) {
  return array.concat(x)
}
var a1 = [1, 2]
var a2 = append(a1, 3)
// a1 == [1, 2]; a2 == [1, 2, 3]var array = [1, 2]
function append(x) {
  array.push(x)
}
append(3) // undefined -> no return
          // array == [1, 2, 3]
a style of building the structure and elements of computer programs—that expresses the logic of a computation without describing its control flow.
[…] without describing its control flow.
while (condition) {
  doAction()
}
do {
  action()
} while (condition)for (var i = 0; i < size; i++) {
  use(i)
}
for (var property in object) {
  use(property)
}
for (var index of array) {
  use(index)
}if (condition) {
  doSomething()
} else {
  doSomethingElse()
}
var a = condition ? val1 : val2switch (response) {
  case "yes":
    return true
  case "no":
    return false
  default:
    return undefined
}map, filter, forEach and reduce
| Imperative | Declarative | 
|---|---|
|  |  | 
|  |  | 



Projects which are:
Open source
Have a lot of contributors
Have been around for a while
 
 





Project = A repository on Github
Sample = Data on patterns from a project’s snapshot
Sample Year = A sample from the specified year
for loop, for..in loops, for..of loops
for (var i = 0; i < size; i++) {...}
for (var i in object) {...}
for (var i of array) {...}while and do-while loops
while (cond) {...}forEach calls
map calls
filter calls
reduce calls






More Declarative Iteration!
but not that much…








Elm

ClojureScript

PureScript
Reason

WebAssembly
Look at a larger sample
Examine more structures, e.g. assignments
Look at the use of higher-order functions
Examine results into more detail.
Examine changes of code over time
Look for projects migrating from Javascript to functional languages
@luftyoav
