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
| lazy val fibs: Stream[BigInt] = | |
| BigInt(0) #:: | |
| BigInt(1) #:: | |
| fibs.zip(fibs.tail).map { n => n._1 + n._2 } | |
| lazy val N: Stream[BigInt] = BigInt(1) #:: N.map(_ + 1) | |
| lazy val fac: Stream[BigInt] = BigInt(1) #:: fac.zip(N).map(a => a._1 * a._2) | |
| fibs take 10 foreach println | |
| fac take 10 foreach println |
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
| package foo | |
| import java.io.ByteArrayInputStream | |
| import com.amazonaws.ClientConfiguration | |
| import com.amazonaws.auth.BasicAWSCredentials | |
| import com.amazonaws.services.s3.AmazonS3Client | |
| import com.amazonaws.services.s3.model.{GetObjectRequest, GroupGrantee, ObjectMetadata, Permission} | |
| import com.typesafe.config.ConfigFactory |
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.Random | |
| /** | |
| * Generates a random string | |
| * Takes an optional prefix and includes timestamp with milisecond accuracy | |
| * To ensure uniqueness, if two strings are requested less than one millisecond apart | |
| * a random string is appended to the end of the string of minimum length 8 | |
| */ | |
| def getRandomFileName(prefix: String = "", i: Int = 8) = { | |
| require(i <= 8) |
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
| /** | |
| * Simple Monad | |
| * | |
| * The key abstraction is the flatMap, which binds the computation through chaining. | |
| * Each invocation of flatMap returns the same data structure type (but of different value), | |
| * that serves as the input to the next command in chain. In the above snippet, flatMap | |
| * takes as input a closure (SomeType) => List[AnotherType] and returns a List[AnotherType]. | |
| * The important point to note is that all flatMaps take the same closure type as input and | |
| * return the same type as output. This is what "binds" the computation thread - every item | |
| * of the sequence in the for-comprehension has to honor this same type constraint. |
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) |
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
| /** | |
| * 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
| 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
| /** | |
| * 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
| /** | |
| * 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 { |