JaxNode July 2017
Douglas Crockford
"JavaScript is the only programming language that people try to program in before actually learning the language"
Brendan Eich
Hired by Netscape
Wanted to do Scheme
Had to look like Java
We got JavaScript
function add(a, b) {
return a + b;
}
const c1 = add(12, 14); // 26
const c2 = add(100, -1); // 99
const c3 = add(34, 17); // 51
let state = 0;
function addToState(value) {
state += value;
return state;
}
let c1 = addToState(3); // 3
let c2 = addToState(10); // 13
let c3 = addToState(3); // 16
const counter = function() {
var privateCounter = 0;
function changeBy(val) {
privateCounter += val;
}
return {
increment: function() {
changeBy(1);
},
decrement: function() {
changeBy(-1);
},
value: function() {
return privateCounter;
}
};
};
const myCounter = counter();
myCounter.increment(); // Changes privateCounter to 1
myCounter.value(); // returns 1
// C#
public class Animal {
public string Name { get; set; }
public int Limbs { get; set; }
public Animal(name, limbs) {
this.Name = name;
this.Limbs = limbs;
}
public void printAnimal() {
if (this.Limbs == 0) {
Console.PrintLine(@"You have a " +
this.Name + " with no limbs.");
} else if (this.Limbs == 1) {
Console.PrintLine(@"You have a " +
this.Name + " with " +
this.Limbs.ToString() + " limb.");
} else {
Console.PrintLine(@"You have a " +
this.Name + " with " +
this.Limbs.ToString() + " limbs.");
}
}
}
class Animal {
constructor(name, limbs) {
this.Name = name;
this.Limbs = limbs;
}
printAnimal() {
if (this.Limbs === 0) {
console.log(`You have a ${this.Name} with no limbs.`);
} else if (this.Limbs === 1) {
console.log(`You have a ${this.Name} with ${this.Limbs} limb.`);
} else {
console.log(`You have a ${this.Name} with ${this.Limbs} limbs.`);
}
}
}
const Cat = new Animal('Gato', 4);
const Dog = new Animal('Perro', 3);
Cat.printAnimal(); // You have a Gato with 4 limbs.
Dog.printAnimal(); // You have a Perro with 3 limbs.
$('#myButton').on('click', Cat.printAnimal()); // Doesn't work
$('#myButton').on('click', Cat.printAnimal.bind(this)); // Will work
$('#myButton').on('click', _ => Cat.printAnimal()); // Will work
const MakeAnimal = (name, limbs) => {
const Name = name;
const Limbs = limbs;
return {
printAnimal: () => {
if (Limbs === 0) {
console.log(`You have a ${Name} with no limbs.`);
} else if (Limbs === 1) {
console.log(`You have a ${Name} with ${Limbs} limb.`);
} else {
console.log(`You have a ${Name} with ${Limbs} limbs.`);
}
}
}
};
const person = {
sayName: function () {
console.log(`my name is ${this.myName}`)
}
}
const bar = Object.create(person);
bar.myName = 'Dinesh';
bar.sayName(); // my name is Dinesh
// This property is overwritable
bar.myName = 'Gilfoyle';
bar.sayName(); // my name is Gilfoyle
const baz = Object.create(person,
{ myName: {
get: function() { return 'Erlich'; }
}
});
baz.sayName(); // my name is Erlich
baz.myName = 'Richard'; // Will throw error! This property is not overwritable.
const sounder = (state) => ({
makeSound: () => console.log(state.sound)
});
const namer = (state) => ({
sayName: () => console.log(`My name is ${state.name}.`)
})
const state = {
name: 'Bill',
sound: 'Ack Ack!'
};
const cat = Object.assign({}, sounder(state), namer(state));
cat.makeSound(); // Ack Ack!
cat.sayName(); // My name is Bill.
const greeter = function(greeting) {
return function(name) {
console.log(`${greeting} there ${name}!`);
}
};
const greet = greeter('Hi');
greet('David'); // Hi there David!
greet('Andy'); // Hi there Andy!
// or
greeter('Hi')('Frank'); // Hi there Frank!
function filterGT2(arr) {
let newArray = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] > 2) {
newArray.push(arr[i]);
}
}
return newArray;
}
console.log(filterGT2([-1, 0, 1, 2, 4, 5, 7]));
// returns [4, 5, 7];
console.log([-1, 0, 1, 2, 4, 5, 7].filter(i => i > 2));
// returns [4, 5, 7]
const multipleBy2 = (item) => item * 2;
[3, 4, 8, 5].map(multipleBy2); // returns [6, 8, 16, 10]
const arr = [ 6, 8, 16, 10 ];
const sum = arr.reduce((a, i) => a += i, 0); // returns 40
Douglas Crockford
Once someone learns what a Monad is, it becomes physically impossible for them to describe what a Monad is to someone else.
Crockford on Monads
const fs = require('fs');
fs.stat('.', function cb(err, result) {
if (err) {
console.error(err);
}
console.log(result);
});
fetch('https://www.jaxnode.com/v1/api/meeting')
.then(response => {
return response.json();
})
.then(json => {
console.log(json);
})
.catch(err => {
console.error(err);
});
// Copyright Mozilla.org
function myAsyncFunction(url) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.onload = () => resolve(xhr.responseText);
xhr.onerror = () => reject(xhr.statusText);
xhr.send();
});
});
const util = require('util');
const fs = require('fs');
const stat = util.promisify(fs.stat);
stat('.')
.then((stats) => {
console.log(stats);
})
.catch((error) => {
console.error(error);
});
const util = require('util');
const fs = require('fs');
const stat = util.promisify(fs.stat);
async function callStat() {
const stats = await stat('.');
console.log(`This directory is owned by ${stats.uid}`);
}
"50% of the bugs that people run into today, coding with C# in our platform, and the same is true of Java for that matter, are probably null reference exceptions. If we had had a stronger type system that would allow you to say that ‘this parameter may never be null, and you compiler please check that at every call, by doing static analysis of the code’. Then we could have stamped out classes of bugs."
// Copyright 2017 Michael K. Snead
// https://medium.com/@aikeru/can-we-please-promote-the-bind-operator-939845ca6ae5
function pipeline(fn, ...args) {
return fn && fn(this, ...args)
}
//Used like so
const addAll = (...numbers) => numbers.reduce((a, b) => a + b);
5 :: pipeline(addAll, 2);
// copyright 2016 https://github.com/gilbert/es-pipeline-operator
function doubleSay (str) {
return str + ", " + str;
}
function capitalize (str) {
return str[0].toUpperCase() + str.substring(1);
}
function exclaim (str) {
return str + '!';
}
let result = exclaim(capitalize(doubleSay("hello")));
console.log(result); //=> "Hello, hello!"
let result2 = "hello"
|> doubleSay
|> capitalize
|> exclaim;
console.log(result2); //=> "Hello, hello!"
result2
|> console.log; // => "Hello hello!"
https://github.com/jaxnode/functionaljavascript
https://www.youtube.com/watch?v=e-5obm1G_FY
https://www.youtube.com/watch?v=Wo0qiGPSV-s
@mpjme FunFunFunction
Douglas Crockford