Skip to content

Instantly share code, notes, and snippets.

Leo Zhang zhangchiqing

Block or report user

Report or block zhangchiqing

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@zhangchiqing
zhangchiqing / kubectl.md
Created Jan 31, 2019
Kubectl cheatsheet
View kubectl.md
View binarysearch.js
var bsearch = function(f, a, b, z) {
if (a + 1 === b) {
return a;
}
var m = Math.floor(( a + b ) / 2);
if (f(m) <= z) {
return bsearch(f, m, b, z);
} else {
return bsearch(f, a, m, z);
}
View premutationsN.js
// [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));
View benchmarkcommand.sh
time seq 10 | xargs -Iz echo "hello"
@zhangchiqing
zhangchiqing / Main.purs
Created Dec 25, 2016
StateMonad vs Folding
View Main.purs
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 / validation.js
Last active Nov 24, 2016
Validation with liftp and toPromise
View validation.js
// 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
Last active Jul 17, 2017
Purescript Async
View Main.purs
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 / getTweetsByUserId.js
Created Nov 13, 2016
An example of async programming with bluebird-promisell - Get Tweets by UserId
View getTweetsByUserId.js
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 / toPromiseMaybe.js
Created Nov 1, 2016
Maybe Promise a -> Promise Maybe a
View toPromiseMaybe.js
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 / promiseShouldFail.js
Last active Jul 13, 2016
promiseShouldFail.js
View 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));
});
You can’t perform that action at this time.