Created
February 29, 2012 01:45
-
-
Save rirakkumya/1936875 to your computer and use it in GitHub Desktop.
scala-ioとscalacheckでランダムデータを作っちゃうコード
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
resolvers += "Scala Tools Snapshots" at "http://scala-tools.org/repo-snapshots/" | |
libraryDependencies += "org.scalaz" %% "scalaz-core" % "6.0.4" | |
libraryDependencies += "com.github.scala-incubator.io" %% "scala-io-core" % "0.3.0" | |
libraryDependencies += "com.github.scala-incubator.io" %% "scala-io-file" % "0.3.0" | |
libraryDependencies += "org.scala-tools.testing" %% "scalacheck" % "1.9" | |
scalaVersion := "2.9.1" |
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
import scalax.io._ | |
import scalax.file.Path | |
import Path.string2path | |
import org.scalacheck.Gen | |
import scalaz._ | |
import Scalaz._ | |
trait Serial { def toSerial:String } | |
case class Head(num:Int) extends Serial { | |
def toSerial = "H%05d" format num | |
} | |
case class Body(dataType:String, data01:String, data02:String, data03:Int) extends Serial { | |
def toSerial = (dataType :: data01 :: data02 :: ("%03d" format data03) :: Nil) mkString "" | |
} | |
case class Foot(cnt:Int) extends Serial { | |
def toSerial = "F%04d" format cnt | |
} | |
object MkTestFile extends App { | |
val dir = "./data" | |
val now_date = "%tY%<tm%<td_%<tH%<tM%<tS" format new java.util.Date | |
val output = "%s/data%s.txt" format (dir, now_date) | |
val head = for{x <- Gen.choose(1,1000)} yield Head(x) | |
val dataType = ('1' to '9') ++ ('A' to 'Z') | |
val data01 = ('A' to 'C') map ("00000%s" format _) | |
val body = for{ | |
rType <- Gen.oneOf(dataType) | |
rStr1 <- Gen.oneOf(data01) | |
rStr2 <- Gen.alphaStr | |
rNum <- Gen.choose(0,999) | |
} yield Body(rType.toString, rStr1, "%-3s" format rStr2.take(3), rNum) | |
val cnt = Gen.choose(1,1000).sample | 1 | |
head.sample <|*|> Gen.listOfN(cnt,body).sample match { | |
case Some((h,b)) => | |
output.write((h :: b ::: Foot(cnt) :: Nil) map (_.toSerial + "\n") mkString "") | |
case None => println("Failed to create file") | |
} | |
} |
Author
rirakkumya
commented
Feb 29, 2012
- sbt install (http://code.google.com/p/simple-build-tool/wiki/Setup)
- sbt-luncher.jarをdownload
- sbtの起動shell作成
- git clone git://gist.github.com/1936875.git gist-1936875
- cd gist-1936875
- sbt run
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment