Skip to content

Instantly share code, notes, and snippets.


Erik Osheim non

  • Providence / Philly
View GitHub Profile
View test.scala
package test
trait Ugh
class Test {
def ugh = new Ugh {
def blah: Int = 999
View typalg.scala
* build.sbt:
* name := "typalg"
* scalaVersion := "2.11.1"
* libraryDependencies += "org.spire-math" %% "spire" % "0.7.5"
* sample sbt session:
non / for-doesnt-drop.txt
Last active Aug 29, 2015
Typelevel Scala has an option to make for-comprehensions easier to reason about
View for-doesnt-drop.txt
erik@vx-2 ~/w/tlc (2.11.x) ⛭ ./build/quick/bin/scala -Zirrefutable-generator-patterns
Welcome to Scala version 2.11.3-20141005-222847-d37174644b (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_04).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val list: List[Any] = List((1,2), (3,4), 1, (4,5))
list: List[Any] = List((1,2), (3,4), 1, (4,5))
scala> for { (a, b) <- list } yield a
scala.MatchError: 1 (of class java.lang.Integer)
non / 01_test.scala
Created Oct 26, 2014
Comparison of different constructor strategies.
View 01_test.scala
package test
abstract class Cat1(val name: String, val age: Int)
class Tabby1(override val name: String, override val age: Int) extends Cat1(name, age)
abstract class Cat2(val name: String, val age: Int)
class Tabby2(name0: String, age0: Int) extends Cat2(name0, age0)
non /
Last active Aug 29, 2015
Heyting[A] versus Bool[A]

So I promised an explanation around the changes to BooleanAlgebra[A] since 0.8.2.

The basic idea here is that we wanted to add support for lattices (and semi-lattices, and heyting algebras). These are structures which generalize boolean algebras but which are much more general. For example, the spire.math.Trilean type almost seems like it could be a boolean algebra, but it violates the law of the excluded middle.

Heyting[A] was introduced to support these types, and "algebra" was dropped from the name since it seemed redundant; Bool[A] was chosen to avoid a name conflict with Boolean[A].

non / emily.el
Created Jan 22, 2015
This is something I whipped together to make editing Emily files in Emacs a bit nicer.
View emily.el
;; major mode for the emily programming language
;; by erik osheim
;; project at:
;; see emily/doc/ for more information.
;; TODO:
;; 1. indentation support
;; 2. special support for definitions (e.g. f ^x = x + 1)

Lambda, the Syntax

Functions are a core concept in functional programming (hence the name). In Scala, there are many ways to define functions (and function-like things). This short essay is aimed at trying to help make the distinctions (and syntax) a bit clearer.

Scala has two distinct function-like constructions:

  • Methods: These are traditional JVM methods (i.e. Java methods on some other class).
  • Functions: These are values (i.e. Java objects) which represent functions.

Both Methods and Functions often behave similarly (and we sometimes use the term function broadly to refer to both). However, in a narrow sense Functions are distinct from Methods. The following sections explore this in a bit more detail.

View ugh.scala
package object foo {
implicit class ThrowAway(val x: Boolean) extends AnyVal {
def func[B]: B => String =
new Function1[B, String] {
def apply(b: B): String = "foo"
non / carnac.scala
Last active Aug 29, 2015
After reading @raganwald's article "Carnac the Magnificent" ( I was interested to see how well I could do with a naïve solution in Scala using streams. I tried to write a bunch of comments to make it clear how it works. Notably, it doesn't using anything like `eval()` which is one reaso…
View carnac.scala
package carnac
// the right approach to this problem is to use dynamic programming.
// i was curious about trying to come up with a readable, minimal
// example solution that worked. i feel ok about this.
object Carnac {
// represents a mathematical operation, either addition or subtraction.
sealed abstract class Sym(val f: (Int, Int) => Int, val s: String)
View gist:8a800e8efdd732f2e177
Verifying I am +d6 on my passcard.