This file contains hidden or 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
case class Add(l: Tree, r: Tree) extends Tree | |
case class Sub(l: Tree, r: Tree) extends Tree | |
case class Mul(l: Tree, r: Tree) extends Tree | |
case class Div(l: Tree, r: Tree) extends Tree | |
case class Num(v: Int) extends Tree | |
// switch ≒ match | |
def eval(tree: Tree): Int = tree match { | |
case Add(l, r) => eval(l) + eval(r) | |
case Sub(l, r) => eval(l) - eval(r) |
This file contains hidden or 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
class Hello { | |
def return100: Int = { | |
for(i <- 1 to 1000) | |
if(i == 100) return 100 | |
0 | |
} | |
} |
This file contains hidden or 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 { | |
com.pi4j.io.i2c.I2CBus; | |
com.pi4j.io.i2c.I2Device; | |
com.pi4j.io.i2c.I2CFactory; | |
java.io.IOException; | |
} | |
def up(x: Byte): Int { | |
return x; | |
} |
This file contains hidden or 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
scalaVersion := "2.13.1" | |
name := "pi4r" | |
version := "0.0.1-SNAPSHOT" | |
libraryDependencies ++= Seq( | |
"com.pi4j" % "pi4j-core" % "1.2", | |
"com.pi4j" % "pi4j-device" % "1.2", | |
"com.pi4j" % "pi4j-gpio-extension" % "1.2", | |
"com.pi4j" % "pi4j-service" % "1.1", | |
"com.pi4j" % "pi4j-native" % "1.2" pomOnly() | |
) |
This file contains hidden or 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
public class Fact { | |
public static int fact(int n) { | |
if(i < 0) throw new IllegalArgumentException("n must be >= 0"); | |
int r = 1; | |
for(int i = 1; i <= n; i++) { | |
r *= i; | |
} | |
return r; | |
} | |
} |
This file contains hidden or 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 scala.reflect.macros.whitebox.Context | |
import scala.language.experimental.macros | |
object Macros { | |
def test(body: Any*): Unit = macro testImpl | |
def testImpl(c: Context)(body: c.Expr[Any]*) : c.Expr[Unit] = { | |
import c.universe._ | |
for(expr <- body) { | |
print("expr is ") | |
expr.tree match { |
This file contains hidden or 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 scala.reflect.macros.whitebox.Context | |
import scala.language.experimental.macros | |
object Macros { | |
def reverse(id: Any): Any = macro reverseImpl | |
def reverseImpl(c: Context)(id: c.Expr[Any]) : c.Expr[Any] = { | |
import c.universe._ | |
println(id) | |
println(id.tree) | |
println(id.getClass) |
This file contains hidden or 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 scala.reflect.macros.whitebox.Context | |
import scala.language.experimental.macros | |
object Macros { | |
def reverse(id: Any): Any = macro reverseImpl | |
def reverseImpl(c: Context)(id: c.Expr[Any]) : c.Expr[Any] = { | |
import c.universe._ | |
val Ident(TermName(name)) = id.tree | |
c.Expr[Any](Ident(TermName(name.reverse))) | |
} |
This file contains hidden or 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 scala.reflect.macros.whitebox.Context | |
import scala.language.experimental.macros | |
object Macros { | |
// 文字列をコンパイル時に反転させるマクロ | |
def reverse(str: String): String = macro reverseImpl | |
// マクロの実装 | |
def reverseImpl(c: Context)(str: c.Expr[String]) : c.Expr[String] = { | |
import c.universe._ | |
val Literal(Constant(s:String)) = str.tree |
This file contains hidden or 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
case class Point(x: Int, y: Int) | |
def traverse(p: Point): Int = p match { | |
case Point(3, 3) => | |
1 | |
case Point(x, y) if x <= 3 && y <= 3 => | |
traverse(p.copy(x = p.x + 1)) + | |
traverse(p.copy(y = p.y + 1)) | |
case Point(_, _) => | |
0 | |
} |