Skip to content

Instantly share code, notes, and snippets.

View kmizu's full-sized avatar

Kota Mizushima kmizu

View GitHub Profile
@kmizu
kmizu / A.scala
Last active February 27, 2020 05:25
A
def mul(x, y) = x _*_ y
mul(
[1 2 3],
[4
5
6]) // 32
@kmizu
kmizu / console.scala
Created February 27, 2020 04:58
Matlike example
$ java -jar target/scala-2.13/matlike.jar
> 1
value = 1
> 23
value = 23
> [1 2 3; 4 5 6]
value =
[
1 2 3
4 5 6
@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)))
}