Skip to content

Instantly share code, notes, and snippets.

View kmizu's full-sized avatar

Kota Mizushima kmizu

View GitHub Profile
@kmizu
kmizu / Tree.scala
Created February 19, 2020 01:30
Minimal Interpreter
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)
@kmizu
kmizu / Hello.scala
Created January 7, 2020 05:59
Disassemble Hello.scala
class Hello {
def return100: Int = {
for(i <- 1 to 1000)
if(i == 100) return 100
0
}
}
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;
}
@kmizu
kmizu / build.sbt
Created January 1, 2020 10:06
Code snippet to control Raspberry Pi from Scala (sbt console)
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()
)
@kmizu
kmizu / Fact.java
Last active December 16, 2019 05:31
CCT社内勉強会用のものです
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;
}
}
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 {
@kmizu
kmizu / Macros.scala
Created December 12, 2019 17:02
Macros
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)
@kmizu
kmizu / Macros.scala
Created December 12, 2019 16:47
A macro the reverse identifier
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)))
}
@kmizu
kmizu / Macros.scala
Created December 12, 2019 16:34
A macro that reverse String
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
@kmizu
kmizu / Search.scala
Last active November 27, 2019 04:47
CS Study 2019/11/27
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
}