1 == 1
> true
1 == 1.0
> true
1 =:= 1.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// not tailrec... :( | |
def min(l: Seq[Int]): Int = l match { | |
case h :: t if t.isEmpty => h | |
case h :: t => min(t) match { | |
case r if h < r => h | |
case r => r | |
} | |
} | |
// tailrec! :) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def hsort(list: Seq[Int]): Seq[Int] = { | |
val l = new ArrayBuffer[Int]() | |
// 3 1 2 -1 4 10 0 | |
// 3 | |
// 1 3 2 | |
// -1 3 2 1 4 | |
// -1 3 0 1 4 10 2 | |
def push(n: Int) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.collection.immutable.{Stack, Queue} | |
import scala.annotation.tailrec | |
case class Graph(es: Map[Vertex, Seq[Edge]]) | |
case class Vertex(i: Int) | |
case class Edge(v: Vertex) | |
def sampleGraph: Graph = Graph( | |
Map( | |
Vertex(3) -> Seq(Edge(Vertex(1)), Edge(Vertex(2))), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.annotation.tailrec | |
import scala.collection._ | |
object Main { | |
def main(args: Array[String]) { | |
// 1マスずつ進みながら、上下左右のマスでゴールに近い順に探索していく | |
val map = """#S######## | |
|# #### G |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.annotation.tailrec | |
/* | |
[C] >2 [A] >2 [E] | |
\ >1 [D] | |
\ >1 | |
\ \ | |
>1 [B] >6 [F] | |
C>A>F が最短 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.annotation.tailrec | |
object Main { | |
def main(args: Array[String]) { | |
bellmanFord(sampleGraph, Vertex("C")) | |
.toSeq.sortBy(_._2).map(println) | |
} | |
def bellmanFord(graph: Graph, start: Vertex): ShortestPath = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.annotation.tailrec | |
object Main { | |
def main(args: Array[String]) { | |
val s = 3 | |
val m = Vector.fill(s, s)(0) | |
val r = floid(makeData(m)) | |
printMatrix(r) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.annotation.tailrec | |
object Main { | |
def main(args: Array[String]) { | |
prim(sampleGraph).foreach(println) | |
} | |
// 参考 : http://ja.wikipedia.org/wiki/%E3%83%97%E3%83%AA%E3%83%A0%E6%B3%95 | |
// 例のグラフもwikipediaのものと同じに。 | |
def prim(g :Graph): Seq[Edge] = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 複数スレッドで++していくとスレッドセーフでないので値がおかしくなる | |
var a = 0 | |
(0 until 10000).par.foreach { _ => a += 1 } | |
println(a) // 6772 | |
// synchronizedを使っても可。 | |
var b = 0 | |
(0 until 10000).par.foreach { _ => synchronized { b += 1 } } | |
println(b) // 10000 |