1. tomo cocoa
  2. ScalazSample

Commits

tomo cocoa  committed ed87b2d

initial commit

  • Participants
  • Branches default

Comments (0)

Files changed (4)

File .hgignore

View file
  • Ignore whitespace
+syntax: glob
+
+.DS_Store
+*~
+*.bak
+*.orig
+target
+.history

File README

View file
  • Ignore whitespace
+A sample for the Typesafe Stack (http://typesafe.com/stack).
+
+Scala sample project using Scala and SBT.
+
+To run and test it use SBT invoke: 'sbt run'

File project/ScalazsampleBuild.scala

View file
  • Ignore whitespace
+// generated by giter8
+import sbt._
+import sbt.Keys._
+
+object ScalazsampleBuild extends Build {
+
+  lazy val scalazsample = Project(
+    id = "scalaz-sample",
+    base = file("."),
+    settings = Project.defaultSettings ++ Seq(
+      name := "scalaz-sample",
+      organization := "net.elliptium",
+      version := "0.1-SNAPSHOT",
+      scalaVersion := "2.10.0-M7",
+      // add other settings here
+
+      resolvers += "Typesafe Snapshots" at "http://repo.typesafe.com/typesafe/snapshots/",
+        
+      libraryDependencies ++= Seq(
+        "org.scalaz" % "scalaz-core" % "7.0.0-M3" cross CrossVersion.full
+      ),
+        
+      scalacOptions += "-feature",
+        
+      initialCommands in console := "import scalaz._, Scalaz._"
+    )
+  )
+}

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

View file
  • Ignore whitespace
+package net.elliptium
+
+import scalaz.Functor
+// import scalaz.Scalaz._
+
+
+/**
+ * case class にして諸々のメソッドの実装を省いている
+ */
+case class Important[T](val value: T) extends Product2[T, T] {
+
+  def _1: T = value
+  def _2: T = value
+
+  override def toString: String = value.toString + ", " + value.toString
+
+}
+
+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 つ値があります。" +
+    " _1: \"" + importantMessage._1 + "\", _2: \"" + importantMessage._2 + "\"")
+
+  println("ちなみにセリフの文字数は " + length(message).toString + " 文字でした。")
+
+  val importantLength = importantFunctor.map[String, Int](importantMessage)(length(_))
+
+  println("importantLength: " + importantLength)
+  println("文字数も大事なので二回繰り返しました。")
+
+  val importantLength2 = liftedLength(importantMessage)
+
+  println("liftedLength でも同じ結果になります。")
+  println("importantLength2: " + importantLength2)
+}
+