Skip to content

Instantly share code, notes, and snippets.

@maasg
maasg / RunningLengthEncoding.scala
Last active December 22, 2015 13:38
Running Lenght Encoding problem: Given a string with repeated characters, return a string with the repeating characters and their frequency: e.g. aaabbc = a3b2c1
object RunningLengthEncoding {
def encode(s:String):String = {
def encSeq(s:String):String = s.head.toString+s.length
if (s.isEmpty()) "" else {
val (same,rest) = s.span(_==s(0));
encSeq(same) ++ encode(rest)
}
}
}
@maasg
maasg / CaesarCodex.scala
Created September 8, 2013 00:15
Caesar Codex
object CaesarCodex {
class CyclicCharRange(start:Char,end:Char) extends PartialFunction[Char,Int=>Char] {
val size = end-start+1
def isDefinedAt(c:Char) = c>=start && c<=end
def apply(c:Char): Int => Char = { amount =>
val rangeAmount = ((amount % size)+size) % size
val shiftedValue = (((c-start)+rangeAmount)%size)+start
shiftedValue.toChar
}
@maasg
maasg / MergeSort.scala
Created September 8, 2013 14:10
MergeSort - classical sort algorithm done as refresh coding exercise
object MergeSort {
def mergeSort[T](list: List[T])(implicit order: Ordering[T]): List[T] = {
def merge[T](l1: List[T], l2: List[T])(implicit order: Ordering[T]): List[T] = {
(l1, l2) match {
case (Nil, Nil) => Nil
case (Nil, l) => l
case (l, Nil) => l
case (x :: tail1, y :: tail2) => if (order.compare(x, y) > 0) y :: merge(l1, tail2) else x :: merge(tail1, l2)
}
@maasg
maasg / InvokeBench.scala
Last active December 24, 2015 13:59
Micro benchmark of using invoke (scala/jvm) in a console session
def invoke1[T,U](obj:Any, method:Method)(param:T):U = method.invoke(obj,Seq(param.asInstanceOf[java.lang.Object]):_*) match {
case x: java.lang.Object if x==null => null.asInstanceOf[U]
case x => x.asInstanceOf[U]
}
def time[T](b: => T):(T, Long) = {
val t0 = System.nanoTime()
val res = b
val t = System.nanoTime() - t0
(res,t )
@maasg
maasg / OptionUnmarshaller.scala
Last active February 1, 2016 09:40
Example of supporting HTTP 404 - Not found status on spray.io http-client unmarshalling pipeline
/** Adds support for Http/404 - Not Found to a processing pipeline.
* A 404/Not found is converted to None while other HTTP statuses will get converted as usual
* (HTTP/200 - will result in a marshalled object, errors will throw an exception, failing the future.
*/
def convertOption[T](implicit unmarshaller:FromResponseUnmarshaller[T]): Future[HttpResponse] => Future[Option[T]] =
(futRes: Future[HttpResponse]) => futRes.map{res =>
if (res.status == StatusCodes.NotFound) None
else Some(unmarshal[T](unmarshaller)(res))
}
@maasg
maasg / cluster.scala
Created June 8, 2014 14:24
Cluster sequential elements given a predicate
// given a list of elements and a predicate over those elements,
// cluster the elements into a lists of lists of elements that fulfil the predicate, preserving the order of the original list.
def cluster[T](l:List[T], cond:List[T] => Boolean):List[List[T]] = {
def splitCond[T](l:List[T], cond:List[T]=>Boolean):(List[T], List[T]) = {
def splitCond0(l:List[T], cond:List[T]=>Boolean, cumm:List[T]):(List[T], List[T]) = l match {
case Nil => (cumm, Nil)
case h::t => if (cond(h::cumm)) splitCond0(t, cond, h::cumm) else (cumm,l)
}
splitCond0(l,cond,Nil)
@maasg
maasg / CalliopeToCassandra.scala
Created June 16, 2014 16:22
Simple Calliope Save To Cassandra Test
def saveToCassandra(sc:SparkContext, total:Int): Long = {
val cas = CasBuilder.cql3.withColumnFamily("keyspace", "raw").onHost(host).onPort(port)
.saveWithQuery("UPDATE keyspace.raw set metric=?, vals=?")
implicit def TsToCasKeys(src:Array[String]): Map[String, ByteBuffer] = src match {
case Array(device_id, aggregation_type, t0, metric, vals) =>
Map("device_id" -> device_id, "aggregation_type" -> aggregation_type, "t0" -> t0)
}
@maasg
maasg / pom.xml
Created September 30, 2014 16:43
POM file to generate an uberjar for the Cassandra-Spark-Driver to use with the spark-shell
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.acme</groupId>
<artifactId>spark-cassandra-assembly</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>A uber jar to easily use the Cassandra-Spark driver with spark-shell </name>
@maasg
maasg / CassandraWordCount.scala
Created October 24, 2014 18:59
Example code for a stateful stream processor using Spark Streaming and Cassandra
/**
-- Datamodel
-- local keyspace
CREATE KEYSPACE example
WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
-- table schema
CREATE TABLE example.words (
word text PRIMARY KEY,
count int
@maasg
maasg / GeoGrid.snb.ipynb
Created November 25, 2016 11:47
Group a dataset of coordinates in cells of a given resolution, starting at the minimal coordinates from the set using a scalable Spark-based approach
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.