This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const doubleMap = numbers => numbers.map(n => n * 2); | |
console.log(doubleMap([2, 3, 4])); // [4, 6, 8] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const doubleMap = numbers => { | |
const doubled = []; | |
for (let i = 0; i < numbers.length; i++) { | |
doubled.push(numbers[i] * 2); | |
} | |
return doubled; | |
}; | |
console.log(doubleMap([2, 3, 4])); // [4, 6, 8] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const a = Object.freeze({ | |
foo: { greeting: 'Hello' }, | |
bar: 'world', | |
baz: '!' | |
}); | |
a.foo.greeting = 'Goodbye'; | |
console.log(`${ a.foo.greeting }, ${ a.bar }${a.baz}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const a = Object.freeze({ | |
foo: 'Hello', | |
bar: 'world', | |
baz: '!' | |
}); | |
a.foo = 'Goodbye'; | |
// Error: Cannot assign to read only property 'foo' of object Object |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const x = { | |
val: 2 | |
}; | |
const x1 = x => Object.assign({}, x, { val: x.val + 1}); | |
const x2 = x => Object.assign({}, x, { val: x.val * 2}); | |
console.log(x1(x2(x)).val); // 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// With shared state, the order in which function calls are made | |
// changes the result of the function calls. | |
const x = { | |
val: 2 | |
}; | |
const x1 = () => x.val += 1; | |
const x2 = () => x.val *= 2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const doSomething = (err) => new Promise((resolve, reject) => { | |
if (err) return reject(err); | |
setTimeout(() => { | |
resolve(42); | |
}, 1000); | |
}); | |
const log = value => console.log(value); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Observable } from 'rxjs/Observable'; | |
// then patch import only needed operators: | |
import 'rxjs/add/operator/map'; | |
import 'rxjs/add/observable/from'; | |
const foo = Observable.from([1, 2, 3]); | |
foo.map(x => x * 2).subscribe(n => console.log(n)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const check = fn => (params = []) => { | |
const { required } = fn; | |
const missing = required.filter(param => !(param in params)); | |
if (missing.length) { | |
throw new Error(`${ fn.name }() Missing required parameter(s): | |
${ missing.join(', ') }`); | |
} | |
return fn(params); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fn = (fn, {required = []}) => (params = {}) => { | |
const missing = required.filter(param => !(param in params)); | |
if (missing.length) { | |
throw new Error(`${ fn.name }() Missing required parameter(s): | |
${ missing.join(', ') }`); | |
} | |
return fn(params); | |
}; |