Skip to content

Instantly share code, notes, and snippets.

@shajra
shajra / Task.scala
Created August 23, 2013 03:34
integration code between Scalaz and Scala standard concurrency libraries.
import concurrent.{ExecutionContext, Future => SFuture, Promise}
import util.Try
import _root_.scalaz.\/
import _root_.scalaz.concurrent.{Task => ZTask}
object Task {
def fromScala[A]

EDIT: This gist has been promoted and is now a blog post.

Kinda-Curried Type Parameters

Scala methods can have multiple lists of value parameters but only one list of type parameters, which is occasionally irritating when some are inferable and others are not. Consider this method which has two type parameters, one inferable and one not.

import scalaz._, Scalaz._
@jtobin
jtobin / fix-free-cofree.hs
Created December 9, 2015 06:13
Fix, Free, and Cofree
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}
import Prelude hiding (succ)
newtype Fix f = Fix (f (Fix f))
deriving instance (Show (f (Fix f))) => Show (Fix f)
@shajra
shajra / ReadV.scala
Last active March 27, 2016 22:28
I've found this useful for accessing nested data with accumulated error reporting.
package shajra.extn.scalaz
import scalaz.
{ \/, Arrow, Applicative, Foldable, Functor, IsomorphismApplicative,
IsomorphismFunctor, IsomorphismMonoid, IsomorphismPlus,
IsomorphismSemigroup, Kleisli, Maybe, Monoid, NonEmptyList, Plus,
ProChoice, Semigroup, Validation }
import scalaz.Isomorphism.
{ <=>, <~>, <~~>, IsoBifunctorTemplate, IsoFunctorTemplate, IsoSet }
@Daenyth
Daenyth / README.md
Last active December 20, 2016 00:05

Running HTTP

  • sbt run - Run the http service on localhost:8989.
  • sbt ~re-start - Run the http service as above, but whenever the code changes, recompile and relaunch the service.

Tests

<project specific env setup>

Once the environment is configured, run

  • sbt test:compile - Compile the tests without running them

Revisiting Tagless Final Interpreters

Tageless Final interpreters are an alternative to the traditional Algebraic Data Type (and generalized ADT) based implementation of the interpreter pattern. This document presents the Tageless Final approach with Scala, and shows how Dotty with it's recently added implicits functions makes the approach even more appealing. All examples are direct translations of their Haskell version presented in the Typed Tagless Final Interpreters: Lecture Notes (section 2).

The interpreter pattern has recently received a lot of attention in the Scala community. A lot of efforts have been invested in trying to address the biggest shortcomings of ADT/GADT based solutions: extensibility. One can first look at cats' Inject typeclass for an implementation of [Data Type à la Carte](http://www.cs.ru.nl/~W.Swierstra/Publications/DataTypesA

-- The meta-circular interpreter from section 5 of Reynolds's Definitional
-- Interpreters for Higher Order Programming Languages
-- (http://www.cs.uml.edu/~giam/91.531/Textbooks/definterp.pdf)
data EXP
= CONST Const
| VAR Var
| APPL Appl
| LAMBDA Lambda
| COND Cond
@tel
tel / Lenses.scala
Created April 18, 2017 04:38
Pure profunctor lenses in Scala
import scala.language.higherKinds
object Lenses {
trait Profunctor[P[_, _]] {
def dimap[A, B, C, D](f: C => A, g: B => D)(p: P[A, B]): P[C, D]
}
object Profunctor {
@tonymorris
tonymorris / free-classy-prisms.hs
Created April 28, 2017 07:35
Free monad with classy prisms on grammar
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE DeriveFunctor #-}
import Control.Lens
import Prelude hiding (readFile, writeFile, print)
import qualified Prelude as Prelude(readFile, writeFile, print)
sealed trait Decidable[+Proof]
final case class Yes[Proof](proof: Proof) extends Decidable[Proof]
final case object No extends Decidable[Nothing]
sealed trait List[+A] {
def nonEmpty: Decidable[this.type <:< ::[A]]
def ::[AA >: A](value: AA): ::[AA] = new ::[AA](value, this)
}
final case object Nil extends List[Nothing] {
def nonEmpty: Decidable[this.type <:< ::[Nothing]] = No