Skip to content

Instantly share code, notes, and snippets.

@coltfred
coltfred / Catsflatten.scala
Created October 23, 2015 20:01
Cats flatten fail
scala> type ErrorOr[A] = Xor[Exception, A]
defined type alias ErrorOr
scala> def f[A](a:A): ErrorOr[A] = a.right
f: [A](a: A)ErrorOr[A]
scala> f(f("string")).flatten
res10: ErrorOr[String] = Right(string)
scala> "hello".right[Exception].right[Exception].flatten

Ideally I'd be able to write this with only one pass of data, but it's not possible in one pass (as far as I know)

def separate(r: RDD[A \/ B]): (RDD[A], RDD[B]) = ???

I'd settle for something like this where the As are dumped to a file and the Bs are still in the RDD. It's kind of like observeW from scalaz-stream.

package freedom
import scala.collection.JavaConverters._
import java.awt.{ Color, Graphics2D }
import java.awt.image.BufferedImage
import cats.data.{ Coproduct, Xor }
import cats.free.{ Free, Inject }

Keybase proof

I hereby claim:

  • I am coltfred on github.
  • I am coltfred (https://keybase.io/coltfred) on keybase.
  • I have a public key whose fingerprint is C461 3538 53FB 4AC0 9EEA 78B5 8F8C 1B8D 4E23 FC33

To claim this, I am signing this object:

@coltfred
coltfred / ColtFree.scala
Created October 7, 2016 15:11
Straightforword Free Monad implementation using Xor
package com.coltfred
import cats._, cats.data._, cats.implicits._, cats.free._, cats.arrow._
case class ColtFree[F[_]: Functor, A](resume: A Xor F[ColtFree[F, A]]) {
def map[B](f: A => B): ColtFree[F, B] = resume match {
case Xor.Left(a) => ColtFree(Xor.Left(f(a)))
case Xor.Right(nested) => ColtFree(Xor.Right(nested.map(_.map(f))))
}
@coltfred
coltfred / CommsExample.scala
Created November 2, 2016 20:06 — forked from tel/CommsExample.scala
Comms example
// Given an interface like
sealed trait Mult
sealed trait One extends Mult
sealed trait ZeroOrOne extends Mult
sealed trait Many extends Mult
case class NoParam()
@coltfred
coltfred / Advent1.hs
Created December 18, 2016 06:33
Advent1 of 2016's Advent of Code
-- Problem and flavor text is here: http://adventofcode.com/2016/day/1
module Advent1 where
import Prelude hiding(Right, Left)
import qualified Data.Set as Set
data Turn = Right Int | Left Int deriving (Eq, Show)
--east positive/west negative, south positive, north negative
newtype Position = Position { unPosition :: (Int,Int) } deriving (Eq, Show, Ord)
data Direction = North | East | South | West deriving (Eq, Show, Ord)
-- 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

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

@coltfred
coltfred / gist:3dcb07aa4a7133019f8667958afe3c7a
Created October 5, 2019 20:47
Exceptions in tests when upgrading > Java 8
WARNING: HK2 service reification failed for [org.glassfish.jersey.message.internal.DataSourceProvider] with an exception:
MultiException stack 1 of 2
java.lang.NoClassDefFoundError: javax/activation/DataSource
at java.base/java.lang.Class.getDeclaredConstructors0(Native Method)
at java.base/java.lang.Class.privateGetDeclaredConstructors(Class.java:3137)
at java.base/java.lang.Class.getDeclaredConstructors(Class.java:2357)