Advanced Object Oriented Programming in Scala
People often associate "Advanced" Scala with pure, monadic functional programming, and "Object Oriented" Scala with newbies fresh from a Java
>>> def foo(): | |
... x = 1 | |
... y = (x, lambda: x) | |
... return (x, y) | |
... | |
>>> foo()[1][1]() | |
1 |
>>> class Foo: | |
... x = 1 | |
... y = x | |
... print y | |
... class Bar: | |
... z = y | |
... print z | |
... | |
1 | |
Traceback (most recent call last): |
>>> a = 1 | |
>>> class Foo: | |
... a = 2 | |
... print a | |
... def bar(self): | |
... print a | |
... | |
2 | |
>>> Foo().bar() | |
1 |
package main.scala | |
trait Enum[T]{ self => | |
type E = this.type => T | |
implicit class k(e: E) extends Function0[T]{ | |
def apply() = e(self) | |
} | |
} |
def outer(x): | |
def inner(): | |
print x | |
inner() | |
print x | |
outer(10) # 10 10 | |
def outer(x): |
var x | |
println("reset{} " + | |
reset{ | |
shift{ cont: (Int => Int) => | |
println("cont(1): " + cont(1)) | |
println("cont(2): " + cont(2)) | |
5 | |
} |
case class Thing(s: String){ | |
def :=(t: Thing) = s + ": " + t.s | |
} | |
val float = Thing("float") | |
val left = Thing("left") | |
val backgroundImage = Thing("background-image") | |
def radialGradient(s: String) = Thing(s"radialGradient($s)") | |
val red = Thing("red") | |
val blue = Thing("red") |
@ load.ivy("com.amazonaws" % "aws-java-sdk" % "1.10.+") | |
:: loading settings :: url = jar:file:/Users/haoyi/amm!/org/apache/ivy/core/settings/ivysettings.xml | |
:: resolving dependencies :: com.amazonaws#aws-java-sdk-caller;working | |
confs: [default] | |
found com.amazonaws#aws-java-sdk;1.10.41 in central | |
[1.10.41] com.amazonaws#aws-java-sdk;1.10.+ | |
found com.amazonaws#aws-java-sdk-support;1.10.41 in central | |
found com.amazonaws#aws-java-sdk-core;1.10.41 in central | |
found commons-logging#commons-logging;1.1.3 in central | |
found org.apache.httpcomponents#httpclient;4.3.6 in central |
haoyi-mbp:Ammonite haoyi$ ~/amm | |
Loading... | |
Welcome to the Ammonite Repl 0.5.2 | |
(Scala 2.11.7 Java 1.8.0_25) | |
haoyi-Ammonite@ load.ivy("org.scalaj" %% "scalaj-http" % "2.2.0") | |
haoyi-Ammonite@ import ammonite.ops._, scalaj.http._ | |
import ammonite.ops._, scalaj.http._ | |
haoyi-Ammonite@ Http("https://api.github.com/repos/scala/scala").asString | |
res2: HttpResponse[String] = HttpResponse( |
Advanced Object Oriented Programming in Scala
People often associate "Advanced" Scala with pure, monadic functional programming, and "Object Oriented" Scala with newbies fresh from a Java