Skip to content

Instantly share code, notes, and snippets.

@rirakkumya
Created February 29, 2012 01:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rirakkumya/1936875 to your computer and use it in GitHub Desktop.
Save rirakkumya/1936875 to your computer and use it in GitHub Desktop.
scala-ioとscalacheckでランダムデータを作っちゃうコード
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"
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")
}
}
@rirakkumya
Copy link
Author

  1. sbt install (http://code.google.com/p/simple-build-tool/wiki/Setup)
    • sbt-luncher.jarをdownload
    • sbtの起動shell作成
  2. git clone git://gist.github.com/1936875.git gist-1936875
  3. cd gist-1936875
  4. sbt run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment