On Github CWDG / Functional-JavaScript-Talk
They are everywhere
true false undefined null "Hello World" 1 new Integer(1) new Object() {} new Array() [] function (x) { return x; }
undefined == null // → true undefined === null // → false 0 == false // → true 1 == true // → true '0' == false // → true '1' == true // → true '' == false // → true 'hello world' == false // → false 'hello world' == true // → false
if conditions are like != false
C-style syntax
if (x == y) { // do something } else { // do something else }
var returnValue; switch(val) { case 2: returnValue = 'A' break; case 6: returnValue = 'B' break; case 9: returnValue = 'B' break; default: returnValue = null; } return returnValue;
beware of breaks
while (condition) { // do something that eventually // manipulates condition }
for (var i = 0; i < arr.length; i++) { // Do something arr.length times }
for (var index in obj) { // what do you think index is? }
var x; x // → undefined var y = 10;
typeof x // → "undefined" x // throws a reference error var x;
with functions
- Ryan McGowan
// Assignment var coolFunc1 = function () { return "Sooo coool"; }; // Pass in as argument(s) var coolFunc2 = function (func) { // do some stuff and then finally … func(); };
var foo = function (bar) { return function (baz) { return function (derp) { return bar.callback(baz, derp, bar.value); }: }; }; var data = { callback: function (x, y, z) { return [x, y, z].join(' '); }, value: 'func?!' }; foo(data)("What")("the"); // → "What the func?!"
(map (parital + 1) [1 2 3])
_.map([1, 2, 3], function (num) { return num + 1; });
(reduce * [2 3 4])
_.reduce([2, 3, 4], function (product, num) { return product * num; });
(->> [1 2 3] (map (partial + 1)) (reduce *))
_.chain([2, 3, 4]).map(function (num) { return num + 1; }).reduce(function (product, num) { return product * num; }).value();
Let's make lambda.js
(def plus-six (partial + 6)) (plus-six 10) ; → 16
(defn all-less-than-ten-lame [coll] (every? (fn [item] (< item 10)) coll)) ; Cooler (def all-less-than-ten (partial every? (partial > 10))) (all-less-than-ten [1 2 3 4 5 8 2 2 7]) ; → true (all-less-than-ten [1 2 3 4 5 8 11]) ; → false
(defn two-numbers [x] (partial + (* 10 x))) ((two-numbers 6) 1) ; → 61 ((two-numbers 10) ((two-numbers 1) 6) 4) ; → 120
Use an Object, true, false and function(s)
if (a == 'blarg') { console.log('Hello'); } else { console.log('Goodbye'); }
console.log({ true: 'Hello', false: 'Goodbye' }[a == 'blarg']);
var x, y; if (a == 'blarg') { x = 2; y = [z * 5]; } else { y = 3; x = [z / 2]; }
{ true: function () { x = 2; y = [z * 5]; }, false: function () { y = 3; x = [z / 2]; } }[a == 'blarg']();
What do you need?
var returnValue; switch(val) { case 2: returnValue = 'A' break; case 6: returnValue = 'B' break; case 9: returnValue = 'B' break; default: returnValue = null; } return returnValue;
{2: 'A', 6: 'B', 9: 'C'}[val]
Well, almost...
var y = function () { ... }; // What's the difference? function y() { ... }
foo(); // TypeError "foo is not a function" bar(); // valid baz(); // TypeError "baz is not a function" spam(); // ReferenceError "spam is not defined" var foo = function () {}; // anonymous function expression ('foo' gets hoisted) function bar() {}; // function declaration ('bar' and the function body get hoisted) var baz = function spam() {}; // named function expression (only 'baz' gets hoisted) foo(); // valid bar(); // valid baz(); // valid spam(); // ReferenceError "spam is not defined"
Twitter: Ryan_VM
Github: RyanMcG