Skip to content

Instantly share code, notes, and snippets.

View zhangchiqing's full-sized avatar

Leo Zhang zhangchiqing

  • Flow Foundation
  • Vancouver, Canada
View GitHub Profile
// Imperative
function foo(bar) {
if (!bar) {
return ;
}
return bar + 1;
}
foo(null);
@zhangchiqing
zhangchiqing / leftPad.js
Created March 30, 2016 01:16
leftPad.js
'use strict';
var R = require('ramda');
// leftPad :: Number -> String -> String
// > leftPad(3, '0', '7')
// '007'
// > leftPad(3, '0', '2016')
// '2016'
var leftPad = R.curry(function(num, fill, str) {
@zhangchiqing
zhangchiqing / promiseShouldFail.js
Last active July 13, 2016 18:15
promiseShouldFail.js
var promiseShouldFail = function(promise) {
return new Promise(function(resolve, reject) {
promise.then(reject).catch(resolve);
});
};
describe('This rejected promise', function() {
it('should fail', function() {
return promiseShouldFail(Promise.reject(10));
});
@zhangchiqing
zhangchiqing / toPromiseMaybe.js
Created November 1, 2016 20:51
Maybe Promise a -> Promise Maybe a
var P = require('bluebird-promisell');
var S = require('sanctuary').create({ checkTypes: true, env: require('sanctury').env });
// toPromiseMaybe :: Maybe Promise a -> Promise Maybe a
var toPromiseMaybe = S.maybe(P.purep(S.Nothing()), P.liftp1(S.Just));
@zhangchiqing
zhangchiqing / getTweetsByUserId.js
Created November 13, 2016 16:32
An example of async programming with bluebird-promisell - Get Tweets by UserId
var P = require('bluebird-promisell');
var R = require('ramda');
var S = require('./service');
// S.getTweets :: UserId -> Promise [Tweet]
// S.getUserName :: UserId -> Promise String
// S.getUserPhoto :: UserId -> Promise String
// makeUser :: (UserId, String, String) -> User
var makeUser = function(id, name, photo) {
@zhangchiqing
zhangchiqing / Main.purs
Last active July 17, 2017 17:41
Purescript Async
module Main where
import Prelude
import Control.Monad.Aff
import Control.Parallel (sequential, parallel)
import Control.Monad.Eff.Class (liftEff)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
type A = String
@zhangchiqing
zhangchiqing / validation.js
Last active November 24, 2016 18:05
Validation with liftp and toPromise
// I've been writing validation utilities for many times,
// never satisfied until I realize that the combination of `liftp` and `toPromise`
// is just perfect.
// It could work with any number of arguments. Leaving
// the choices to you of what error to return and how to check if an argument is valid.
var P = require('bluebird-promisell');
var R = require('ramda');
// Number -> Number -> Promise Number
@zhangchiqing
zhangchiqing / Main.purs
Created December 25, 2016 04:18
StateMonad vs Folding
module Main where
import Prelude (Unit, negate, (==), (>>>), (-), (>), (+), ($), bind)
import Data.Foldable (traverse_, foldl)
import Data.String (toCharArray)
import Control.Monad.State (State, execState, modify)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, logShow)
match :: Int -> Char -> Int
@zhangchiqing
zhangchiqing / benchmarkcommand.sh
Created February 28, 2017 19:27
benchmark a command
time seq 10 | xargs -Iz echo "hello"
// [a] -> [[a]] -> [[a]]
var iter = function(arr, parents) {
if (R.isEmpty(arr)) {
return parents;
}
return R.addIndex(R.chain)(function(a, index) {
return iter(
R.remove(index, 1, arr),
R.map(R.append(a), parents));