Skip to content

Instantly share code, notes, and snippets.

View masaeedu's full-sized avatar

Asad Saeeduddin masaeedu

  • Montreal, QC, Canada
View GitHub Profile
@masaeedu
masaeedu / ComonadKleisli.hs
Last active July 4, 2019 18:52
Comonad on a Kleisli category
module ComonadKleisli where
import Data.Maybe
import Data.List
import Control.Monad
fmapM :: (a -> Maybe b) -> [a] -> Maybe [b]
fmapM f = Just . catMaybes . fmap f
duplicateM :: [a] -> Maybe [[a]]
@masaeedu
masaeedu / Test.purs
Created June 30, 2019 05:09
Lensing into cases that may not be there
module Test where
import Prelude
import Data.Lens (Lens, _Just, lens, (.~))
import Data.Maybe (Maybe(..))
import Effect.Exception.Unsafe (unsafeThrow)
data CRUD a
= Create
@masaeedu
masaeedu / Main.hs
Last active June 28, 2019 03:20
Questionnaire thing
{-# LANGUAGE
KindSignatures
, DataKinds
, RankNTypes
, GADTs
, TypeFamilies
, TypeFamilyDependencies
, ScopedTypeVariables
, TypeApplications
, ViewPatterns
@masaeedu
masaeedu / monoidal.hs
Created June 8, 2019 07:47
Various monoidal categories, monoidal functors, and monoid objects
{-# LANGUAGE
RankNTypes,
TypeApplications,
TypeOperators,
KindSignatures,
TypeFamilies,
TypeInType,
MultiParamTypeClasses,
FunctionalDependencies,
FlexibleInstances,
@masaeedu
masaeedu / reservoir_sampling.hs
Last active April 25, 2019 13:16
Reservoir sampling in Haskell
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
module Main where
import Prelude as P
@masaeedu
masaeedu / example1.dts
Last active April 11, 2019 00:45
Dream type system
:: :: morphism c = c -> c -> *
:: :: morphism c -> *
:: category p =
:: { id: p a a, compose: p b c -> p a b -> p a c }
:: :: (c -> d) -> morphism c -> morphism d -> *
:: functor f i o =
:: { map: i a b -> o (f a) (f b) }
@masaeedu
masaeedu / cpsfreer.js
Last active April 5, 2019 20:17
The CPS-ed freer monad
const { adt, match, otherwise } = require("@masaeedu/adt");
const {
Obj,
Fn,
Arr,
Cont,
implement,
Functor,
Apply,
Chain,
@masaeedu
masaeedu / traversing_state.js
Created March 27, 2019 00:43
Refactoring to discover Arr.traverse and the State applicative
// This gist shows how to factor a concrete stateful fold in vanilla JS
// into a number of general purpose utilities that can be combined to recover
// the original behavior
// We're going to start from a particular code snippet and iteratively make
// small changes to it to get a finished product
// While reading this post, it is useful to have a diff tool at hand that allows
// you to compare snippets side by side and see what changed in each iteration
// Utils
const range = n => [...Array(n).keys()];
const add = ([x0, y0]) => ([x1, y1]) => [x0 + x1, y0 + y1];
const rotate = θ => ([x, y]) => [
Math.round(x * Math.cos(θ) - y * Math.sin(θ)),
Math.round(x * Math.sin(θ) + y * Math.cos(θ))
];
const map = f => g =>
function*() {
for (const v of g()) {
@masaeedu
masaeedu / automonad.js
Last active April 15, 2019 14:10
Automatic monad stack construction using pointed functors on the category of monads
const { mdo } = require("@masaeedu/do");
const {
Obj,
Fn,
Fnctr,
ReaderT,
Reader,
StateT,
State
} = require("@masaeedu/fp");