-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Streams: support flatten (issue #22)
- Loading branch information
Showing
5 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package scalaxy.streams | ||
|
||
import impl.withQuietWarnings | ||
|
||
private[streams] trait FlattenOps | ||
extends ClosureStreamOps | ||
with CanBuildFromSinks | ||
with Streams | ||
with Strippers | ||
{ | ||
val global: scala.reflect.api.Universe | ||
import global._ | ||
|
||
val SomeStream: Extractor[Tree, Stream] | ||
|
||
object SomeFlattenOp extends StreamOpExtractor { | ||
private[this] lazy val PredefSymbol = rootMirror.staticModule("scala.Predef") | ||
override def unapply(tree: Tree) = Option(tree) collect { | ||
case q"$target.flatten[$tpt]($asTraversable)" if { | ||
asTraversable match { | ||
case q"$predef.`$conforms`[$colTpt]" if predef.symbol == PredefSymbol => | ||
// println(s"colTpt = $colTpt, target.tpe = ${target.tpe}") | ||
target.tpe match { | ||
case TypeRef(_, _, List(internalColTpe)) => | ||
internalColTpe =:= colTpt.tpe | ||
case _ => | ||
false | ||
} | ||
|
||
case Strip(Function(List(param), Option2Iterable(ref))) if param.symbol == ref.symbol => | ||
true | ||
} | ||
} => | ||
(target, FlattenOp(tpt.tpe)) | ||
// case _ if { | ||
// println("NOT A FLATTEN: " + tree) | ||
// false | ||
// } => | ||
// ??? | ||
} | ||
} | ||
|
||
case class FlattenOp(tpe: Type) extends StreamOp | ||
{ | ||
override def describe = Some("flatten") | ||
|
||
override def lambdaCount = 0 | ||
|
||
override def subTrees = Nil | ||
|
||
override def canInterruptLoop = false | ||
|
||
override def canAlterSize = true | ||
|
||
override def transmitOutputNeedsBackwards(paths: Set[TuploidPath]) = paths | ||
|
||
override val sinkOption = None | ||
|
||
override def emit(input: StreamInput, | ||
outputNeeds: OutputNeeds, | ||
nextOps: OpsAndOutputNeeds): StreamOutput = | ||
{ | ||
import input.{ vars, fresh, transform, typed, currentOwner } | ||
|
||
val itemVal = fresh("item") | ||
val Function(List(itemValDef @ ValDef(_, _, _, _)), itemValRef @ Ident(_)) = typed(q""" | ||
($itemVal: $tpe) => $itemVal | ||
""") | ||
|
||
val sub = emitSub( | ||
input.copy( | ||
vars = ScalarValue(tpe, alias = Some(itemValRef)), | ||
outputSize = None, | ||
index = None), | ||
nextOps) | ||
|
||
sub.copy(body = List(withQuietWarnings(transform(typed(q""" | ||
// TODO: plug that lambda's symbol as the new owner of sub.body's decls. | ||
${vars.alias.get}.foreach(($itemValDef) => { | ||
..${sub.body}; | ||
}) | ||
"""))))) | ||
} | ||
} | ||
} |
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
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