Created
April 12, 2012 17:00
-
-
Save takungsk/2369143 to your computer and use it in GitHub Desktop.
合成関数で 最長重複文字列問題をとく
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
//以前の最長文字列問題を 合成関数を使うように変更したバージョン | |
// mississipi という文字列の中の最長重複文字を探す | |
package takuya71 | |
import takuya71.ExFunction._ | |
class ExFunction[-ARG, +RET](f: ARG => RET) { | |
def :>:[A](g: A => ARG): A => RET = f compose g | |
def :*:(arg: ARG): RET = f(arg) | |
} | |
object ExFunction { | |
implicit def funtionToEx[A, B](f: A => B) = new ExFunction(f) | |
} | |
object Main extends App{ | |
// 文字列を定義 | |
val moji = "mississippi" | |
// 接尾辞リストを作る | |
val tailslist: String => List[String] = {s => s.tails.toList} | |
// リストをソートする | |
val sort: List[String] => List[String] = { s => s.sorted } | |
// 隣り合う要素を組にする | |
val makepair: List[String] => List[(String, String)] = {s => s.zip(s.tail)} | |
// 共通部部の長さをだす | |
val comlen: Tuple2[String, String] => Int = {t => {t.zipped.takeWhile{case (a,b) => a == b}.size}} | |
// 共通部分が一番長い要素と取り出す | |
val chooseMax: List[(String,String)] => Tuple2[Int,String] = {s => {s.map{t => (comlen(t),t._1)}.maxBy(_._1)}} | |
// 共通部分のみ残す | |
val extract: Tuple2[Int, String] => String = {t => {t._2.take(t._1)}} | |
// 文字列出力 | |
val putStr: String => Unit = {s => println(s)} | |
// つなげて出力 | |
moji :*: tailslist :>: sort :>: makepair :>: chooseMax :>: extract :>: putStr | |
} | |
// 実行結果 | |
/* | |
> run | |
[info] Running takuya71.Main | |
issi | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment