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 / lists.js
Last active May 2, 2018 13:12
Datatype-generic lists
const Arr = (() => {
const Nil = [];
const Cons = head => rest => [head, ...rest];
const match = ({ Cons, Nil }) => a =>
a.length === 0 ? Nil : Cons(a[0])(a.slice(1));
return { Nil, Cons, match };
})();
const GenericList = ({ Cons, Nil, match }) => {
@masaeedu
masaeedu / nify.js
Created May 6, 2018 03:15
n-ification
const { pipe, compose, map, encase, K, range, pow } = require("sanctuary");
const ret = compose;
const repeat = n => x => map(K(x))(range(0, n));
const nify = n => pipe(repeat(n - 1)(ret));
const fail = () => {
throw new Error();
};
// N-ifying encase
require("@babel/polyfill");
const {
Fnctr,
Arr,
implement,
Functor,
Chain,
Apply
} = require("@masaeedu/fp");
const Cont = require("@masaeedu/fp/dist/instances/cont");
@masaeedu
masaeedu / index.js
Last active June 7, 2018 14:34
mysillypackage
const sillify = input => input
.split("")
.map((c, i) => i % 2 === 0 ? c.toLowerCase() : c.toUpperCase())
.reduce((p, c) => p + c)
module.exports = { sillify }
@masaeedu
masaeedu / _main.js
Last active January 13, 2019 22:20
Interpreting callback APIs as pure, monad-returning functions
const Cont = require("@masaeedu/fp/dist/instances/cont");
const S = require("sanctuary");
// > It is often said that callbacks do not compose. While this may
// > be true, functions that *accept* callbacks compose extraordin-
// > arily well. In fact, such functions, which I'll hereafter refer
// > to as "continuations", form a monad, and a rather powerful and
// > versatile monad at that.
// >
// > In this snippet, I'll try to demonstrate how the continuation
@masaeedu
masaeedu / datatypes.js
Last active July 3, 2018 15:47
Various datatypes
const fail = reason => {
throw reason;
};
const log = console.log;
const Maybe = (() => {
const k = Symbol();
// Algebra
const Just = x => ({ [k]: x });
{-# LANGUAGE NoMonomorphismRestriction, FlexibleContexts, FlexibleInstances, DeriveFunctor #-}
import Data.Bool
import Data.Functor
import Data.Bifunctor
import Data.Function ((&))
import Data.Semigroup
import Control.Applicative
import Control.Monad
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace FPUtils
{
using static EnumerableUtils;
using static DictUtils;
using static TaskUtils;
@masaeedu
masaeedu / rng.js
Created July 15, 2018 11:38
Using the state monad for RNG
// A simple pseudorandom number generator
// http://indiegamr.com/generate-repeatable-random-numbers-in-js/
// :: type Seed = Int
const PRNG = {
// :: Seed -> Seed
next: s => (s * 9301 + 49297) % 233280,
// Generate a number between 0 and 1
// and increment the seed
// :: Seed -> (Float, Seed)
random: s_ => {
using System;
using System.Threading.Tasks;
namespace tmp.zNpqzfT0PE
{
class Program
{
static void Main(string[] args)
{
var job = ChangeForegroundColor(ConsoleColor.Blue)