Skip to content

Instantly share code, notes, and snippets.

import scala.util.parsing.combinator.RegexParsers
object Calculator extends Cal {
def main(args: Array[String]) {
assert(!parse(exp, "a").successful)
assert(parseAll(exp, "1").get == 1)
assert(parseAll(exp, "1+1").get == 2)
assert(parseAll(exp, "1*1").get == 1)
assert(parseAll(exp, "1/1").get == 1)
import scala.util.matching.Regex
import scala.util.parsing.combinator.RegexParsers
object JsonPrac {
def main(args: Array[String]) {
assert(JParser( """[1,2,3]""").successful)
assert(JParser( """
{"id": "ZoomIn"}
@purijatin
purijatin / TraversableWithStatistics.scala
Last active March 15, 2017 10:03 — forked from MishaelRosenthal/TraversableWithStatistics.scala
Adding mean and variance to Scala collections.
object TraversableWithStatistics {
implicit class RichTraversable[A](collection: Traversable[A])(implicit val numeric : Numeric[A]) {
private def pow2[C](x: C)(implicit num: Numeric[C]) = math.pow(num.toDouble(x), 2)
private def sqrt[C](x: C)(implicit num: Numeric[C]) = math.sqrt(num.toDouble(x))
def mean = {
import io.Source
import scala.util.control.Breaks._
/**
* Scala TicTacToe game without any side effects
*
* Written in response to following post (which also contains task description):
* http://blog.tmorris.net/scala-exercise-with-types-and-abstraction/
*/
object TicTacToe {

Advanced Functional Programming with Scala - Notes

Copyright © 2017 Fantasyland Institute of Learning. All rights reserved.

1. Mastering Functions

A function is a mapping from one set, called a domain, to another set, called the codomain. A function associates every element in the domain with exactly one element in the codomain. In Scala, both domain and codomain are types.

val square : Int => Int = x => x * x
@purijatin
purijatin / WordCount.scala
Created July 20, 2017 09:25
WordCount.scala
import akka.Done
import akka.actor.Status.Success
import akka.actor.{ActorRef, ActorSystem}
import akka.stream.scaladsl.{Flow, RunnableGraph, Sink, Source}
import akka.stream.{ActorMaterializer, OverflowStrategy}
import scala.concurrent.Future
object Generic {
@purijatin
purijatin / CFSession.java
Created September 4, 2017 14:19
Get started with CompletableFuture Session
/**
* A Future represents the result of an asynchronous
* computation. Methods are provided to check if the computation is
* complete, to wait for its completion, and to retrieve the result of
* the computation. The result can only be retrieved using method
* {@code get} when the computation has completed, blocking if
* necessary until it is ready. Additional methods are provided to
* determine if the task completed normally or was cancelled.
*