Programming is the art of telling another human what one wants the computer to do- Donald Knuth
Daniel - Briton Rivière (1872)
|
val
final
?
var r = double(1) + triple(1);
function double(x) {return x * 2;}
var triple = function(x) {return x * 3;}
TypeError: triple is not a function
class Vals {
val r = double(1) + triple(1)
def double(x: Int) = x * 2
val triple = (x: Int) => x * 3
}
java.lang.NullPointerException
at Vals.<init>(Vals.scala:2)
class Vals {
println(answer)
private[this] val answer = 42
}
return
in lambda
def samples(xs: TraversableOnce[Int]) {
xs map {x => return x * 10} foreach println
}
Never prints anything
The Scala Language Specification
6.20 Return Expressions
java.util.ArrayList
scala.collection.immutable.List
Source: github.com/tpolecat/examples/raw/master/doc/List.pdf
val p = Seq(1,2,3).permutations
if (p.size < 10) {
//p.size is 6
p foreach println
}
Never prints anything
map
in Clojure
(defn map [f coll]
(lazy-seq
(if-not (empty? coll)
(cons
( f (first coll))
(map f (rest coll))))))
map :: (a -> b) -> [a] -> [b]
map _ [] = []
map f (x:xs) = f x : map f xs
def map[B, That](f: A => B)
(implicit bf: CanBuildFrom[Repr, B, That]): That = {
def builder = {
val b = bf(repr)
b.sizeHint(this)
b
}
val b = builder
for (x <- this)
b += f(x)
b.result
}
def repr: Repr = this.asInstanceOf[Repr]
T
, +T
and -T
)new Foo {...}
)x: {def y: Int}
)a.B
)@specialized
)this =>
)A#B
)M[_]
)<:
, >:
)=:=
, <:<
and <%<
)type T
)type T = Int
) (implicit ...)
)<%
)* => *
)M[T <: M[T]]
)[error] C:\Work\CompStak\CompStak-services\persistence\src\main\scala\com\compstak\persistence\Tables.scala:1628: type mismatch; [error] found : scala.slick.collection.heterogenous.HCons[Long,scala.slick.collection.heterogenous.HCons[Long,scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[java.sql.Timestamp],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[String,scala.slick.collection.heterogenous.HCons[Int,scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[Long],scala.slick.collection.heterogenous.HCons[String,scala.slick.collection.heterogenous.HCons[Option[...],scala.slick.collection.heterogenous.HCons[...,...]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] [error] required: Tables.this.CompProposalRawRow [error] (which expands to) scala.slick.collection.heterogenous.HCons[Long,scala.slick.collection.heterogenous.HCons[Long,scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[java.sql.Timestamp],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[String,scala.slick.collection.heterogenous.HCons[Int,scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[String],scala.slick.collection.heterogenous.HCons[Option[Long],scala.slick.collection.heterogenous.HCons[String,scala.slick.collection.heterogenous.HCons[Option[...],scala.slick.collection.heterogenous.HCons[...,...]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] [error] <<[Long] :: <<[Long] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[java.sql.Timestamp] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<[String] :: <<[Int] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[Long] :: <<[String] :: <<?[Long] :: <<?[Long] :: <<?[Int] :: <<?[Int] :: <<?[String] :: <<?[Boolean] :: <<[Boolean] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<[Boolean] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[String] :: <<?[Long] :: <<?[String] :: <<?[String] :: HNil [error] ^
Source: groups.google.com/d/topic/scalaquery/GIptm2LUExg/discussion
def f(x: Int) {}
def f(s: String) {}
Seq(1, 2) foreach f
def f(x: => Int) {}
def f(s: => String) {}
Seq(1, 2) foreach f
(defmacro log-expr [s]
`(println (str "Value of " '~s " is " ~s)))
user=> (log-expr (inc 2))
Value of (inc 2) is 3
object LogMacros {
import language.experimental.macros
import reflect.macros.whitebox.Context
def logExpr(s: Any) = macro logExprInternal
private def logExprInternal(c: Context)
(exp: c.Expr[Any]): c.Expr[Unit] = {
import c.universe._
val text = show(exp.tree)
val literal = Literal(Constant(text))
val str = c.Expr[String](literal)
reify {
println(s"Value of ${str.splice} is ${exp.splice}")
}
}
}
seq foreach println
seq.foreach{println}
seq.foreach(println)
seq.foreach{println(_)}
seq.foreach(println(_))
seq.foreach(x => println(x))
seq.foreach(x: Int => println(x))
seq foreach{x: Int => println(x)}
seq.foreach{x: Int => println(x)}
seq foreach{case x => println(x)}
seq.foreach{case x => println(x)}
seq foreach{case x: Int => println(x)}
seq.foreach{case x: Int => println(x)}
object
almost, but not quite, entirely unlike singleton
object
do's
object Util {
val answer = 42
def sinDeg(angleDeg: Double) = math.sin(angleDeg * math.Pi / 180.0)
def props(target: ActorRef) =
Props(new MyActor(target)).withMailbox("my-mailbox")
}
object
dont's
object Util {
def query(sql: Query) = //...
def sendTweet(msg: String) {/*...*/}
def logWarn(event: LogEvent) {/*...*/}
def randBool() = math.random < 0.5
}
public static Date extract(Object obj) throws ParseException {
long time;
if (obj instanceof Date) {
time = ((Date) obj).getTime();
} else if (obj instanceof Long) {
time = (Long)obj;
} else if (obj instanceof Calendar) {
time = ((Calendar)obj).getTimeInMillis();
} else if (obj instanceof String) {
time = format.parse((String) obj).getTime();
} else {
throw new IllegalArgumentException("Unrecognized: " + obj);
}
return new Date(time);
}
def extract(obj: Any): Date = {
val time = obj match {
case d: Date => d.getTime
case x: Long => x
case c: Calendar => c.getTimeInMillis
case s: String => format.parse(s).getTime
case _ =>
throw new IllegalArgumentException(s"Unrecognized: $obj")
}
new Date(time)
}
case class MSet(s: Set[Int])
case class MSeq(s: Seq[Int])
case class MMap(m: Map[Int, Int])
val msg = MSeq(new collection.mutable.ArrayBuffer())
import collection.immutable.Seq
Daniel's Answer to the King - Briton Rivière (1890)