Skip to content

Instantly share code, notes, and snippets.

@possiblywrong
Created February 5, 2014 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save possiblywrong/8820864 to your computer and use it in GitHub Desktop.
Save possiblywrong/8820864 to your computer and use it in GitHub Desktop.
Shapeless poly binding & inference
object TestCase {
// Given input of type X return a function of
// type List[(A,B)] => List[B] forall B
// Types X & A are related
object filt extends Poly1 {
implicit def caseOne = at[Int]( i => {
object partialbind extends somefn(i);
partialbind
} )
// ...may have other cases
}
class somefn(j: Int) extends Poly1 {
implicit def caseOne[A] = at[List[(Int,A)]]( xs => xs.filter(_._1 == j).map(_._2) )
}
val t1 = filt(4)
val t2 = filt(5)(List((1,1),(5,5)))
// type mismatch;
// [error] found : List[(Int, Int)]
// [error] required: shapeless.poly.Case[discsort.TestCase.filt.type,shapeless.::[Int,shapeless.HNil]]
// [error] val t2 = filt(5)(List((1,1),(5,5)))
// [error] ^
}
object TestCase2 {
// Given input of type X return a function of
// type List[(A,B)] => List[B] forall B
// Types X & A are related
object filt extends Poly1 {
implicit def caseOne[A] = at[Int]( i => {
xs: List[(Int,A)] => xs.filter(_._1 == i).map(_._2)
} )
// ...may have other cases
}
val t1 = filt[String](4)
// overloaded method value apply with alternatives:
// [error] (implicit c: discsort.TestCase2.filt.ProductCase.Aux[shapeless.HNil,String])String <and>
// [error] (a: String)(implicit cse: shapeless.poly.Case[discsort.TestCase2.filt.type,shapeless.::[String,shapeless.HNil]])cse.Result
// [error] cannot be applied to (Int)
// [error] val t1 = filt[String](4)
// [error] ^
val t2 = filt[Int](5)(List((1,1),(5,5)))
// type mismatch;
// [error] found : List[(Int, Int)]
// [error] required: shapeless.poly.Case[discsort.TestCase2.filt.type,shapeless.::[Int,shapeless.HNil]]
// [error] val t2 = filt[Int](5)(List((1,1),(5,5)))
// [error] ^
}
@mandubian
Copy link

The only currification I could find with Poly2 is the following stupid one :)

object somefn extends Poly2 {
   implicit def caseOne[A] = at[Int, List[(Int,A)]]( (j, xs) => xs.filter(_._1 == j).map(_._2) )
}

def curried = { l : List[(Int, String)] => somefn.apply(5, l) }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment