Skip to content

Instantly share code, notes, and snippets.

View travisbrown's full-sized avatar
😎
In hiding

Travis Brown travisbrown

😎
In hiding
View GitHub Profile
/** Constructing "singleton types" from compile-time literals
*
* val x = Example.foo(42)
* val y: x.T = 42 // compiles
* val z: x.T = 43 // doesn't
*
*/
package s
import scala.language.experimental.macros
@milessabin
milessabin / gist:4719811
Created February 6, 2013 02:50
Sneak preview for NEScala
scala> def foo[A, B, C](a: SNat[A], b: SNat[B], c: SNat[C])(implicit ssum: SSum[A, B, C]) = ssum
foo: [A, B, C](a: shapeless.SNat[A], b: shapeless.SNat[B], c: shapeless.SNat[C])(implicit ssum: shapeless.SSum[A,B,C])shapeless.SSum[A,B,C]
scala> foo(2, 3, 5)
res0: shapeless.SSum[Int(2),Int(3),Int(5)] = $anon$1@53d76e96
scala> foo(2, 3, 7)
<console>:15: error: could not find implicit value for parameter ssum: shapeless.SSum[Int(2),Int(3),Int(7)]
foo(2, 3, 7)
^
@folone
folone / gist:4946543
Last active December 13, 2015 17:18
Hanoi towers at compile time become very easy with dependent types. https://gist.github.com/travisbrown/3772462
> shapeless-core/console
[warn] Credentials file /home/folone/.ivy2/.credentials does not exist
[info] Compiling 24 Scala sources to /home/folone/workspace/shapeless/core/target/scala-2.11/classes...
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.11.0-20130205-141957-132e09fc2e (OpenJDK 64-Bit Server VM, Java 1.7.0_09).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import shapeless.SingletonTypes._
@vmarquez
vmarquez / TxMapTest.scala
Last active November 28, 2021 12:33
A mini STM if you will. I've made a'Transactional' map that mutates in a referentially transparent way.
import java.util.concurrent.atomic.AtomicReference
import java.util.concurrent.CountDownLatch
import scala.concurrent.Future
import scala.concurrent.ExecutionContext
import ExecutionContext.Implicits.global
object TxMapTest {
/*
* Example Usage
* We want to show two threads working with the same data source having both of their effects succeed
@joewiz
joewiz / trim-phrase-to-length.xq
Last active March 3, 2016 04:57
Trim phrases of arbitrary length to a maximum length, without cutting off words or ending on unwanted words, with XQuery
xquery version "3.0";
declare function local:trim-phrase-to-length($phrase, $length) {
(: if the phrase is already short enough, we're done :)
if (string-length($phrase) le $length) then
$phrase
(: the phrase is too long, so... :)
else
(: we will split the phrase into words and look for the longest possible arrangement within our length limit,
that doesn't end with boring words :)
@milessabin
milessabin / gist:6140251
Created August 2, 2013 14:25
HLists and singleton types encode records in Scala
scala> import shapeless._; import SingletonTypes._; import Record._
import shapeless._
import SingletonTypes._
import Record._
scala> val r = ("foo" ->> 23) :: ("bar" ->> true) :: ("baz" ->> 2.0) :: HNil
r: (String("foo"), Int) :: (String("bar"), Boolean) :: (String("baz"), Double) :: HNil =
(foo,23) :: (bar,true) :: (baz,2.0) :: HNil
scala> r.head // r is an HList of pairs of singleton-typed Strings and values ...
@folone
folone / gist:6258410
Last active April 27, 2018 14:09
Ackermann function
def ackermann(m: Int, n: Int): Int = {
(m, n) match {
case (0, _) ⇒ n + 1
case (m, 0) if m > 0 ⇒ ackermann(m - 1, 1)
case (m, n) if m > 0 && n > 0 ⇒ ackermann(m - 1, ackermann(m, n - 1))
}
}
import scalaz._, Scalaz._, Free.{suspend ⇒ _, _}, Trampoline._
def ackermannTramp(m: Int, n: Int): Trampoline[Int] = {
import shapeless._
import record._
import syntax.singleton._
object ScaldingPoC extends App {
// map, flatMap
val birds =
List(
"name" ->> "Swallow (European, unladen)" :: "speed" ->> 23 :: "weightLb" ->> 0.2 :: "heightFt" ->> 0.65 :: HNil,
@channingwalton
channingwalton / shapelessy.scala
Last active August 29, 2015 14:00
Replacing boilerplate with shapeless
case class Foo[T](x: T) {
def map[B](f: T => B) = Foo(f(x))
}
object OldWay {
def combineLatest[T1, T2](e1: Foo[T1], e2: Foo[T2]): Foo[(T1, T2)] = Foo((e1.x, e2.x))
def combineLatest[T1, T2, T3](e1: Foo[T1], e2: Foo[T2], e3: Foo[T3]): Foo[(T1, T2, T3)] =
combineLatest(combineLatest(e1, e2), e3) map {
@milessabin
milessabin / gist:11244675
Created April 24, 2014 07:17
Lens inference in shapeless 2.0.0
import shapeless._
import ops.record.{ Selector, Updater }
import record.{ FieldType, field }
trait PathLens[T, P] {
type Elem
def get(t : T) : Elem
def set(t : T)(e : Elem) : T
}