Skip to content

Commit b464449

Browse files
committed
Add cats tarai func
1 parent 6e32c1c commit b464449

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

build.sbt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ scalacOptions ++= Seq(
1313
"-Xlint" // その他、望ましい書き方や落とし穴についての情報
1414
// "-Xfatal-warnings" // 警告をエラーとして扱う
1515
)
16+
17+
// cats-core: Most core type classes and functionality (required).
18+
libraryDependencies += "org.typelevel" %% "cats-core" % "1.0.1"

src/main/scala/BenchMark.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ object BenchMark extends App {
44

55
val measureCompareTime = BenchMarkUtils.compareMeasure _
66

7-
Util.regularExpression()
7+
8+
Util.taraiFunc()
9+
// Util.regularExpression()
810

911
/*
1012
Collection.sizeOrLength()

src/main/scala/Util.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import cats.{Now, Eval}
2+
3+
import cats.data._
4+
import cats.implicits._
5+
16
import services.BenchMarkUtils
27

38
object Util {
@@ -26,4 +31,21 @@ object Util {
2631
measureCompareTime("findAllIn and positive after read", () => (0 to 100).foreach(_ => xsFunc()))("findPrefixOf ", () => (0 to 100).foreach((_ => ysFunc())))
2732
}
2833

34+
// たらい回し関数のチェック
35+
def tarai(x: Int, y: Int, z: Int): Int =
36+
if (x <= y) y else tarai(tarai(x - 1, y, z),
37+
tarai(y - 1, z, x),
38+
tarai(z - 1, x, y))
39+
40+
def taraiE(x: Eval[Int], y: Eval[Int], z: Eval[Int]): Eval[Int] =
41+
(x, y).mapN(_ <= _) ifM (y, taraiE(taraiE(x.map(_ - 1), y, z),
42+
taraiE(y.map(_ - 1), z, x),
43+
taraiE(z.map(_ - 1), x, y)))
44+
45+
def taraiFunc(): Unit = {
46+
// tarai(20, 10, 5)
47+
measureCompareTime("tarai func ", () => tarai(20, 10, 5))("taraiE ", () => taraiE(Now(20), Now(20), Now(5)).value )
48+
}
49+
50+
2951
}

0 commit comments

Comments
 (0)