" format i) + "\""
+ def apply() = {
+{header}
/** <p>
- * Function with {i} parameters.
+ * Function with {i} parameter{s}.
* </p>
- * {descriptiveComment(i)}
+ * {descriptiveComment}
*/
-trait {functionClassname(i)}{__typeArgs__} extends AnyRef {{ self =>
- def apply({__funArgs__}): R
- override def toString() = "<function>"
- {moreMethods(i)}
+trait {className}{contraCoArgs} extends AnyRef {{ self =>
+ def apply({funArgs}): R
+ override def toString() = {toStr}
+ {moreMethods}
}}
}
+ // (x1: T1) => (x2: T2) => (x3: T3) => (x4: T4) => apply(x1,x2,x3,x4)
+ def shortCurry = {
+ val body = xdefs.mkString("apply(", ",", ")")
+ List.map2(xdefs, targs)("(%s: %s) => ".format(_, _)).mkString("", "", body)
+ }
- def moreMethods(i:Int) = i match {
- case 1 => """
- /** (f compose g)(x) == f(g(x))
- */
- def compose[A](g: A => T1): A => R = { x => apply(g(x)) }
+ // (x1: T1) => ((x2: T2, x3: T3, x4: T4, x5: T5, x6: T6, x7: T7) => self.apply(x1,x2,x3,x4,x5,x6,x7)).curry
+ def longCurry = (List.map2(xdefs, targs)(_ + ": " + _) drop 1).mkString(
+ "(x1: T1) => ((",
+ ", ",
+ ") => self.apply(%s)).curry".format(xdefs mkString ",")
+ )
- /** (f andThen g)(x) == g(f(x))
+ // f(x1,x2,x3,x4,x5,x6) == (f.curry)(x1)(x2)(x3)(x4)(x5)(x6)
+ def curryComment = { """
+ /** f(%s) == (f.curry)%s
*/
- def andThen[A](g: R => A): T1 => A = { x => g(apply(x)) }
-"""
- case _ if (i > 1) => """
- /** f(""" + join(",", (1 to i).map(i => "x" + i)) + """) == (f.curry)""" + join("", (1 to i).map(i => "(x" + i + ")")) + """
- */
- def curry: """ + join(" => ", (1 to i).map(i => "T" + i)) + """ => R = {
-""" +
- (if (i < 5) {
- " " + join(" => ", (1 to i).map(i => "(x" + i + ": T" + i + ")")) + """ => apply(""" + join(",", (1 to i).map(i => "x" + i)) + ")\n }"
- } else {
- """ (x1: T1) => ((""" + join(", ", (2 to i).map(i => "x" + i + ": T" + i)) + """) => self.apply(""" + join(",", (1 to i).map(i => "x" + i)) + ")).curry\n }"
- })
- case _ => """
-"""
-
+""".format(xdefs mkString ",", xdefs map ("(" + _ + ")") mkString)
}
- def descriptiveComment(i: Int) = i match {
- case 0 => """
- In the following example the definition of
- * currentSeconds
is a shorthand for the anonymous class
- * definition anonfun0
:
- *
- *
- * object Main extends Application {
- *
- * val currentSeconds = () => System.currentTimeMillis() / 1000L
- *
- * val anonfun0 = new Function0[Long] {
- * def apply(): Long = System.currentTimeMillis() / 1000L
- * }
- *
- * println(currentSeconds())
- * println(anonfun0())
- * }
"""
- case 1 => """
- In the following example the definition of
- * succ
is a shorthand for the anonymous class definition
- * anonfun1
:
- *
- *
- * object Main extends Application {
- *
- * val succ = (x: Int) => x + 1
- *
- * val anonfun1 = new Function1[Int, Int] {
- * def apply(x: Int): Int = x + 1
- * }
- *
- * println(succ(0))
- * println(anonfun1(0))
- * }
"""
- case 2 => """
- In the following example the definition of
- * max
is a shorthand for the anonymous class definition
- * anonfun2
:
- *
- *
- * object Main extends Application {
- *
- * val max = (x: Int, y: Int) => if (x < y) y else x
- *
- * val anonfun2 = new Function2[Int, Int, Int] {
- * def apply(x: Int, y: Int): Int = if (x < y) y else x
- * }
- *
- * println(max(0, 1))
- * println(anonfun2(0, 1))
- * }
"""
- case _ => ""
+
+ def curryMethod = {
+ val body = if (i < 5) shortCurry else longCurry
+ " def curry: %s => R = {\n %s\n }\n".format(
+ targs mkString " => ", body
+ )
}
-} // object FunctionFile
+ override def moreMethods = curryComment + curryMethod
+} // object Function
+
+
/* zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
T U P L E
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz */
-object TupleFile {
- import genprod._
- def make(i: Int) = {
- val __typeArgs__ = covariantArgs(i).mkString("[",", ","]")
- val __typeArgRefs__ = targs(i).mkString("[",", ","]")
- val __fields__ = fields(i)
-
-/* __ *\
-** ________ ___ / / ___ Scala API **
-** / __/ __// _ | / / / _ | (c) 2002-2009, LAMP/EPFL **
-** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
-** /____/\___/_/ |_/____/_/ | | **
-** |/ **
-\* */
+object Tuple
+{
+ def make(i: Int) = apply(i)()
+ def apply(i: Int) = i match {
+ case 2 => TupleTwo
+ case _ => new Tuple(i)
+ }
+}
-// {"$Id$"}
+object TupleTwo extends Tuple(2)
+{
+ override def moreMethods = """
+ /** Swap the elements of the tuple */
+ def swap: Tuple2[T2,T1] = Tuple2(_2, _1)
+"""
+}
-// generated by genprod on {new java.util.Date().toString()} {if(descriptiveComment(i).length > 0) "(with fancy comment)" else ""} {if(moreMethods(i).length > 0) "(with extra methods)" else ""}
+class Tuple(val i: Int) extends Group("Tuple") with Arity
+{
+ // prettifies it a little if it's overlong
+ def mkToString() = {
+ def str(xs: List[String]) = xs.mkString(""" + ", " + """)
+ if (i <= MAX_ARITY / 2) str(mdefs)
+ else {
+ val s1 = str(mdefs take (i / 2))
+ val s2 = str(mdefs drop (i / 2))
+ s1 + " +\n \", \" + " + s2
+ }
+ }
-package scala
+ def apply() = {
+{header}
-/** {tupleClassname(i)} is the canonical representation of a @see {productClassname(i)}
- * {descriptiveComment(i)}
+/** {className} is the canonical representation of a @see {Product.className(i)}
+ * {descriptiveComment}
*/
-case class {tupleClassname(i)}{__typeArgs__}({ __fields__ })
- extends {productClassname(i)}{__typeArgRefs__} {{
-
- override def toString() = {{
- val sb = new StringBuilder
- { if ({i} == 1)
- "sb.append('(').append(_" + {i} + ").append(\",)\")"
- else {
- val xs = (1 to i).map(i => ".append(_" + i + ")")
- xs.mkString("sb.append('(')", ".append(',')",".append(')')")
- }
- }
- sb.toString
- }}
- {moreMethods(i)}
+case class {className}{covariantArgs}({fields})
+ extends {Product.className(i)}{invariantArgs}
+{{
+ override def toString() = "(" + {mkToString} + ")"
+ {moreMethods}
}}
}
- def moreMethods(i:Int) = i match {
- case 2 => """
- /** Swap the elements of the tuple */
- def swap: Tuple2[T2,T1] = Tuple2(_2, _1)
-"""
- case _ => ""
- }
- def descriptiveComment(i: Int) = i match {
- case _ => ""
- }
-} // object TupleFile
-
+} // object Tuple
/* zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
P R O D U C T
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz */
-object ProductFile {
- import genprod._
- def make(i:Int) = {
- val __typeArgs__ = if (i==0) Nil else covariantArgs(i).mkString("[",", ","]")
- val __refArgs__ = if (i==0) Nil else targs(i).mkString("[",", ","]")
-
-/* __ *\
-** ________ ___ / / ___ Scala API **
-** / __/ __// _ | / / / _ | (c) 2002-2009, LAMP/EPFL **
-** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
-** /____/\___/_/ |_/____/_/ | | **
-** |/ **
-\* */
-
-// {"$Id$"}
-
-// generated by genprod on {new java.util.Date().toString()} {if(descriptiveComment(i).length > 0) "(with fancy comment)" else ""} {if(moreMethods(i).length > 0) "(with extra methods)" else ""}
+object Product extends Group("Product")
+{
+ def make(i: Int) = apply(i)()
+ def apply(i: Int) = new Product(i)
+}
-package scala
+class Product(val i: Int) extends Group("Product") with Arity
+{
+ def cases = {
+ val xs = for ((x, i) <- mdefs.zipWithIndex) yield "case %d => %s".format(i, x)
+ val default = "case _ => throw new IndexOutOfBoundsException(n.toString())"
+ "\n" + ((xs ::: List(default)) map (" " + _ + "\n") mkString)
+ }
+ def proj = {
+ List.map2(mdefs, targs)(
+ " /** projection of this product */\n def %s: %s\n\n".format(_, _)
+ ) mkString
+ }
+ def apply() = {
+{header}
import Predef._
-object {productClassname(i)} {{
- def unapply{__refArgs__}(x: {productClassname(i)}{__refArgs__}): Option[{productClassname(i)}{__refArgs__}] =
+object {className} {{
+ def unapply{invariantArgs}(x: {className}{invariantArgs}): Option[{className}{invariantArgs}] =
Some(x)
}}
-/** {productClassname(i)} is a cartesian product of {i} components
- * {descriptiveComment(i)}
+/** {className} is a cartesian product of {i} component{s}.
+ * {descriptiveComment}
*/
-trait {productClassname(i)}{__typeArgs__} extends Product {{
-
+trait {className}{covariantArgs} extends Product {{
/**
* The arity of this product.
* @return {i}
@@ -340,21 +334,11 @@ trait {productClassname(i)}{__typeArgs__} extends Product {{
* @throws IndexOutOfBoundsException
*/
@throws(classOf[IndexOutOfBoundsException])
- override def productElement(n: Int) = n match {{
- {for ((m, j) <- mdefs(i).zip(List.range(0, i)))
- yield "case "+j+" => "+m+"\n "}case _ => throw new IndexOutOfBoundsException(n.toString())
- }}
-
- {for ((m, t) <- mdefs(i) zip targs(i)) yield
- "/** projection of this product */\n def " + m + ": " + t + "\n\n" }
+ override def productElement(n: Int) = n match {{ {cases} }}
- {moreMethods(i)}
+{proj}
+{moreMethods}
}}
}
- def moreMethods(i:Int) = i match {
- case _ => ""
- }
- def descriptiveComment(i: Int) = i match {
- case _ => ""
- }
-};
+
+}
\ No newline at end of file
--
cgit v1.2.3