Skip to content

Instantly share code, notes, and snippets.

Miles Sabin milessabin

Block or report user

Report or block milessabin

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@milessabin
milessabin / gist:6c9c060cf5159563b722d49ce9ee103e
Last active Jul 26, 2017
Induction benchmark: vanilla scalac vs. scalac + proof of concept inductive solver
View gist:6c9c060cf5159563b722d49ce9ee103e
// Compiled with ./build/pack/bin/scalac -J-Xss4M -J-Xmx4G test/files/pos/inductive-implicits.scala
// HList scalac scalac +
// Size inductive heuristics
// 50 6 4
// 100 15 5
// 150 36 6
// 200 68 6
// 250 114 7
// 300 179 9
View gist:2cc749c84e504876cb6e9507993fa7c4
scala> def stuff(xs: ::[Int]): Int = xs.head
stuff: (xs: ::[Int])Int
scala> stuff(Nil)
<console>:13: error: type mismatch;
found : scala.collection.immutable.Nil.type
required: ::[Int]
stuff(Nil)
^
View gist:55939412017e2da20bcb9047240364f8
scala> def narrow[T <: Singleton](t: T): T {} = t
narrow: [T <: Singleton](t: T)T
scala> narrow(23)
res1: 23 = 23
scala> var x = narrow(42)
x: 42 = 42
scala>
View gist:9005671ea17add0f5054f01995e54025
### Keybase proof
I hereby claim:
* I am milessabin on github.
* I am milessabin (https://keybase.io/milessabin) on keybase.
* I have a public key whose fingerprint is 87D4 DCA4 07A5 556C 0BAD D01F 420D 4BEE 9FA4 7A44
To claim this, I am signing this object:
View sip23.scala
object Test {
def one[T <: 1](t: T): T = t
final val o = one(1)
o: 1
def narrow[T <: Singleton](t: T): T {} = t
final val fn0 = narrow(23)
fn0: 23
val n0 = narrow(23)
@milessabin
milessabin / singleton-only.scala
Created Jun 6, 2016
Scala type which can only be extended by an object, not by a non-abstract type ...
View singleton-only.scala
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
View pws.scala
import scala.language.higherKinds
object cats {
trait Show[A] {
def apply(x: A): String
}
object Show {
implicit def stringShow: Show[String] = new Show[String] {
def apply(x: String): String = x
@milessabin
milessabin / blah.scala
Created May 19, 2016
Doing something useful with bare singleton types and no macros ...
View blah.scala
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)
@milessabin
milessabin / shapeless-session.txt
Created Apr 1, 2016
shapeless on the Ammonite REPL with no dependencies other than an installed JDK. Many thanks to @przemekpokrywka for the idea, @alxarchambault for Coursier and @li_haoyi for Ammonite.
View shapeless-session.txt
miles@frege:~$ ./shapeless.sh
Loading...
Welcome to the Ammonite Repl 0.5.2
(Scala 2.11.7 Java 1.8.0_51)
@ val l = 23 :: "foo" :: true :: HNil
l: Int :: String :: Boolean :: HNil = ::(23, ::("foo", ::(true, HNil)))
@
@milessabin
milessabin / dec.scala
Created Mar 21, 2016
Idris's Dec in Scala
View dec.scala
sealed trait Dec[+T]
final case class Yes[T](t: T) extends Dec[T]
final case object No extends Dec[Nothing]
object Dec extends Dec0 {
implicit def yes[T](implicit t: T): Dec[T] = Yes(t)
}
trait Dec0 {
implicit def no[T]: Dec[T] = No
You can’t perform that action at this time.