Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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')
}