This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Imperative | |
function foo(bar) { | |
if (!bar) { | |
return ; | |
} | |
return bar + 1; | |
} | |
foo(null); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
time seq 10 | xargs -Iz echo "hello" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// [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)); |