Skip to content

Instantly share code, notes, and snippets.

@etorreborre
etorreborre / eff-match-result.scala
Last active April 24, 2016 21:27
Experiment using the Eff monad to test deeply nested structures
val e = Option(List(Right(2), Left("bad")))
runResult {
for {
as <- (e must beSome).opt
a <- fromList(as)
i <- (a must beRight).opt
_ <- (i must be_>(0)).check
} yield ()
}
@paulp
paulp / valign.txt
Last active May 9, 2016 20:55
Realizing the benefits of vertical alignment.
To realize the benefits of vertical alignment:
- code must be *self-formatting*
- deltas must be *semantic*
Each is within reach today, with moderate effort:
- edit *virtual* files which continuously adapt
- store semantic actions in a parallel VCS
@raichoo
raichoo / gist:5371927
Last active May 12, 2016 18:43
Playing with propositional equality in Scala (+inductive proof)
import scala.language.higherKinds
/*
* The usual peano numbers with addtion and multiplication
*/
sealed trait Nat {
type Plus[N <: Nat] <: Nat
type Mult[N <: Nat] <: Nat
}
:paste
type ErrorMessage = String
trait LeftBiased
trait RightBiased
sealed abstract class Or[+G, +B] extends LeftBiased {
def map[H](f: G => H): H Or B
@etorreborre
etorreborre / dynamic_typeclass.scala
Created June 1, 2016 06:47
"Dynamic" typeclass instance
import cats._, data._
object Test {
trait Decoder[T] {
def decode(s: String): String Xor T
}
case class Password(pw: String)
case class User(name: String)
@milessabin
milessabin / blah.scala
Created May 19, 2016 13:32
Doing something useful with bare singleton types and no macros ...
scala> trait Label[T] { val value: Boolean }
defined trait Label
scala> def mkInstance(s: String, v: Boolean): Label[s.type] = new Label[s.type] { val value = v }
mkInstance: (s: String, v: Boolean)Label[s.type]
scala> implicit def lTrue = mkInstance("True", true)
lTrue: Label[String("True")]
scala> implicit def lFalse = mkInstance("False", false)
@etorreborre
etorreborre / gist:7126b0804f5d4e68fd4ad373931950ac
Created June 7, 2016 15:17
Transform your browser into a text editor, paste this in the navigation bar
data:text/html, <html contenteditable style="font-family:Courier New, Sans Serif;font-size:90px">
@milessabin
milessabin / singleton-only.scala
Created June 6, 2016 11:04
Scala type which can only be extended by an object, not by a non-abstract type ...
scala> class Foo { self: Singleton => }
defined class Foo
scala> class Bar extends Foo
<console>:12: error: illegal inheritance;
self-type Bar does not conform to Foo's selftype Foo with Singleton
class Bar extends Foo
^
scala> object Bar extends Foo
@paulp
paulp / omg.scala
Created July 19, 2016 18:31
scala is bad.
object Main {
val s1 = Set[BigDecimal](-4.872401723124452E9, 0, 1.1752011936438014, 8.696374707602505E17, -1.1752011936438014)
val s2 = Set[Double] (-4.872401723124452E9, 0, 1.1752011936438014, 8.696374707602505E17, -1.1752011936438014)
val s3: Set[BigDecimal] = s2 map (d => BigDecimal(d))
def main(args: Array[String]): Unit = {
/* false */ println(s1 == s2)
/* true */ println(s1 == s3)
/* false */ println(s2 == s3)
/* true */ println(s1.toSeq.sorted == s2.toSeq.sorted)
@milessabin
milessabin / gist:b16e0765e1acdc90fd29
Last active August 16, 2016 16:25
Functional update of common fields of an open family of case classes via a case-class-like copy through the common super type ...
import shapeless._
/**
* Functional update of common fields of an *open* family of case classes
* via a case-class-like copy through the common super type ...
*
* This is a follow up to my earlier post showin how to do functional
* update for a sealed family of case classes,
*
* https://gist.github.com/milessabin/a212d946aef33811fee1