Skip to content

Instantly share code, notes, and snippets.

View aaronlevin's full-sized avatar

Aaron Levin aaronlevin

View GitHub Profile
@aaronlevin
aaronlevin / record_shelf.txt
Last active September 3, 2018 18:20
Approximate size of record shelf
+------+----------------------------+
| ^ |
| | | depth: 12.75"
| | 12.92" |
| | |
| v |
+------+----------------------------+
| |
| |
| |
@aaronlevin
aaronlevin / TLists.scala
Last active November 29, 2020 19:10
Type-aligned, stack-safe lists.
import scala.annotation.tailrec
/**
* A Type-aligned list is a list of functions which can be
* chained together. They are "type-aligned" because their
* types all line. For example, suppose you have the following
* functions:
*
* val foo: Int => String = i => i.toString
* val bar: String => Char = s => s.head
@aaronlevin
aaronlevin / cats-mtl.scala
Last active May 26, 2017 17:38
Does the cats-mtl design support for-comprehensions with multiple Monad constraints? Looks like it does!
package cats
import cats.data.{StateT, WriterT}
import cats.implicits._
/**
* TypeLevel is working on some alternative encodings of the Monad Transformer
* Library (MTL). The existing solution in cats was broken for a few reasons.
* One annoying issue made it imposisible to use for-comprehension with more
* than one Monad$FOO constraint owing to implicit resolution ambiguities.
@aaronlevin
aaronlevin / simulacrum-generated-FlatMap.scala
Created April 5, 2017 13:20
Simulacrum-Generated FlatMap object in typelevel cats
// https://github.com/mpilquist/simulacrum
trait FlatMap[F[_]] extends Apply[F] with _root_.scala.Serializable {
def flatMap[A, B](fa: F[A])(f: _root_.scala.Function1[A, F[B]]): F[B];
def >>=[A, B](fa: F[A])(f: _root_.scala.Function1[A, F[B]]): F[B] = flatMap(fa)(f);
def flatten[A](ffa: F[F[A]]): F[A] = flatMap(ffa)(((fa) => fa));
def followedBy[A, B](fa: F[A])(fb: F[B]): F[B] = flatMap(fa)(((x$1) => fb));
@inline final def >>[A, B](fa: F[A])(fb: F[B]): F[B] = followedBy(fa)(fb);
def followedByEval[A, B](fa: F[A])(fb: Eval[F[B]]): F[B] = flatMap(fa)(((x$2) => fb.value));
override def ap[A, B](ff: F[_root_.scala.Function1[A, B]])(fa: F[A]): F[B] = flatMap(ff)(((f) => map(fa)(f)));
@aaronlevin
aaronlevin / mtl.scala
Last active April 4, 2017 21:52
Expeirmental Functor,Monad,Applicative implementation that supports MTL
import scala.annotation.tailrec
import scala.language.higherKinds
object jazz {
trait Functor[F[_]] {
def fmap[A,B](value: F[A])(func: A => B): F[B]
}
abstract class Applicative[F[_]](val functor: Functor[F]) extends Functor[F] {
@aaronlevin
aaronlevin / exception
Created April 3, 2017 20:20
When trying to compile this with Scala 2.12.1 I got a NullPointerException
☭ scala foo.scala
cat: /release: No such file or directory
java.lang.NullPointerException
at Main$Monad.<init>(foo.scala:29)
at Main$$anon$1.<init>(foo.scala:58)
at Main$.<init>(foo.scala:58)
at Main$.<clinit>(foo.scala)
at Main.main(foo.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
@aaronlevin
aaronlevin / minimal-http.hs
Last active June 27, 2017 16:37
Minimal Haskell HTTP server written on top of warp via the Web Application Interface (no frameworks)
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Network.Wai (pathInfo, Request, requestMethod, Response, responseLBS, ResponseReceived)
import Network.Wai.Handler.Warp (run)
import Network.HTTP.Types (status200, status401)
-- note: type Application = Request -> (Response -> IO ResponseReceived) -> IO ResponseReceived
application :: Request -> (Response -> IO ResponseReceived) -> IO ResponseReceived
@aaronlevin
aaronlevin / events.hs
Last active December 3, 2023 13:03
LambdaWorld 2016 & Typelevel Summit 2017 (Copenhagen): Type-Level DSLs // Typeclass induction
-- Our goal is to create a type describing a list of events. This is our
-- type-level DSL.
-- We will then use typeclass resolution to "interpret" this type-level DSL
-- into two things:
-- 1. A comma-separated list of events
-- 2. A method that, when given an event name and a payload, will try to parse
-- that event type with the payload. A form of dynamic dispatching
--
-- To model a list of types we will use tuples. You can imagine the list of
-- types "Int, String, Char" to look like:
@aaronlevin
aaronlevin / OCaml-99-00.ml
Last active October 14, 2016 10:02
99 Problems in OCaml
(* 99 problems in OCaml: https://ocaml.org/learn/tutorials/99problems.html *)
@aaronlevin
aaronlevin / cps.scala
Last active October 23, 2017 20:51
Scala version of the blog post Resources, Laziness, and Continuation-Passing Style
object cps {
/**
*
* The code below translates this blog post
* http://blog.infinitenegativeutility.com/2016/8/resources--laziness--and-continuation-passing-style)
* into scala, and uses laziness where appropriate to highlight the issue.
*
* I also include a type for IO. This is mainly illustrative, but also ensures
* we "sequence" actions appropriately. Haskell's IO monad works in tandem with the