Skip to content

Instantly share code, notes, and snippets.

@amaya382
amaya382 / compareImmutableWithMutable.scala
Last active August 29, 2015 14:11
immutable なコレクション(immutable.List)と mutable なコレクションの(mutable.ListBuffer) append 性能比較
def append2Immutable(noTimes: Long): Unit = {
@scala.annotation.tailrec
def go(l: List[Int], i: Long = 0): List[Int] = {
if (i > noTimes) l
else go(l :+ 0, i + 1)
}
go(List())
}
def append2Mutable(noTimes: Long): Unit = {
@amaya382
amaya382 / Benchmark.scala
Last active July 7, 2016 20:09
Scalaのベンチマーク用スニペット
def timeOf(action: => Unit, noTimes: Int = 10, multiplier: Int = 1): Double = {
import java.util.concurrent.TimeUnit
val runtime = Runtime.getRuntime
val result = (1 to noTimes + 1) map { _ =>
val startTime = System.nanoTime
var i = 0
while (i < multiplier) {
action
i += 1
}
@amaya382
amaya382 / loan.scala
Last active August 29, 2015 14:11
loan pattern
class Loan[T <: {def close()}] private(resources: T) {
def map[U](func: T => U): Option[U] =
try {
Option(func(resources))
} catch {
case ex: Throwable =>
ex.printStackTrace()
None
} finally {
if (resources != null)
@amaya382
amaya382 / Fraction.cs
Last active August 29, 2015 14:01
第2回c#講習会演習の一部(サンプル)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication4
{
class Fraction
{