Skip to content

Instantly share code, notes, and snippets.

@beala
beala / Main.hs
Created January 12, 2016 21:41
haskell docker dsl sketch
{-# LANGUAGE DeriveFunctor #-}
module Main where
import Control.Monad.Free
data DockerAction a = Add String a
| EntryPoint String a
| Run String a
deriving (Functor)
@beala
beala / sensor-server.hs
Last active February 22, 2017 00:07
An HTTP endpoint that accepts sensor readings as JSON and writes them to a database. Written with servant: http://haskell-servant.github.io/tutorial/
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Main where
import Data.Aeson
@beala
beala / future-side-effect.scala
Last active October 18, 2022 14:59
Mixing side effects and futures is painful.
import com.twitter.util._
// import scala.concurrent.Future
// import scala.concurrent.ExecutionContext.Implicits.global
// Prints 'future' once.
Future{println("future")}
// Lets give that action a name
lazy val printLoop = Future{println("future")}
// Now lets evaluate it.
$ cabal install cabal-helper-0.3.6.0
Resolving dependencies...
Notice: installing into a sandbox located at
/private/tmp/ghc-mod/.cabal-sandbox
Configuring lifted-base-0.2.3.6...
Building lifted-base-0.2.3.6...
Installed lifted-base-0.2.3.6
Configuring io-choice-0.0.5...
Building io-choice-0.0.5...
Installed io-choice-0.0.5
@beala
beala / ReverseLang.hs
Last active August 29, 2015 14:22
Using tardis (forwards and backwards state monad) to implement a toy language that lets you dereference a var before assigning to it.
{-# LANGUAGE RecursiveDo #-}
import Control.Monad.Tardis
import qualified Data.Map as M
-- Language that lets you dereference variable names and assign to names,
-- but assignment must happen *after* dereference!
data ReverseLang = Assign String Int -- Assign to a name.
| Var String -- Dereference a name.
@beala
beala / timer.hs
Created May 29, 2015 16:52
Free Timer monad with multiple interpreters
{-# LANGUAGE DeriveFunctor #-}
import Control.Concurrent.Timer
import Control.Concurrent.Suspend.Lifted
import Control.Monad.Free
import GHC.Int (Int64)
import Control.Monad.Writer.Lazy
-- Timer type to be lifted into Free.
data TimerM r = TimerM Int64 (IO ()) r deriving (Functor)
@beala
beala / fix.scala
Created May 29, 2015 02:24
FIxpoint combinator.
scala> def fix[A](f: (=> A) => A): A = f(fix(f))
fix: [A](f: (=> A) => A)A
scala> def fact(f: => (Int => Int))(n: Int): Int = if (n>0) n * f(n-1) else 1
fact: (f: => Int => Int)(n: Int)Int
scala> fix(fact)(10)
res19: Int = 3628800
@beala
beala / sched.hs
Created May 23, 2015 04:55
smt scheduling
import Data.SBV
sched :: SInteger -> SInteger -> SInteger -> SInteger -> SInteger -> SInteger -> SBool
sched t_11 t_12 t_21 t_22 t_31 t_32 = (t_11 .>= 0) &&& (t_12 .>= t_11 + 2) &&& (t_12 + 1 .<= 8) &&&
(t_21 .>= 0) &&& (t_22 .>= t_21 + 3) &&& (t_22 + 1 .<= 8) &&&
(t_31 .>= 0) &&& (t_32 .>= t_31 + 2) &&& (t_32 + 3 .<= 8) &&&
((t_11 .>= t_21 + 3) ||| (t_21 .>= t_11 + 2)) &&&
((t_11 .>= t_31 + 2) ||| (t_31 .>= t_11 + 2)) &&&
((t_21 .>= t_31 + 2) ||| (t_31 .>= t_21 + 3)) &&&
((t_12 .>= t_22 + 1) ||| (t_22 .>= t_12 + 1)) &&&
@beala
beala / NonEmptyList.scala
Last active August 29, 2015 14:21
NonEmptyList
object Main {
// NonEmpty because the constructor forces you to provide at least one
// instance of A.
case class NonEmptyList[A](a: A, as: List[A]) {
def map[B](f: A => B): NonEmptyList[B] = {
NonEmptyList[B](f(a), as.map(f))
}
def flatMap[B](f: A => NonEmptyList[B]): NonEmptyList[B] = flatten(map(f))
}
@beala
beala / rot13.hs
Created March 29, 2015 22:58
rot13 (verified!)
{-# LANGUAGE DeriveDataTypeable #-}
import Data.SBV
import Control.Monad.IO.Class
import Data.Foldable
import Data.Generics
-------------------------------------------------------------------------------
-- Rot13 over an array of 8 bit words.
--