Free excerpt from TypeSafe (pdf): Scala for the Impatient
I discovered the special syntax for a field setter:
class Person {
private var privateAge = 0 // Make private and rename
def age = privateAge
def age_ = (newValue: Int) {| trait Huggable { | |
| self: Pet => | |
| def hug() :Unit = println("Dawww...!") | |
| } | |
| class Pet | |
| class Puppy extends Pet with Huggable |
| package app; | |
| object Bar { | |
| implicit val b: Bar = new Bar(5) | |
| } | |
| class Bar(val x: Int) { | |
| def doStuff() { | |
| println(x) | |
| } |
| // See: http://nurkiewicz.blogspot.com/2012/04/secret-powers-of-foldleft-in-scala.html | |
| def transformString(s: String, fns: List[Function1[String,String]]) :String = | |
| (s /: fns) { (s: String, f: Function1[String,String]) => f apply s } | |
| val toUpper = (s: String) => s.toUpperCase | |
| val reverse = (s: String) => s.reverse | |
| val disenvowel = (s: String) => s filterNot { Set('a','e','i','o','u') contains _ } | |
| // scala> transformString("blah",List(disenvowel)) | |
| // res175: String = blh |
| // Reference: http://www.cforcoding.com/2012/01/interview-programming-problems-done.html | |
| object PascalsTriangle { | |
| def nextrow(prev: List[Int]) :Stream[List[Int]] = { | |
| val curr = collection.mutable.ArrayBuffer(1) | |
| for(i <- 1 until prev.length) { | |
| curr += prev(i - 1) + prev(i) |
Free excerpt from TypeSafe (pdf): Scala for the Impatient
I discovered the special syntax for a field setter:
class Person {
private var privateAge = 0 // Make private and rename
def age = privateAge
def age_ = (newValue: Int) {| [noahlz:stringly]$ git checkout master | |
| Switched to branch 'master' | |
| [noahlz:stringly]$ git log -6 --oneline | |
| d230ce9 merge branch develop [no ci] | |
| 908e62b only build master | |
| 0416e6c Added a few words about Perforate to the readme. | |
| 621565e only build master | |
| 5832d26 Added a few words about Perforate to the readme. | |
| 3ca1764 Merge branch 'issues/6/upgrade-perforate' | |
| [noahlz:stringly]$ git reset --soft HEAD~3 |
| ====== Benching (Character/isLetter ^char c) ====== | |
| WARNING: Final GC required 3.956775511289807 % of runtime | |
| amd64 Linux 3.2.0-48-generic 2 cpu(s) | |
| Java HotSpot(TM) 64-Bit Server VM 23.21-b01 | |
| Runtime arguments: -XX:+TieredCompilation -XX:TieredStopAtLevel=1 -Dclojure.compile.path=/tmp/oneoff/classes -Doneoff.version=0.1 -Dfile.encoding=UTF-8 -Dclojure.debug=false | |
| Evaluation count : 5580540 in 60 samples of 93009 calls. | |
| Execution time sample mean : 11.046857 µs | |
| Execution time mean : 11.046903 µs | |
| Execution time sample std-deviation : 24.758483 ns | |
| Execution time std-deviation : 24.947300 ns |
| user=> (set! *warn-on-reflection* true) | |
| true | |
| user=> (defn is-letter? [c] (Character/isLetter c)) | |
| Reflection warning, NO_SOURCE_PATH:1:22 - call to isLetter can't be resolved. | |
| #'user/is-letter? | |
| user=> (defn is-letter? [^char c] (Character/isLetter c)) | |
| user=> CompilerException java.lang.IllegalArgumentException: Only long and double primitives are supported, compiling:(NO_SOURCE_PATH:1:1) | |
| ;; Ugh...no. |
| Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 08:51:28-0500) | |
| Maven home: /home/nzucker/java/maven-latest | |
| Java version: 1.6.0_31, vendor: Sun Microsystems Inc. | |
| Java home: /home/nzucker/java/jdk1.6.0_31/jre | |
| Default locale: en_US, platform encoding: UTF-8 | |
| OS name: "linux", version: "2.6.18-194.el5", arch: "amd64", family: "unix" | |
| [INFO] Error stacktraces are turned on. | |
| [DEBUG] Reading global settings from /home/nzucker/java/maven-latest/conf/settings.xml | |
| [DEBUG] Reading user settings from /home/nzucker/.m2/settings.xml | |
| [DEBUG] Using local repository at /home/nzucker/.m2/repository |
| user=> (concat '(1 2 3) '(4 5 6)) | |
| (1 2 3 4 5 6) | |
| user=> (conj '(1 2 3) '(4 5 6)) | |
| ((4 5 6) 1 2 3) | |
| user=> (cons '(1 2 3) '(4 5 6)) | |
| ((1 2 3) 4 5 6) | |
| user=> (list '(1 2 3) '(4 5 6)) |