Skip to content

Instantly share code, notes, and snippets.

View evilsoft's full-sized avatar

Ian Hofmann-Hicks evilsoft

View GitHub Profile
function filter(func, arr) {
return arr.reduce(function(memo, item) {
return func(item) ? memo.concat(item) : memo.slice()
}, [])
}
@evilsoft
evilsoft / Writer.js
Last active November 16, 2019 09:14
Valid Writer in javascript?
// Based on http://adit.io/posts/2013-06-10-three-useful-monads.html
import map from 'ramda/src/map'
import compose from 'ramda/src/compose'
import curry from 'ramda/src/curry'
import multiply from 'ramda/src/multiply'
const chain = curry((fn, m) => m.chain(fn))
const read = m => m.read()
@evilsoft
evilsoft / IO.js
Created July 5, 2016 18:42
Just an IO with map and chain
import compose from 'ramda/src/compose'
import curry from 'ramda/src/curry'
import map from 'ramda/src/map'
import always from 'ramda/src/always'
const chain = curry((fn, m) => m.chain(fn))
function IO(run) {
const map = fn => IO(compose(fn, run))
const chain = fn => IO(() => map(fn).run().run())
// I use flat map for a couple cases. I hope demonstrates
// a practical use case for flatMapping.
// IO is a way to capture a flow that is dependent on some side-effect.
// grabbing from and writing to a global (yuck!) for instance also logging to a console.
// We want to control these side effects. So if I have a flow that does multiple
// side-effects, chain is the way to go to compose them together and not run them
// until I call run on that IO.
const { IO, compose, curry, constant, map, chain } = crocks
// A common use for chaining Maybes is deep pulls on objects
// where there is uncertainty
const { curry, Maybe, option } = crocks
const data = {
happy: { joy: true }
}
// propMaybe : String -> Object -> Maybe
@evilsoft
evilsoft / Arrow.js
Last active January 20, 2017 06:48
Concat to compose a Star...
const isFunction = require('../predicates/isFunction')
const isType = require('../internal/isType')
const _inspect = require('../internal/inspect')
const compose = require('../helpers/compose')
const identity = require('../combinators/identity')
const constant = require('../combinators/constant')
const { Pred, mconcat, isArray, not, isEmpty, safe } = require('crocks')
const pred =
mconcat(Pred, [ not(isEmpty), isArray ])
// maybeArray : a -> Maybe []
const maybeArray =
safe(pred)
console.log(maybeArray([ 1, 2 ])) // Just [ 1, 2 ]
const {
Either, coalesce, compose,
constant, identity, ifElse,
isString, map
} = require('crocks')
const { Left, Right } = Either
// onlyStrings : a -> Either Number String
const onlyStrings =
const {
curry, isNumber, pipeS, prop, safeLift, Star
} = require('../crocks')
const add = curry(
(x, y) => x + y
)
const pull =
x => Star(prop(x))
@evilsoft
evilsoft / login.js
Created November 5, 2017 21:05
Using a ReaderT for database interaction
const { validateUser } = require('../data/users')
const {
compose, curry, objOf
} = require('crocks')
module.exports = ({ config, express, jwt, knex }) => {
const { jwtSecret } = config
const router = express.Router()