Skip to content

Commit

Permalink
Wrappers for TupleN, adding a fold.
Browse files Browse the repository at this point in the history
scala> (1, 2).fold(_ + _)
res1: Int = 3

scala> (1, 2).fold(Tuple2.apply)
res2: (Int, Int) = (1,2)
  • Loading branch information
retronym committed Jun 27, 2010
1 parent 45c6978 commit 8f3b315
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion core/src/main/scala/scalaz/Identity.scala
Expand Up @@ -154,7 +154,7 @@ sealed trait Identity[A] extends PimpedType[A] {
/**
* @return the result of pf(value) if defined, otherwise the the Zero element of type B.
*/
def matchOrZero[B: Zero](pf: PartialFunction[A, B]) = ~pf.lift(value)
def matchOrZero[B: Zero](pf: PartialFunction[A, B]): B = ~pf.lift(value)

@tailrec
final def doWhile(f: A => A, p: A => Boolean): A = {
Expand Down
1 change: 1 addition & 0 deletions core/src/main/scala/scalaz/Scalaz.scala
Expand Up @@ -55,6 +55,7 @@ object Scalaz extends ScalazLow
with Streams
with Strings
with Trees
with TupleWs
with TreeLocs
with Validations
with Zeros
Expand Down
44 changes: 44 additions & 0 deletions project/build/Boilerplate.scala
@@ -0,0 +1,44 @@
import sbt._
import xsbt.FileUtilities.write

trait Boilerplate {
self: DefaultProject =>

def srcManagedScala = "src_managed" / "main" / "scala"

lazy val generateTupleW = {
val cleanSrcManaged = cleanTask(srcManagedScala) named ("clean src_managed")
task {
val arities = 2 to 22

def writeFileScalazPackage(fileName: String, source: String): Unit = {
val file = (srcManagedScala / "scalaz" / fileName).asFile
write(file, source)
}

for (arity: Int <- arities) {
val tupleWSource: String = {
val tparams = (0 until arity).map(n => ('A' + n).toChar).mkString(", ")
val params = (1 to arity).map("_" + _).mkString(", ")
"""|
|trait Tuple%dW[%s] extends PimpedType[Tuple%d[%s]] {
| def fold[Z](f: => (%s) => Z): Z = {import value._; f(%s)}
|}
|
|trait Tuple%dWs {
| implicit def ToTuple%dW[%s](t: (%s)): Tuple%dW[%s] = new { val value = t } with Tuple%dW[%s]
|}
|""".stripMargin.format(arity, tparams, arity, tparams, tparams, params, arity, arity,
tparams, tparams, arity, tparams, arity, tparams)
}

val source = "package scalaz\n\n" + tupleWSource
writeFileScalazPackage("Tuple%dWs.scala".format(arity), source)
}

val source = "package scalaz\n\n" + "trait TupleWs extends " + arities.map(n => "Tuple%dWs".format(n)).mkString("\n with ")
writeFileScalazPackage("TupleW.scala", source)
None
} dependsOn (cleanSrcManaged)
}
}
5 changes: 4 additions & 1 deletion project/build/ScalazProject.scala
Expand Up @@ -67,7 +67,10 @@ final class ScalazProject(info: ProjectInfo) extends ParentProject(info) with Ov

val parentPath = path _

class Core(info: ProjectInfo) extends ScalazDefaults(info)
class Core(info: ProjectInfo) extends ScalazDefaults(info) with Boilerplate {
override def mainSourceRoots = super.mainSourceRoots +++ srcManagedScala##
override def compileAction = super.compileAction dependsOn(generateTupleW)
}

class Http(info: ProjectInfo) extends ScalazDefaults(info) {
val servlet = "javax.servlet" % "servlet-api" % "2.5" withSources
Expand Down

0 comments on commit 8f3b315

Please sign in to comment.