- expression-oriented programming one of the great advances of FP
- expressions plug together like legos, making more malleable programming experience in-the-small
Write in an expression-oriented style, scoping variables as locally as possible:
// ==UserScript== | |
// @name Medium: remove location hash | |
// @namespace http://efcl.info/ | |
// @description Remove location hash from medium | |
// @include https://medium.com/*#* | |
// @version 1 | |
// @grant none | |
// ==/UserScript== | |
function removeLocationHash(){ |
// class | |
class ClassCar { | |
drive () { | |
console.log('Vroom!'); | |
} | |
} | |
const car1 = new ClassCar(); | |
car1.drive(); |
Now using node v4.7.2 (npm v2.15.11) | |
// v8 4.5.103.43 (Node.js 4.7.2) | |
forVar_______: 2ms | |
forLet_______: 13ms | |
forOfVar_____: 66ms | |
forOfLetConst: 64ms | |
forEachVar___: 15ms | |
forEachLet___: 21ms |
console.log(1); | |
(_ => console.log(2))(); | |
eval('console.log(3);'); | |
console.log.call(null, 4); | |
console.log.apply(null, [5]); | |
new Function('console.log(6)')(); | |
Reflect.apply(console.log, null, [7]) | |
Reflect.construct(function(){console.log(8)}, []); | |
Function.prototype.apply.call(console.log, null, [9]); | |
Function.prototype.call.call(console.log, null, 10); |
// Add flag 'g' if it isn’t there, yet | |
// Solution 1 | |
let cloneFlags = regExp.flags; | |
if (!cloneFlags.includes('g')) { | |
cloneFlags += 'g'; | |
} | |
// Solution 2 | |
const cloneFlags = regExp.flags.includes('g') |
// From callbacks to Promises to async functions | |
function callbackFunc(x, callback) { | |
f1(x, (err1, result1) => { | |
if (err1) { | |
console.error(err1); | |
callback(err1); | |
return; | |
} | |
f2(result1, (err2, result2) => { |
const assert = require('assert'); | |
//========== Helper functions | |
/** | |
* Resolves after `ms` milliseconds | |
*/ | |
function delay(ms) { | |
return new Promise((resolve, _reject) => { | |
setTimeout(resolve, ms); |
/** | |
* Executes an XPath expression returning an iterable node list. | |
* @param {string} expression XPath expression. | |
* @param {Node} node Optional context node for query. Default is document element. | |
* @param {XPathNSResolver | ((prefix: string) => string | null) | null} nsResolver Optional namespace resolver function. | |
* @returns {Iterator<Node>} Result nodes iterator. | |
*/ | |
function* xpath(expression, node = document.documentElement, nsResolver = null) | |
{ | |
const xPathResult = document.evaluate( |
/g |
/y |
/yg |
|
---|---|---|---|
.exec() |
at .lI or later |
at .lI |
same as /y |
.test() |
at .lI or later |
at .lI |
same as /y |
.replace() |
ignores & resets .lI |
at .lI |
/g w/o gaps |
.replaceAll() |
ignores .lI |
TypeError |
/g w/o gaps |
.search() |
no effect | no effect | no effect |
.match() |
Array of group 0 captures | like .exec() |
/g w/o gaps |