Skip to content

Instantly share code, notes, and snippets.

Avatar

Kota Mizushima kmizu

View GitHub Profile
@kmizu
kmizu / From.scala
Last active Oct 1, 2021
Delayed list in various programming languages
View From.scala
scala> def from(n: Int): LazyList[Int] = LazyList.cons(n, from(n + 1))
def from(n: Int): LazyList[Int]
scala> from(0).take(10).toList
val res0: List[Int] = List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
@kmizu
kmizu / Calculator1.scala
Last active Jul 14, 2021
8÷2(3+1) に対する二通りのパーザ(あるいはインタプリタ)作ってみた
View Calculator1.scala
import com.github.kmizu.scomb._
object Calculator1 extends SCombinator[Int] {
def root: Parser[Int] = expression
def expression: Parser[Int] = rule(A)
def A: Parser[Int] = rule(chainl(M) {
$("+").map { op => (lhs: Int, rhs: Int) => lhs + rhs } |
$("-").map { op => (lhs: Int, rhs: Int) => lhs - rhs }
@kmizu
kmizu / NQueen.java
Created Dec 9, 2020
NQueen Problem Solver in Java
View NQueen.java
import java.util.*;
import java.awt.Point;
public class NQueen {
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_WHITE = "\u001B[37m";
private final int N;
private final Set<Point> queens;
public NQueen(int N) {
this.N = N;
@kmizu
kmizu / ExampleSpec.scala
Created Jun 24, 2020
NoSuchMethodError occurs in (Scala 2.12.x or 2.13.x) and ScalaTest 3.1.x with scalikejdbc
View ExampleSpec.scala
import org.scalatest.BeforeAndAfterAll
import org.scalatest.fixture.FunSpec
import scalikejdbc.{DB, _}
import scalikejdbc.config.DBs
import scalikejdbc.scalatest.AutoRollback
class ReservationRepositorySpec extends FunSpec with AutoRollback with BeforeAndAfterAll {
//テスト実行前にダミーデータを追加
override def beforeAll(): Unit = {
@kmizu
kmizu / description.md
Last active May 27, 2020
FizzBuzz class problem in Scala
View description.md

整数mから整数n(n >= 1、m >= 1)までのFizzBuzzを計算せよ。ただし、FizzBuzzのクラス定義は次のようになっていなければいけないものとする。

なお、剰余は x % y、 数値の文字列化は num.toString で可能である。

case class FizzBuzz(val m: Int, val n: Int) {
  def call(): Unit = {
    ???
  }
}
@kmizu
kmizu / For.ij
Created Apr 19, 2020
Indent-based Java (Thought Experiment)
View For.ij
package for
public class For:
public static void main(String[] args):
for(int i = 0; i < 10; i++):
System.out.println(i)
View A.scala
def mul(x, y) = x _*_ y
mul(
[1 2 3],
[4
5
6]) // 32
@kmizu
kmizu / console.scala
Created Feb 27, 2020
Matlike example
View console.scala
$ 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 Feb 19, 2020
Minimal Interpreter
View Tree.scala
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 Jan 7, 2020
Disassemble Hello.scala
View Hello.scala
class Hello {
def return100: Int = {
for(i <- 1 to 1000)
if(i == 100) return 100
0
}
}