Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactor into thinkstats

Signed-off-by: fedesilva <fede.silva@gmail.com>
  • Loading branch information...
commit 81f08a7c3c9bb84afafb9f47148d5cb8bb75f7f8 1 parent 586d65a
@fedesilva authored
Showing with 17 additions and 14 deletions.
  1. +17 −14 src/main/scala/{ → thinkstats}/survey.scala
View
31 src/main/scala/survey.scala → src/main/scala/thinkstats/survey.scala
@@ -9,10 +9,11 @@ import language.{
dynamics
}
-/*
+
+/*
Data Access machinery.
-
+
This file is a port from original code for "Think Stats" by Allen B. Downey.
### Original disclaimer
@@ -45,11 +46,12 @@ object Field {
}
-object Builders {
+object Converters {
+
+ def parseDate(d:String) = new DateTime(d)
+ def parseInt(n:String) = catching(classOf[NumberFormatException]).opt(n.trim.toInt).getOrElse(0)
+ def parseDouble(n:String) = catching(classOf[NumberFormatException]).opt(n.trim.toDouble).getOrElse(0D)
- val parseDate = (d:String) => new DateTime(d)
- val parseInt = (n:String) => catching(classOf[NumberFormatException]).opt(n.trim.toInt).getOrElse(0)
- val parseDouble = (n:String) => catching(classOf[NumberFormatException]).opt( n.trim.toDouble ).getOrElse(0D)
}
@@ -57,10 +59,12 @@ abstract class Table[R]( val fileName: String ) {
val fields: Vector[Field]
-
-
- /** The data */
- def rows(path: String) = read( path+"/"+fileName )
+ /** Computes a lazy record sequence
+ *
+ * @param path
+ * @return
+ */
+ def records(path: String) = read( path+"/"+fileName )
.map( parseLine )
.map( make )
@@ -100,8 +104,6 @@ abstract class Table[R]( val fileName: String ) {
}
-trait Record extends Dynamic
-
case class Pregnancy(
caseid: Int,
@@ -119,7 +121,7 @@ case class Pregnancy(
object Pregnancy extends Table[Pregnancy]("2002FemPreg.dat"){
import Field._
- import Builders._
+ import Converters._
val fields: Vector[Field] = Vector(
("caseid", 1, 12),
@@ -136,6 +138,8 @@ object Pregnancy extends Table[Pregnancy]("2002FemPreg.dat"){
/** Builds an `R` from a vector of values.
*
+ * FIXME Must make a macro quickly.
+ *
* @param data the ordered values.
* @return a new Pregnancy instance.
*/
@@ -155,4 +159,3 @@ object Pregnancy extends Table[Pregnancy]("2002FemPreg.dat"){
}
-
Please sign in to comment.
Something went wrong with that request. Please try again.