Skip to content

Instantly share code, notes, and snippets.

@nuttycom
nuttycom / gist:dc442de9faec92fbf5af
Created October 18, 2014 21:13
Cabal sandbox complaining about reinstalls?
cabal install --only-dependencies | grep reinstall 1 ↵
unix-2.7.0.1 (reinstall) changes: bytestring-0.10.4.0 -> 0.10.2.0
directory-1.2.1.0 (reinstall)
process-1.2.0.0 (reinstall)
cabal: The following packages are likely to be broken by the reinstalls:
haskell98-2.0.0.3
ghc-7.8.3
Cabal-1.18.1.4
bin-package-db-0.0.0.0
haskeline-0.7.1.2
object Foo {
def unapply(a: ASup): Option[B] =
a match {
case a0: A => a0.b match { case b: B => Some(b); case _ => None }
case _ => None
}
}
ax collect { case Foo(b) => ... }
@nuttycom
nuttycom / ListAlgebra.hs
Created November 6, 2014 04:11
Haskell list catamorphism
{-# LANGUAGE RankNTypes #-}
module ListAlgebra
( ListAlgebra(..)
) where
newtype ListAlgebra a = ListAlgebra (forall b. b -> (a -> b -> b) -> b)
nil :: ListAlgebra a
nil = ListAlgebra const
➜ quixotic git:(master) ✗ nix-shell --arg networkBitcoin 'import /home/nuttycom/oss/network-bitcoin {}'
[nix-shell:~/projects/quixotic]$ eval "$configurePhase"
configure flags: --enable-split-objs --disable-library-profiling --enable-shared --enable-library-vanilla --enable-executable-dynamic --enable-tests --ghc-option=-optl=-Wl,-rpath=/nix/store/jg1r4ghgyc97y84dnridx0wx4pq98g14-haskell-quixotic-ghc7.8.4-0.1-shared/lib/ghc-7.8.4/quixotic-0.1
Warning: quixotic.cabal: This package requires at least Cabal version 1.20
Configuring quixotic-0.1...
Setup: At least the following dependencies are missing:
configurator ==0.2.*, optparse-applicative >=0.9.0 && <0.10
[nix-shell:~/projects/quixotic]$ cat shell.nix
# This file was auto-generated by cabal2nix. Please do NOT edit manually!
require 'flow/common/function'
class Maybe
include Enumerable
def self.just(v)
Maybe.new(->(if_nothing, if_just) { if_just.call(v) })
end
def self.nothing
@nuttycom
nuttycom / overlapping_f-algebrae.md
Last active August 29, 2015 14:19
Overlapping F-algebras in Haskell

On a number of occasions, I've encountered situations where I've wanted to be able to define multiple data types that referenced the same data constructors, without requiring wrapping and unwrapping; while lenses make traversing these sorts of nested data structures easy, I still am not completely comfortable with them, so I went looking for something a bit simpler. The solution that came to me was this:

data X = X
data Y = Y
data Z = Z

-- An algebra over X and Y
data XYAlg a = XYAlg (X -> a) (Y -> a)
@nuttycom
nuttycom / gist:e4327adc21c840d77287
Created April 30, 2015 16:04
Capturing existential typeclasses in Scala
scala> trait Pet[A] {
| def blah(a: A): A
| }
defined trait Pet
scala> object IntPet extends Pet[Int] {
| def blah(i: Int) = i
| }
defined module IntPet
sealed trait Request[A]
case class PostTweet[A](userId: UserId, tweet: Tweet, a: => A) extends Request[A]
case class GetTweets[A](userId: UserId, cont: List[Tweet] => A) extends Request[A]
case class GetUserName[A](userId: UserId, cont: UserName => A) extends Request[A]
case class GetPhoto[A](userId: UserId, cont: UserPhoto => A) extends Request[A]
import java.util.UUID
object Test {
implicit def columnMap2Map[A, B](map: Map[Array[Byte], Array[Byte]]) = new MapConverter(map)
class MapConverter(map: Map[Array[Byte], Array[Byte]]) {
def to[A, B](implicit f: Array[Byte] => A, g: Array[Byte] => B): Map[A, B] = {
map.foldLeft(Map[A, B]()) { case(map, (k, v)) =>
val tuple = (k: A, v: B)
map + tuple
@nuttycom
nuttycom / gist:714663
Created November 24, 2010 23:58
What Iterator.iterate should have been.
object Bah {
def iterate[T](t: T)(f: T => Option[T]): Iterator[T] = new Iterator {
var next = Some(t)
override def hasNext = next.isDefined
override def next = {
val result = next.get
next = f(result)
result
}
}