Skip to content

Instantly share code, notes, and snippets.

@Blaisorblade
Created July 11, 2012 22:00
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 Blaisorblade/3093925 to your computer and use it in GitHub Desktop.
Save Blaisorblade/3093925 to your computer and use it in GitHub Desktop.
A code fragment in the context of https://github.com/ps-mr/LinqOnSteroids/
import ivm._
import expressiontree._
import Lifting._
import collection.generic.CanBuildFrom
import collection.TraversableLike
object OptimizationV1 {
val mergeMaps: PartialFunction[Exp[_], Exp[_]] = {
case MapNode(m@MapNode(coll, f), g) =>
(coll map (f andThen g))(m.cbf) //Here, g andThen f would be accepted by the compiler.
}
}
object OptimizationV2 {
//Some typechecking:
private def buildMergedMaps[T, U, V](coll: Exp[Traversable[T]], f: Fun[T, U], g: Fun[U, V]): Exp[Traversable[V]] =
(coll map (f andThen g)) //Here, g andThen f would be rejected.
val mergeMaps: PartialFunction[Exp[_], Exp[_]] = {
case MapNode(m @ MapNode(coll, f), g) =>
buildMergedMaps(coll, f, g)
}
}
object OptimizationV3 {
private def buildMergedMaps[
T, U, V, To <: Traversable[V] with TraversableLike[V, To]](coll: Exp[Traversable[T]],
f: Fun[T, U], g: Fun[U, V],
cbf: CanBuildFrom[Nothing, V, To with TraversableLike[V, To]]): Exp[To] =
coll map(f andThen g)(collection.breakOut(cbf))
val mergeMaps: PartialFunction[Exp[_], Exp[_]] = {
case m @ MapNode(MapNode(coll, f), g) =>
buildMergedMaps(coll, f, g, m.c)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment