Skip to content

Instantly share code, notes, and snippets.

@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.
*
@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 {

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
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 {
@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 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"}
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)
@purijatin
purijatin / ZooKeeperLock.scala
Last active October 3, 2016 11:22
Distributed Lock based on ZooKeeper
package zookeeper
import java.util.concurrent.{ExecutorService, Executors}
import org.apache.zookeeper._
import scala.collection.JavaConverters._
import scala.concurrent.duration._
import scala.concurrent.{Await, ExecutionContext, Future}
import scala.util.control._
@purijatin
purijatin / DifferentTypes.scala
Last active September 23, 2016 06:51
New.scala
To write a method which takes two type parameters and both should not be the same:
trait =!=[A,B]
implicit def neq[A,B]: =!=[A,B] = null
implicit def eqAmb[A]: =!=[A,A] = null
implicit def eqAmbig2[A]: =!=[A,A] = null
case class Foo2[A,B]( a: A, b: B )( implicit ev: A =!= B )
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Optional;
import static java.util.Objects.isNull;
public class User {