Skip to content

Instantly share code, notes, and snippets.

@DadgadCafe
DadgadCafe / clojurescript-unraveled.md
Last active April 20, 2017 08:15
translation draft.

1. 关于此书

本书涵盖了 ClojureScript 编程语言,可作为其开发工具的详细指南,并提供了一系列适用于 ClojureScript 日常编程的主题文章。

这不是一本入门编程的书,它假定读者至少有一种语言的编程经验。不过,并不要求有 Clojure 或者函数式编程经验。当谈到一些不是人尽皆知的 ClojureScript 理论基础时,我们会尝试给出参考材料的链接。

ClojureScript 文档虽很好但太过松散,因此我们想写一个参考资料的纲要,并提供全面的示例,作为 ClojureScript 的入门书和实用指南。本文档作为语言特性的参考和实战的手册,会随着 ClojureScript 语言更新。

本书将非常适合你,如果:

@DadgadCafe
DadgadCafe / for-clojure.cljs
Created April 10, 2017 14:39
notes of problem on 4clojure
;;;;;;;;;; Last Element
;; (= (__ [1 2 3 4 5]) 5)
;; (= (__ '(5 4 3)) 3)
;; (= (__ ["b" "c" "d"]) "d")
#(nth % (- (count %) 1))
#(nth % (dec (count %)))
;;;;;;;;;; Penultimate Element
;; (= (__ (list 1 2 3 4 5)) 4)
@DadgadCafe
DadgadCafe / koa2middleware.js
Created April 2, 2017 08:50
chaining async flow, like in koa2.
// must return or await next() in koa2, otherwise promise chain will be terminated.
const delay = () => new Promise(resolve => {
console.log('delay...')
setTimeout(() => {
resolve()
}, 1000)
})
@DadgadCafe
DadgadCafe / with.js
Created March 30, 2017 11:11
notes about with statement.
const obj = {
x: 10,
foo: function foo () {
function bar () {
console.log(x) // undefined, scope foo
console.log(y) // 30, scope bar
console.log(this.x) // 20
console.log(this.y) // undefined
}
@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 = {
@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 / 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 / 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 / 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 / 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([