Skip to content

Instantly share code, notes, and snippets.

View rupeshtr78's full-sized avatar

rupeshtr78 rupeshtr78

View GitHub Profile
package scalacollections
object ScalaCollectionsMap {
import scala.collection.immutable.{HashMap, ListMap}
val res: Seq[Char] = "hello world".toList.filter(_.isLetter).map(_.toUpper) //List(H, E, L, L, O, W, O, R, L, D)
val resMap: Map[Char, Seq[Char]] = res.groupBy(x => x) //Map(E -> List(E), L -> List(L, L, L), H -> List(H), W -> List(W), R -> List(R), O -> List(O, O), D -> List(D))
spark.readStream
.format("kafka")
.option("subscribe", "input")
.load()
.withWatermark(eventTime="timestampColumn", delayThreshold="10 seconds")
.groupBy('value.cast("string") as 'key)
.groupBy(window(col("time"), "1 day").as("time")) // tumbling window
.groupBy(window(col("time"), "10 minutes", "5 minutes").as("time")) // sliding window
.groupBy( 'keyCol, window("timestamp","10 mins"))
.agg(count("*") as 'value)
import java.io.{FileWriter, PrintWriter}
import scala.io.Source
val fileLines = Source.fromFile("/data/bullflag.txt").getLines()
for ((line, n) <- fileLines zipWithIndex) {
val lineNumber = n+1
val fileName = s"line-$lineNumber.txt"
val filePath = s"/data/$fileName"
val fileWriter = new PrintWriter(new FileWriter(filePath))
fileWriter.write(line)
//Big rule → Functions
//“Be liberal in what you accept from others”
//“Be conservative in what you do”
//type Function[-A, +B] = Function1[A, B]
//the inputs of functions are contravariant and output are covariant.
//method arguments are in CONTRAVARIANT position -A (more general input argument)
//return types are in COVARIANT position +B (more specific return type)
val bullFlag = stockRecord.close match {
case stockRecord.close if (previousState.bullFlag == 1 && stockRecord.close >= previousState.lowClose) => 1
case stockRecord.close if ((stockRecord.close > highOfLowClose) && stockRecord.candle == 1) => 1
case _ => 0
}
@rupeshtr78
rupeshtr78 / tail-recursion.scala
Created October 29, 2020 01:01
tail-recursion
def sum(args:Int *): Int = {
@tailrec
def tailRecursionSum(argSeq: Seq[Int],result: Int): Int = {
if (argSeq == Nil) result
else
tailRecursionSum(argSeq.tail, result + argSeq.head)
}
tailRecursionSum(args,0)
}
Immutable
val numList = List[Int](1, 2, 3)
val numList2 = List[Int](10, 20, 30)
val numList3 = 1 :: 2:: 3:: Nil //List(1, 2, 3)
val numlist3 = numList ++ numList2 //List(1, 2, 3, 10, 20, 30)
val x = 100 :: numList //List(100, 1, 2, 3)
val x1 = numList :+ 100 //List(1, 2, 3, 100)
val x2 = 100 +: numList //List(100, 1, 2, 3)
val x3 = numList :: numList2 //List(List(1, 2, 3), 10, 20, 30)
val y = numList +: numList2 //List(List(1, 2, 3), 10, 20, 30)
import scala.collection.immutable.{HashMap, ListMap, ListSet, Queue}
import scala.collection.{LinearSeq, SortedSet, mutable}
val trav = Traversable(1, 2, 3)
val iter = Iterable("x", "y", "z")
val map = Map("x" -> 24, "y" -> 25, "z" -> 26)
val numArray = Array[Int](1, 2, 3, 4, 5)
val numList = List[Int](1, 2, 3, 4, 5)
package PlayGround
object ScalaCollectionsFoldReduceFlat {
val evens = List(2,4,6)
val odds = List(1,3,5)
val a = "foo bar baz"
val foo = "foo"
val bar = "bar"
val names = List("Al","Chistina","Kim")
package PlayGround
object ScalaCollectionsSortGroup {
val evens = List(2,4,6)
val odds = List(1,3,5)
val a = "foo bar baz"
val foo = "foo"
val bar = "bar"
val names = List("Al","Chistina","Kim")