2017-05-06から1日間の記事一覧

sample code spiral

Code Examples for Programming in Scala Apache License, Version 2.0 import Element.elem object Main extends App { val nSides = 100 println(Spiral.spiral(nSides, 0)) } object Spiral { val space = elem(" ") val corner = elem("+") def spiral(n…

カリー化

object Main extends App { def plainSum(x: Int, y: Int): Int = x + y def curriedSum(x: Int)(y: Int): Int = x + y def first(x: Int): (Int) => Int = (y: Int) => x + y def first1: (Int) => Int = first(1) def first2: (Int) => Int = curriedSum(1)…

GoFパターンをなるべく短文で説明を求められたとき用

Iteratorパターン・・・Iterator[hasNext, next] Adapter・・・異なるインターフェースのオブジェクトを、同じインターフェースで扱う TemplateMethod・・・abstract method FactoryMethod・・・FactoryオブジェクトがProductオブジェクトを作るという話 Sin…

Scala標準APIを利用したリファクタリング例

object Main extends App { def containsNeg1(nums: List[Int]): Boolean = { var exists = false for (num <- nums) if (num < 0) exists = true exists } def containsNeg2(nums: List[Int]): Boolean = nums.exists(_ < 0) def containsOdd1(nums: List[I…

Closureを利用したリファクタリング例

import java.io.File object Main extends App { } object FileMatcher { private def filesHere: Array[File] = new File(".").listFiles def filesEnding(query: String) = for (file <- filesHere; if file.getName.endsWith(query)) yield file def file…

末端再帰

末端再帰に持ち込めれば、「whileループによる実装と同じパフォーマンスまで最適化」を受けることができる。 末端再帰の最適化をオフにするコンパイラオプションは、 -g:notailcalls ただし、Scalaの現状の最適化は、「同じメソッドに対して関数オブジェクト…