Introduction to Developing in Scala
- Setup of tool chain
- Value Types
- Simple functions
| trait Foo[T] { | |
| def foo(t: T): String | |
| } | |
| // Creates an implicit function that uses an implicit Foo[T] (evidence) | |
| implicit def foo[T](t: T)(implicit ev: Foo[T]): String = ev.foo(t) | |
| // Or using context bounds (choose one or the other depending on usage of the implicit evidence) | |
| implicit def foo[T : Foo](t: T): String = implicitly[Foo[T]].foo(t) |
| Add rx things together and log stuff: | |
| (consumer.stream1 ++ consumer.stream2 ++ consumer.stream3).subscribe(event => logger.info("Received event " + event)) | |
| Filter out messages: | |
| consumer.stream1.filter(_.isEnabled != true).subscribe(event => map.remove(event.id)) | |
| It’s kinda handy that rx streams are basically infinite lists that you can apply all scala magical collection incantations to. |
| for (ln <- io.Source.stdin.getLines()) println(ln) | |
| Iterator.continually(StdIn.readLine()).takeWhile(_.nonEmpty).foreach(line => println("read " + line)) | |
| Observable.from(Iterator.continually(StdIn.readLine()).toIterable).subscribe(line => println("read " + line)) |
| def fib(n: Int): Stream[Int] = n match { | |
| case 0 ⇒ 0 #:: fib(1) | |
| case 1 ⇒ 1 #:: fib(2) | |
| case _ ⇒ (fib(n - 1).head + fib(n - 2).head) #:: fib(n + 1) | |
| } | |
| fib(0) take 10 foreach println | |
| // From http://www.scala-lang.org/api/current/#scala.collection.immutable.Stream | |
| val fibs: Stream[BigInt] = BigInt(0) #:: BigInt(1) #:: fibs.zip(fibs.tail).map { n => n._1 + n._2 } |
| import org.scalacheck.Prop.BooleanOperators | |
| import org.scalacheck.{Arbitrary, Gen} | |
| import org.scalatest.prop.GeneratorDrivenPropertyChecks | |
| import org.scalatest.{FreeSpec, Matchers} | |
| /** | |
| * Some example usages of the (Scala Check)[http://scalacheck.org] library | |
| * | |
| * References: | |
| * (Scala Check User Guide)[https://github.com/rickynils/scalacheck/wiki/User-Guide] |
| object OptionToOr { | |
| implicit class PimpedOption[T](opt: Option[T]) { | |
| def orError(error: String): T Or Every[String] = opt match { | |
| case Some(value) => Good(value) | |
| case None => Bad(One(error)) | |
| } | |
| } | |
| } |
I often find myself leaving out a change set of local changes that I don’t want to check in. In Idea, you can add them to a separate change list of local changes and ignore them from commits. On the command line, you can do a local ignore/untrack of a file, whithout using .gitignore. Useful if your .gitignore is also itself checked into git.
nelanka@hydra:/Projects/booking> git status
On branch master
Your branch is up-to-date with 'origin/master'.
| " ~/.vim/filetype.vim | |
| au BufNewFile,BufRead *.gradle setf groovy |
| *.class | |
| *.log | |
| # mine | |
| .idea | |
| build | |
| target | |
| logs | |
| .gradle | |
| data |