Skip to content

Instantly share code, notes, and snippets.

@ochafik
Last active December 13, 2015 22:38
Show Gist options
  • Save ochafik/4985398 to your computer and use it in GitHub Desktop.
Save ochafik/4985398 to your computer and use it in GitHub Desktop.
Ideas on how to extend Scalaxy/Compilets to support untyped trees for much more power and flexibility.
replace(
{
// $vparamss($x) are special variables that capture all parameter groups.
// $vparams($x) would only capture one group
@extend($type) def $someMethod($vparamss: $) = $body
},
{
implicit class $someMethod(self: $type) extends AnyRef {
def $someMethod($vparamss: $) = $body
// $fresh$x is a special variable that picks a fresh name with fresh("x")
def $fresh$f = {
println("Infinite loop!")
$fresh$f
}
}
}
)
replace(
{
def $algo($list: List[$r]): $r = {
$list match {
case $h :: $rest =>
$f($h) :: $algo($rest)
case Nil =>
Nil
}
$OR$
if ($list.isEmpty)
Nil
else
$f($list.head) :: $algo($list.tail)
}
},
{
def $algo($list: List[$r]): $r = {
var $fresh$l = $list
var $fresh$b = collection.mutable.ListBuilder[$r]()
while ($fresh$l ne Nil) {
$fresh$b += $f($fresh$l.head)
$fresh$l = $fresh$l.tail
}
$fresh$b.result()
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment