Skip to content

Instantly share code, notes, and snippets.

@pfgray
pfgray / functions-are-functors.ts
Created September 20, 2019 16:04
Functions are functors
import * as React from 'react'
import { compose } from 'redux'
import { reader } from 'fp-ts/lib/Reader'
type StateContext<A> = [A, (a: A) => void]
const keepMax: (input: StateContext<number>) => StateContext<number> =
([state, setState]) => {
const keepMaxSetState = React.useCallback((n: number) => {
@pfgray
pfgray / non-breaking-option.scala
Created September 20, 2019 13:39
Non Breaking Option
import scalaz.Monad
import scalaz._, Scalaz._
object OptionTest {
case class User(name: String, image: String)
// Gets a user, optionally
@pfgray
pfgray / non-breaking-option.scala
Created September 20, 2019 13:39
Non Breaking Option
import scalaz.Monad
import scalaz._, Scalaz._
object OptionTest {
case class User(name: String, image: String)
// Gets a user, optionally
import { Monad2 } from "fp-ts/lib/Monad";
import { HKT } from "fp-ts/lib/HKT";
import { createSelector, Selector } from "reselect";
import { sequenceT } from "fp-ts/lib/Apply";
import { Do } from "fp-ts-contrib/lib/Do";
declare module "fp-ts/lib/HKT" {
interface URI2HKT2<L, A> {
Selector: Selector<L, A>;
}
import * as React from "react";
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
type Func = (...args: any) => any;
export type Lit = string | number | boolean | undefined | null | void | {};
export const tuple = <T extends Lit[]>(...args: T) => args;
type RenderProp<Props, SuppliedValue> = React.FC<
Props & { children: (s: SuppliedValue) => React.ReactNode }
>;
interface InferableHOC<ProvidedProps> {
import { array } from 'fp-ts/lib/Array'
import { io, IO } from 'fp-ts/lib/IO'
import { none, option, some, Option } from 'fp-ts/lib/Option'
import { getTraversableComposition } from 'fp-ts/lib/Traversable2v'
const T = getTraversableComposition(array, option)
T.traverse(???)
/**
* This method sequences an array of Monads, chaining each one into the next.
* The result will be one Monad of a list of each unwrapped value.
*/
function sequenceM(Type) {
return ms => {
function doIt(mArr, restMs) {
if(restMs.length === 0) {
return mArr;
} else {
@pfgray
pfgray / All.js
Last active September 20, 2018 03:27
// All works with all fantasy-land applicatives: https://github.com/fantasyland/fantasy-land#applicative
// It turns an array of applicatives into an applicative of array
function All(Type) {
return values =>
values.reduce((aggOp, aOp) => {
// crocks doesn't support ['fantasy-land/ap']
return aggOp['fantasy-land/ap'](aOp['fantasy-land/map'](a => {
return agg => agg.concat([a]);
}));
@pfgray
pfgray / Do2.js
Created September 13, 2018 11:40
// Do works with any Monad that implements the fantasy-land spec
// https://github.com/fantasyland/fantasy-land#monad
function Do(Type) { return (a, ...fns) => {
function doIt(as, fns) {
const [fn, ...rest] = fns;
if(rest.length === 0) {
return as['fantasy-land/chain'](a2s => {
return Type['fantasy-land/of'](fn.apply(null, a2s));
});
} else {
@pfgray
pfgray / Do.js
Last active September 16, 2018 20:34
// Do works with any Monad that implements the fantasy-land spec
// https://github.com/fantasyland/fantasy-land#monad
function Do(Type) { return (a, ...fns) => {
function doIt(as, fns) {
const [fn, ...rest] = fns;
if(rest.length === 0) {
return as['fantasy-land/chain'](a2s => {
return Type['fantasy-land/of'](fn.apply(null, a2s));
});
} else {