Skip to content

Instantly share code, notes, and snippets.

@daimatz
daimatz / Sudoku.scala
Created September 30, 2013 09:28
適当に書いた数独 (Scala)
object Sudoku {
val N = 9
val ROOM = Math.sqrt(N).asInstanceOf[Int]
type Coord = (Int, Int)
def nextCoord(coord: Coord): Coord = {
if (coord._2 == N - 1) {
return if (coord._1 == N-1) (0, 0) else (coord._1 + 1, 0)
} else {
@daimatz
daimatz / test.scala
Created October 9, 2013 11:02
リフレクションで動的にインスタンスを生成する何か
import scala.reflect.runtime.{universe => ru}
import ru._
case class Hoge(id: Int, title: String)
class Fuga[T: TypeTag](orig: T) {
val mirror = ru.runtimeMirror(Thread.currentThread.getContextClassLoader)
val clazz = mirror.runtimeClass(typeOf[T])
val fields = clazz.getDeclaredFields
@daimatz
daimatz / thread-proc.rb
Created October 25, 2013 04:06
Proc オブジェクトを単純に並列実行する例
f = Proc.new { puts "start f"; sleep 10; puts "end f" }
g = Proc.new { puts "start g"; sleep 5 ; puts "end g" }
puts "start"
[f, g].map{ |prc| Thread.new{ prc.call } }.map(&:join)
p Thread.list
puts "all end"
@daimatz
daimatz / github-wiki-toc.user.js
Last active December 28, 2015 09:48
GitHub Wiki TOC JS
// ==UserScript==
// @name GitHub Wiki TOC
// @namespace http://www.daimatz.net/
// @version 0.11
// @description Add TOC to GitHub Wiki Page
// @include https://github.com/*/*/wiki/*
// @license MIT License(http://en.wikipedia.org/wiki/MIT_License)
// ==/UserScript==
(function() {
case class User(username: String, password: String)
trait UserRepositoryComponent {
val userRepository: UserRepository
// ordinal class
class UserRepository {
def authenticate(user: User): User = {
println("authenticating user: " + user)
user
}
@daimatz
daimatz / CaseBench.scala
Last active January 4, 2016 08:28
h は遅い
import scala.collection.mutable.ArrayBuffer
import scala.util.Random
object Main {
def main(args: Array[String]) {
val data = testdata()
val start = System.currentTimeMillis()
val ret = f(data) // g, h
val end = System.currentTimeMillis()
println(ret)
@daimatz
daimatz / Lambda.scala
Created January 28, 2014 06:44
Lambda を渡す。 g のほうがわずかに速いか
object Main {
def main(args: Array[String]) {
val to_s: Int => String = _.toString
val start = System.currentTimeMillis()
(0 until 5000000).foreach { i =>
f(to_s, i)
// g(i)
}
val end = System.currentTimeMillis()
println(end - start)
object Main {
def main(args: Array[String]) = {
val data = testData()
val start1 = System.currentTimeMillis()
data.foreach(isValidIpFormat1)
val end1 = System.currentTimeMillis()
println("1: " + (end1 - start1))
val start2 = System.currentTimeMillis()
@daimatz
daimatz / Main.java
Last active May 10, 2021 09:35
QPS Control example
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Random;
class Main {
@daimatz
daimatz / httpd.py
Created July 23, 2014 17:34
SimpleHTTPServer with custom headers
#!/usr/bin/env python
import SimpleHTTPServer
import BaseHTTPServer
import sys
"""
Usage:
python httpd.py [port] [additional headers ...]