Skip to content

Instantly share code, notes, and snippets.

scala> def foo[T, Coll[T] <: Seq[T]] = ???
warning: there were 1 feature warning(s); re-run with -feature for details
foo: [T, Coll[T] <: Seq[T]]=> Nothing
scala> foo[Int, List[Int]]
<console>:9: error: List[Int] takes no type parameters, expected: one
foo[Int, List[Int]]
^
scala> type LA[A] = List[Int]
defined type alias LA
trait Functor {
type M <: { type T }
def fmap[A, B](fa: M { type T = A })(f: A => B): M { type T = B }
}
implicit class EitherRightFunctor extends Functor { self =>
type L
type M = Either { type A = self.L ; type T = B } //doesn't this specify a subtype of Either, rather than Either itself?
def fmap[A0, B0](fa: M { type A = self.L ; type B = A0 })(f: A0 => B0): Either { type A = self.L ; type B = B0 } =
implicit class MaxBy[A](t: (A, A)) {
def maxBy[B: Order](f: A => B) =
(Order[A].order _).tupled(t umap f) match {
case GT | EQ => t._1
case LT => t._2
}
}
@nuttycom
nuttycom / gist:6800642
Created October 2, 2013 21:15
Monad for the right side of Either.
trait Monad[M[_]] {
def pure[A](a: A): M[A]
def flatMap[A, B](f: A => M[B]): M[A] => M[B]
def map[A, B](f: A => B): M[A] => M[B] = flatMap(a => pure(f(a)))
}
class EitherRightMonad[L] extends Monad[({type M[R] = Either[L, R]})#M] {
def pure[A](a: A): Either[L, A] = error("todo")
def flatMap[A, B](f: A => Either[L, B]): Either[L, A] => Either[L, B] = error("todo")
}
@nuttycom
nuttycom / .ctags
Created October 25, 2013 20:41
Scala ctags configuration.
--langdef=scala
--langmap=scala:.scala
--regex-scala=/^.*trait[ \t]+([a-zA-Z0-9_]+)/\1/t,traits/
--regex-scala=/^.*class[ \t]+([a-zA-Z0-9_]+)/\1/c,classes/
--regex-scala=/^.*object[ \t]+([a-zA-Z0-9_]+)/\1/o,objects/
--regex-scala=/^.*case class[ \t]+([a-zA-Z0-9_]+)/\1/c,case classes/
--regex-scala=/^.*case object[ \t]+([a-zA-Z0-9_]+)/\1/o,case objects/
--regex-scala=/^.*type[ \t]+([a-zA-Z0-9_]+)/\1/T,types/
--regex-scala=/^.*def[ \t]+([a-zA-Z0-9_\?]+)/\1/m,methods/
--regex-scala=/^.*val[ \t]+([a-zA-Z0-9_]+)/\1/C,constants/
@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