Skip to content

Instantly share code, notes, and snippets.

View shuding's full-sized avatar
🌿

Shu Ding shuding

🌿
View GitHub Profile
@shuding
shuding / promise.js
Last active December 10, 2015 08:30
Promise
function a (callback) {
setTimeout(function () {
console.log('This is a');
callback && callback(null);
}, 100);
}
function b (callback) {
setTimeout(function () {
console.log('This is b');
# encoding=utf-8
import re
s = """It was a very cold evening, an old man was waiting for a ride across the river. He saw several horsemen pass by but he didn’t ask for any help. The wait seemed __1__(end).Then came another rider,the old man __2__(catch) his eye and said, “Sir, would you mind doing me a favor?”
Stopping his horse,he replied, “Of course.” Almost __3__(freeze), the old man could not get __4__ the ground. The horseman helped him onto his horse. He took the old man not just across the river,__5__ to his home.
“Sir, you didn’t even ask the other riders for help, why? What __6__ I had said ‘no’ and left you there?” the horseman asked.
The old man looked at him straight in the eyes and said, “I looked into their eyes, I found they didn’t care,__7__ told me it would be useless, but when I looked into __8__,I saw kindness.”
These words touched the rider deeply. “Thank you for __9__ you’ve said, I hope I will never be too busy to help others.” with that, Thomas Jefferson, the __10__(three) presid
@shuding
shuding / range.js
Created January 22, 2016 07:37
An one-line range function with JavaScript ES6, like Python
const range = (s, t = 0, a = 1) => [...Array(Math.abs(s - t))].map((x, i) => Math.min(s, t) + i * a).filter(x => x < Math.max(s, t));
console.log(range(5));
console.log(range(2, 5));
console.log(range(0, 5, 2));
// node.js: please run with `node --harmony_default_parameters --harmony range.js`
var y = count();
y.next();
function *count() {
yield setTimeout(() => { console.log(1); y.next(); }, 1000);
yield setTimeout(() => { console.log(2); y.next(); }, 1000);
yield setTimeout(() => { console.log(3); y.next(); }, 1000);
yield setTimeout(() => { console.log(4); y.next(); }, 1000);
}
@shuding
shuding / delay.js
Created January 22, 2016 09:07
Tricky delay function with ES6 yield, inspired by co
function run(x, cb) {
var f;
if (this.constructor.constructor == (function*() {}).constructor) {
f = this;
} else {
f = x();
}
var w = f.next();
if (!w.done) {
setTimeout(() => {
@shuding
shuding / delay.js
Last active March 1, 2016 05:27
Isn't this beautiful?
var $=(f,c,t,r)=>(r=(t=t||f()).next()).done?c&&c(r.value):setTimeout(()=>$(f,c,t),r.value);
$(function *() {
console.log(1);
yield 1000;
console.log(2);
yield 1000;
console.log(3);
yield 1000;
return 4;
@shuding
shuding / scope.js
Created January 22, 2016 09:51
setTimeout with scope
for (var i = 0; i < 3; ++i) { setTimeout(function () { console.log(i); }, 1000 * i); }
for (var i = 0; i < 3; ++i) { setTimeout(function (i) { console.log(i); }, 1000 * i, i); }
@shuding
shuding / demo.jsx
Created January 28, 2016 04:03
JSX Multiple Elements Demo
let elements = [
<div/>,
<div/>
];
return React.createElement('div', null, ...elements);
// !!! using `<div>{elements}</div>` here will got a `unique key in array` error
@shuding
shuding / observeStore.js
Created January 28, 2016 06:01 — forked from gaearon/observeStore.js
Wait for some condition to become true on a Flux store, useful for react-router async transition hooks
// Usage example:
//
// willTransitionTo(transition, params, query, callback) {
// observeStore(DraftStore, s => s.isLoaded()).then(() => {
// if (DraftStore.isMissingTitle()) {
// transition.redirect('composeDraft', params);
// }
// }).finally(callback);
// }
@shuding
shuding / innsbruck.md
Last active August 21, 2017 12:21
innsbruck

Updated on May 4th 2016

inns is a collection of SMART components

inns-blog, inns-port, ...

a SMART component also passing STYLE as its state to all the cascading children components

.. and gets a couple of scores (readibility, color, ...)