Skip to content

Instantly share code, notes, and snippets.

@zhangchiqing
zhangchiqing / kubectl.md
Created January 31, 2019 05:19
Kubectl cheatsheet
@zhangchiqing
zhangchiqing / binarysearch.js
Created April 23, 2017 04:00
simple binary search
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);
}
// [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));
@zhangchiqing
zhangchiqing / benchmarkcommand.sh
Created February 28, 2017 19:27
benchmark a command
time seq 10 | xargs -Iz echo "hello"
@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 / 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
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 / 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 / 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 / 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));
});