Skip to content

Instantly share code, notes, and snippets.

@shehaaz
shehaaz / WhatIsASeqView.scala
Last active July 29, 2016 08:17
What is a SeqView in Scala
scala> val seq = (1 to 10000000).view.filter(_ % 2 == 0).map(x => x+1)
seq: scala.collection.SeqView[Int,Seq[_]] = SeqViewFM(...)
scala> seq.take(10)
res0: scala.collection.SeqView[Int,Seq[_]] = SeqViewFMS(...)
scala> seq.take(10).force
res1: Seq[Int] = Vector(3, 5, 7, 9, 11, 13, 15, 17, 19, 21)
scala> val seq = (1 to 10000000).filter(_ % 2 == 0).map(x => x+1)
val time = Latency.measure[Unit](Kamon.metrics.histogram("awesomeHistogram"))
time(printInt(5))
Latency.measure(Kamon.metrics.histogram("awesomeHistogram"))(printInt(5))
def printInt(num: Int): Unit = {
print(num)
}
import java.io.InputStream
import akka.actor.{ Actor, ActorRef, Props, ActorSystem }
case class ProcessStringMsg(lineNumber: Int, fileName: String, string: String, fileSender: Option[ActorRef], listener: ActorRef)
case class StringProcessedMsg(words: Integer, fileSender: Option[ActorRef])
case class FileReference(fileName: String, stream: InputStream)
case class ProcessedFile(fileName: String, totalNumWords: Int, timeElapsed: Long, onCompleteSignal: Boolean)
case class CaptureStream(fileName: String, numOfWords: Int, lineNumber: Int, onCompleteSignal: Boolean)
case class StartProcessFileMsg()
@shehaaz
shehaaz / Options.scala
Created March 28, 2016 04:22
Scala Options Explained
/**
* Option Collections
*
* Some developers see Option as a safe replacement for null values,
* notifying users that the value may be missing and
* reducing the likelihood that its use will trigger a Null PointerException.
* Others see it as a safer way to build chains of operations,
* ensuring that only valid values will persist for the duration of the chain.
*
* The Option type is itself unimplemented but relies on two subtypes for the implementation:
@shehaaz
shehaaz / Optional.java
Created January 5, 2016 21:42
Optional in Java 8
Optional<String> optional = Optional.of("Bam");
if(optional.isPresent())
{
System.out.println(optional.get()); //Bam
}
optional = Optional.empty();
System.out.println(optional.orElse("test")); //test
let rec trace_insert ((x,d) as e) t = match t with
| Empty -> (Node(e,Empty,Empty), H x)
| Node((y,d'),l,r) -> if (y=x) then (Node(e,l,r),H x)
else if x<y then
let(t',tr) = trace_insert e l in ( Node((y,d'),t',r), L(y,tr) )
else
let(t',tr) = trace_insert e r in ( Node((y,d'),l,t'), R(y,tr) )
let rec trace_insert ((x,d) as e) t = match t with
| Empty -> (Node(e,Empty,Empty), H x)
| Node((y,d'),l,r) -> if (y=x) then (Node(e,l,r),H x)
else if x<y then
let(t',tr) = trace_insert e l in ( Node((y,d'),t',r), L(y,tr) )
else
let(t',tr) = trace_insert e r in ( Node((y,d'),l,t'), R(y,tr) )
@shehaaz
shehaaz / flip_flop.ml
Created December 12, 2013 02:14
Fun with References: Write a function flip_flop :- unit -> (unit -> int) flip_flop: Generates a function that takes in UNIT and produces INT. Every time the generated function is called with unit it either produces "0" or "1" e.g: let ff = flip_flop();; ff();; >1 ff();; >0 ff();; >1 ...
let flip_flop () =
let counter = ref 0
in
fun () -> (if(!counter = 0) then counter := 1 else counter := 0; !counter);;
@shehaaz
shehaaz / gist:7921325
Created December 12, 2013 00:42
OCaml Question: Write a function to sum all the ODD numbers between a & b (a is guaranteed to be smaller than b)
let sumOdd a b =
let rec inner a b =
if a>=b then b else a + (inner (a+2) b)
in
let (new_a,new_b) =
(
(if ((a mod 2) = 0) then (a+1) else a),
@shehaaz
shehaaz / gist:7165109
Created October 26, 2013 03:56
Ocaml Option Example
# let x = Some "test";;
val x : string option = Some "test"
# match x with
|None -> Printf.printf "saw none\n"
|Some v -> Printf.printf "saw %s\n" v;;
saw test
- : unit = ()