Skip to content

Instantly share code, notes, and snippets.

@ejconlon
ejconlon / trampoline.py
Created January 30, 2011 05:03
Tail-Recursion helper in Python
#!/usr/bin/env python
"""
Tail-Recursion helper in Python.
Inspired by the trampoline function at
http://jasonmbaker.com/tail-recursion-in-python-using-pysistence
Tail-recursive functions return calls to tail-recursive functions
(themselves, most of the time). For example, this is tail-recursive:
@ejconlon
ejconlon / pyreactor.py
Created February 27, 2011 03:26
Translated from the example in "Deprecating the Observer Pattern" (Maier, Rompf, Odersky)
#!/usr/bin/env python
"""
Translated from the example in
Deprecating the Observer Pattern (Maier, Rompf, Odersky)
http://lamp.epfl.ch/~imaier/pub/DeprecatingObserversTR2010.pdf
Their Scala example:
Reactor.once { self =>
// step 1:
@ejconlon
ejconlon / annotree.hs
Created May 20, 2011 04:35
Monad-like trees with fun applications like integer factoring
#!/usr/bin/env runhaskell
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, TypeSynonymInstances, FunctionalDependencies #-}
{- Monad-like trees can do cool things like factoring integers and
- pruning leaves with bind (>>=).
-
- Internal nodes are annotated with a measure of some sort that can be used
- for efficient indexing and sizing.
-
- Measured typeclass and tagged nodes from
@ejconlon
ejconlon / sudoku.hs
Created May 20, 2011 05:13
Project Euler 96 - Solve Sudoku! (in Haskell)
#!/usr/bin/env runhaskell
{- Project Euler 96 - Solve Sudoku!
-
- Compile it with ghc -o blah --make sudoku
-
- And grab sudoku.txt from
- http://projecteuler.net/index.php?section=problems&id=96
-
- Answer: 24702
@ejconlon
ejconlon / package.scala
Last active August 29, 2015 14:26
Elm Tasks, in Scala
package object tasks {
trait Task[A, B] {
private[tasks] def run(): Either[A, B]
def map[C](f: B => C): Task[A, C] = ???
def flatMap[C](f: B => Task[A, C]): Task[A, C] = ???
}
private[tasks] class Http[A, B](
data: HttpRequest,
handler: Either[Throwable, HttpResponse] => Either[A, B],
@ejconlon
ejconlon / haskds.md
Last active December 15, 2016 04:32
Simple data science operations in Haskell

Simple data science operations in Haskell

Simple tasks

  1. Read a table of data from disk

Cassava: exploratory CSV parsing

@ejconlon
ejconlon / hkd.scala
Last active January 13, 2021 16:17
Higher-Kinded Data Pattern in Scala
import scala.languageFeature.higherKinds
import cats.{Applicative, Id, ~>}
object Shapes {
case class Child[F[_]](nickname: F[String], age: F[Int])
case class Adult[F[_]](job: F[String], innerChild: Child[F])
class ApplicativeTrans[F[_]](implicit applicativeF: Applicative[F]) extends ~>[Id, F] {
override def apply[A](value: A): F[A] = applicativeF.pure(value)
@ejconlon
ejconlon / CopSpec.scala
Created May 12, 2018 18:04
Interpreting coproducts of functors
package example
import cats.~>
import org.scalatest.FunSuite
object CopSpec {
final case class Foo[+A](value: A)
final case class Bar[+A](value: A)
final case class Baz[+A](value: A)
@ejconlon
ejconlon / Repro.hs
Last active March 6, 2019 16:33
Repro Aeson optional serialization error
#!/usr/bin/env stack
-- stack --resolver lts-13.9 --install-ghc runghc --package aeson --package tasty --package text
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE UndecidableInstances #-}
import Data.Aeson
@ejconlon
ejconlon / Repro.hs
Created March 6, 2019 19:40
Fixed repro
#!/usr/bin/env stack
-- stack --resolver lts-13.9 --install-ghc runghc --package aeson --package tasty --package text
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}