Skip to content

Instantly share code, notes, and snippets.

View daiksy's full-sized avatar

KASUYA, Daisuke daiksy

View GitHub Profile
def qSort[T <% Ordered[T]](data: List[T]): List[T] = {
data match {
case Nil => data
case x::xs => {
qSort(xs.filter(_ < x)) ++ List(x) ++ qSort(xs.filter(x <= _))
}
}
}
qSort(List(2, 44, 5, 3, 1, 3, 9, 56, 7, 8, 4, 111, 0, 3, 4)) foreach println
def bubbleSort(arr: Array[Int]): Array[Int] = {
for(i <- 0 until arr.length - 1; j <- 0 until arr.length - 1 - i) {
if (arr(j) > arr (j + 1)) {
val temp = arr(j)
arr(j) = arr(j + 1)
arr(j + 1) = temp
}
}
arr
}
@daiksy
daiksy / gist:5972013
Last active December 19, 2015 14:49
ScalaでRuntimeExceptinの派生
class MyException(message: String, cause: Throwable) extends RuntimeException(message, cause) {
def this() = this(null, null)
def this(message: String) = this(message, null)
def this(cause: Throwable) = this(null, cause)
}
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant::Config.run do |config|
# All Vagrant configuration is done here. The most common configuration
# options are documented and commented below. For a complete reference,
# please see the online documentation at vagrantup.com.
# Every Vagrant virtual environment requires a box to build off of.
config.vm.box = "centos64_64"
@daiksy
daiksy / gist:5363696
Last active December 16, 2015 02:39
質問にお答えする

step1

パラメータを受け取るコントローラを作る

object Application extends Controller {
  def parameterExample(parameter: String) = Action  {
    Ok(views.html.index(parameter))
  }
}
@daiksy
daiksy / DateUtil.scala
Last active December 15, 2015 23:09
ScalaでC#っぽい Date ⇔ StringとかDate同士の大小比較とかするためのtrait
package util
import java.util.Date
import java.text.SimpleDateFormat
object DateUtil {
implicit class StringToDate(self: String) {
/**
* フォーマット指定された形式でDate型に変換する
*
@daiksy
daiksy / dq7Baroque.scala
Last active December 14, 2015 04:49
ドラクエ7のバロックタワーのパズルを解くためのコード。
// コードを読みやすいように名前をつけとこう.
val Up = 0
val Right = 1
val Down = 2
val Left = 3
type Direction = Int
/*
* 石像をあらわすcase class. 像の向きを状態として持つ.
* また、一回の動作で向きが90度回転するという振る舞いも定義.
// http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2025
// http://projecteuler.net/problem=25
val fib:Stream[BigInt] = BigInt(1) #:: fib.scanLeft(BigInt(1)){(a, b) => a + b }
def getDigits = (n: BigInt) => n.toString.size
fib.takeWhile(getDigits(_) <= 1000).zipWithIndex.find(t => getDigits(t._1) == 1000).get._2 + 1
@daiksy
daiksy / maxPrime.scala
Created December 19, 2012 00:38
ご祝儀用に予算内のMax素数を算出するコード
def maxPrime(n: Int) = {
def isPrime(n: Int) = Iterator.from(2).takeWhile(p => p * p <= n).forall(p => n % p != 0)
(1 to n).filter(isPrime).max
}
@daiksy
daiksy / problem20.scala
Created December 8, 2012 11:05
Project Euler Problem 20
(BigInt(100) to BigInt(1) by -1).reduce((x, y) => x * y).toString.toList.map(_.getNumericValue).sum