This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scalacache._ | |
| import guava._ | |
| import memoization._ | |
| import com.google.common.cache.CacheBuilder | |
| import scala.concurrent.Future | |
| import scala.concurrent.ExecutionContext.Implicits.global | |
| import scala.concurrent.duration._ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Creates an update statement for a SQL db table from a type where some or all of the values might be None | |
| * If all the values are None will return empty string else it will create the appropriate strings for the Some values | |
| * | |
| * This could be much improved and generalised but its just a simple example. Useful when using db libraries | |
| * where pure SQL is used. | |
| */ | |
| case class Person(name: Option[String], | |
| age: Option[Int]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /******************** | |
| * sortBy Example 1 | |
| ********************/ | |
| case class Person(name: String, age: Int) | |
| val people = List(Person("Julian", 35), Person("David", 25), Person("Linda", 40)) | |
| //ascending | |
| people.sortBy(_.age) //List(Person("David", 25), Person("Julian", 35), Person("Linda", 40)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Takes a list of A and returns a map of A -> Int where Int represents occurences of A | |
| */ | |
| def listCount[A](list: List[A]): Map[A, Int] = | |
| list.foldLeft(Map[A,Int] ()) { (a,b) => | |
| a.get(b).fold(a + (b -> 1)) (count => a + (b -> (count + 1))) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * One way to help the compiler prevent you from introducing bugs. It places | |
| * log that is usually only available at runtime into bugs. | |
| * | |
| * In scala a nested type is bound to the specific instance of the outer type | |
| * and not the type itself | |
| * | |
| */ | |
| class MusicGenre { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Any developer can declare that a type is a member of a type class simply by providing implementations of the | |
| * operations the type must support. Now, once T is made a member of the type class C, functions that have constrained | |
| * one or more of their parameters to be members of C can be called with arguments of type T. | |
| * | |
| * As such, type classes allow ad-hoc and retroactive polymorphism. Code that relies on type classes is open to extension | |
| * without the need to create adapter objects. | |
| * | |
| * They also allow program design that is open for extension without giving up important information about concrete types | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cats.data.Xor | |
| class Boom(msg:String) extends Error(msg) | |
| def boomXorUnit(blowUp: Boolean): Boom Xor Unit = | |
| if(blowUp) Xor.Left(new Boom("kaboom")) | |
| else new Xor.Right(()) | |
| val left = boomXorUnit(true) | |
| val right = boomXorUnit(false) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * A closure is a function, whose return value depends on the value of one or more variables | |
| * declared outside this function. | |
| */ | |
| /* | |
| * Example 1 | |
| * closureIdentity has a reference to the variable i outside the function but in the enclosing scope. | |
| * The function references factor and reads its current value each time. | |
| */ | |
| var i = 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| A Scala method, as in Java, is a part of a class. It has a name, a signature, | |
| optionally some annotations, and some bytecode. | |
| A function in Scala is a complete object. | |
| */ | |
| def sizeConstraint(pred: IntPairPred, n: Int, text: String): Boolean = | |
| pred(text.size, n) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scala.util.{Failure, Success, Try} | |
| /** | |
| * Basics | |
| */ | |
| class OddNumberException extends Exception | |
| def doubleEvenNumbers(x: Int): Either[OddNumberException, Int] = | |
| if (x % 2 != 0) Left(new OddNumberException) | |
| else Right(x * 2) |