Skip to content

Instantly share code, notes, and snippets.

@nelanka
nelanka / scala-class.md
Last active August 29, 2015 14:16
Scala Class

Scala Brown Bag Lunch Series

Day 1

Introduction to Developing in Scala

  • Setup of tool chain
  • Value Types
  • Simple functions

Day 2

@nelanka
nelanka / typeclass.scala
Last active November 4, 2018 16:42
Typeclass
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)
@nelanka
nelanka / rx.scala
Created February 13, 2015 15:18
Scala RX Snippets
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.
@nelanka
nelanka / io.scala
Created February 11, 2015 18:18
Scala I/O
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))
@nelanka
nelanka / streams.scala
Last active August 29, 2015 14:14
Stream Examples
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 }
@nelanka
nelanka / ScalaCheckExamples.scala
Last active December 19, 2023 04:45
ScalaCheck Examples
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]
@nelanka
nelanka / OptionToOr.scala
Last active August 29, 2015 14:13
Map an Option to a Scalactic Or
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))
}
}
}
@nelanka
nelanka / LocalGitUntrack.md
Last active October 16, 2023 15:45
Locally untrack files in Git, without using .gitignore

Excluding files from git's changeset

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.

Change the file(s) to be untracked:

nelanka@hydra:/Projects/booking> git status
On branch master
Your branch is up-to-date with 'origin/master'.
@nelanka
nelanka / filetype.vim
Created November 26, 2014 17:25
Gradle Syntax Highlighting in Vim
" ~/.vim/filetype.vim
au BufNewFile,BufRead *.gradle setf groovy
@nelanka
nelanka / .gitignore
Created November 25, 2014 03:46
Git Ignore File
*.class
*.log
# mine
.idea
build
target
logs
.gradle
data