tomo cocoa avatar tomo cocoa committed e5efd3d

cleaned code up

Comments (0)

Files changed (1)

src/main/scala/net/elliptium/Scalazsample.scala

 package net.elliptium
 
 import scalaz.Functor
-// import scalaz.Scalaz._
+import language.implicitConversions
+
+/*
+ * 可換図式
+ * 
+ *               lift(length)
+ * Important[String] → Important[Int]
+ *        ↑                ↑
+ *        ↑                ↑
+ *        ↑                ↑
+ *      String       →     Int
+ *                 length
+ */
 
 
 /**
 
 }
 
+/**
+ * Functor#map の実装
+ */
 class ImportantFunctor[A, B] extends Functor[Important] {
 
   def map[A, B](fa: Important[A])(f: A => B): Important[B] = new Important[B](f(fa.value))
 
 }
 
-/*
- *               lift(length)
- * Important[String] → Important[Int]
- *        ↑                ↑
- *        ↑                ↑
- *        ↑                ↑
- *      String       →     Int
- *                 length
- */
-
 object Scalazsample extends App {
 
   def length(value: String): Int = value.length
 
   val importantFunctor = new ImportantFunctor[String, Int]
-  def liftedLength: Important[String] => Important[Int] =
-    importantFunctor.lift[String, Int](length(_))
-
 
   val message = "3分間だけ待ってやる"
   val importantMessage = new Important[String](message)
-
   println(importantMessage.toString)
 
   println("大事なことなので二回繰り返しました。")
 
-  println("ちゃんと 2 つ値があります。" +
+  println("importantMessage にはちゃんと 2 つ値があります。" +
     " _1: \"" + importantMessage._1 + "\", _2: \"" + importantMessage._2 + "\"")
 
   println("ちなみにセリフの文字数は " + length(message).toString + " 文字でした。")
+  println()
 
+  // length を Important に持ち上げた射を適用します
   val importantLength = importantFunctor.map[String, Int](importantMessage)(length(_))
 
   println("importantLength: " + importantLength)
   println("文字数も大事なので二回繰り返しました。")
+  println()
 
+  // より自然な lift で length を持ち上げることもできます
+  def liftedLength: Important[String] => Important[Int] =
+    importantFunctor.lift[String, Int](length(_))
   val importantLength2 = liftedLength(importantMessage)
 
   println("liftedLength でも同じ結果になります。")
   println("importantLength2: " + importantLength2)
+
 }
-
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.