Skip to content

Instantly share code, notes, and snippets.

@DadgadCafe
DadgadCafe / naivePromise.js
Last active March 21, 2017 06:31
The simplest promise implementation.
'use strict'
function promisify (fakeAjax){
let content = null
let func = null
return function (url) {
fakeAjax(url, (err, data) => {
if (err) {
throw Error('unhandled error')
}
@DadgadCafe
DadgadCafe / editDistance.js
Created March 21, 2017 08:00
edit distance: counting the minimum number of operations required to transform one string into the other.
'use strict'
/**
* note: substitute costs 2
*
* if either string is empty => length of the other
* if the last chars of both match =>
* recursively call edit without adding distance
* if not =>
* Math.min of three ways
@DadgadCafe
DadgadCafe / cont.scm
Created March 21, 2017 13:08
continuation demo.
(define call/cc call-with-current-continuation)
;;;;;;;;;;;;;;
(call/cc (lambda (cont) 2)) ; 2, cont do nothing
(call/cc (lambda (cont) (cont 2))) ; 2, cont do nothing
(* 3 (call/cc (lambda (cont)
(+ 1 2)))) ; 9, cont is not used
@DadgadCafe
DadgadCafe / diff.js
Last active March 22, 2017 07:02
diff & patch the array, using DP.
'use strict'
module.exports = diff
const MATCH = 0
const REMOVE = 1
const INSERT = 2
const REPLACE = 3
function diff (fromArr, toArr, diffKey) {
const len1 = fromArr.length
@DadgadCafe
DadgadCafe / cancelPromise.js
Last active March 23, 2017 04:49
cancel the ajax call after settled time.
const fakeAjax = (time, cb) => {
setTimeout(() => {cb('data...')}, time)
}
const noopPromise = new Promise(() => {})
// cancel promise when timeout reached, using Promise.race
const timer = (fn, time) =>
Promise
.race([
@DadgadCafe
DadgadCafe / rangeProxy.js
Created March 24, 2017 03:38
generator-like range, using Proxy.
function fakeGen (fromV = 0, toV, step = 1) {
// only one arg
if (toV == undefined) {
toV = fromV
fromV = 0
}
// init status
const status = {
done: false,
@DadgadCafe
DadgadCafe / safeObjProxy.js
Created March 24, 2017 05:29
it works like obj?.prop?.name to prevent reference error, using Proxy.
'use strict'
const isObject = obj => typeof obj === 'object'
const hasKey = (obj, key) => key in obj
const Undefined = new Proxy({},{
get (target, key, receiver) {
// always return Undefined when accessing Undefined.whatever
return receiver
}
@DadgadCafe
DadgadCafe / eventPriority.js
Last active March 24, 2017 08:37
detailed demo of event priority.
(function () {
setImmediate(function () {
console.log(6) // run macro-task
})
setTimeout(function () {
console.log(8) // 4ms delay
}, 0)
Promise
@DadgadCafe
DadgadCafe / yin-yang.scm
Created March 26, 2017 03:32
yin-yang problem.
;; detailed continuation usage, see previous gist: https://gist.github.com/DadgadCafe/07b0b50bf9537522fed09b3dc8df2784
(define call/cc call-with-current-continuation)
(let* ((yin
((lambda (cc) (display #\@) cc)
(call/cc (lambda (c) c))))
(yang
((lambda (cc) (display #\*) cc)
(call/cc (lambda (c) c)))))
@DadgadCafe
DadgadCafe / privateProps.js
Last active March 29, 2017 15:14
implement private Object props, using Proxy.
function proxied (obj) {
const o = Object.assign({}, obj)
function filterPrivate (key) {
if (key[0] === '_') {
throw Error('cannot access private key: ${key}')
}
}
const handler = {