Created
July 11, 2012 22:00
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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