Skip to content

Instantly share code, notes, and snippets.

View r-wheeler's full-sized avatar

Ryan Wheeler r-wheeler

View GitHub Profile
@r-wheeler
r-wheeler / build.sbt
Created January 29, 2015 14:36
akka sbt
name := "akkaapp"
version := "1.0"
scalaVersion := "2.11.1"
resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"
libraryDependencies += "com.typesafe.akka" % "akka-actor_2.11" % "2.3.4"
@r-wheeler
r-wheeler / daily_201.scala
Created February 15, 2015 01:08
Daily programmer Challenge 201
//http://www.reddit.com/r/dailyprogrammer/comments/2vkwgb/20150211_challenge_201_practical_exercise_get/
case class Element(item: String,A: Double, B: Double)
type queue = List[Element]
def enqueue(q: queue, i: String, A: Double, B: Double): queue = Element(i,A,B) :: q
def dequeue(q: queue)(key: Element => Double): (Element, queue) =
(q.sortBy(key).reverse.head, q.sortBy(key).reverse.tail)
val dequeueA = dequeue(_:queue)(_.A)
@r-wheeler
r-wheeler / Futures.scala
Created February 27, 2015 13:46
Futures.scala
//Here is the example Scala code, it assumes a function called fetch which when given a URL will return a Future.
def getThumbnail(url: String): Future[Webpage] = {
val promise = new Promise[Webpage]
fetch(url) onSuccess { page =>
fetch(page.imageLinks(0)) onSuccess { p =>
promise.setValue(p)
} onFailure { exc =>
promise.setException(exc)
}
} onFailure { exc =>
@r-wheeler
r-wheeler / stringzip.scala
Last active August 29, 2015 14:16
zip strings with tail recursion
impprt scala.annotation.tailrec
def f(a: String, b: String): String = {
@tailrec
def loop(a: String, b: String, acc: String): String = {
if (a.length == 0 && b.length ==0 ) acc
else {
val (al, ar) = a.splitAt(1)
val (bl, br) = b.splitAt(1)
val x = al + bl
@r-wheeler
r-wheeler / recurance.scala
Created March 17, 2015 14:53
Daily Programmer 206
//http://www.reddit.com/r/dailyprogrammer/comments/2z68di/20150316_challenge_206_easy_recurrence_relations/
// skip the IO
val y = "*3 +2 *2"
def f(s: String, x: Int, n: Int): Unit = {
def rawF(s: String): Int => Int =
s(0) match {
case '+' => (x: Int) => x + s(1).toString.toInt
case '*' => (x: Int) => x * s(1).toString.toInt
case '/' => (x: Int) => x / s(1).toString.toInt
import scalaz.Monad
import scalaz.syntax.monad._
import scalaz.std.AllInstances._
import scalaz.Functor
import scalaz.Monoid
import scalaz.syntax.monoid._
sealed trait Maybe[+A] {
def map[B](f: A => B): Maybe[B] = this match {
import spark.streaming.StreamingContext._
import spark.streaming.{Seconds, StreamingContext}
import spark.SparkContext._
import spark.storage.StorageLevel
import spark.streaming.examples.twitter.TwitterInputDStream
import com.twitter.algebird.HyperLogLog._
import com.twitter.algebird._
/**
* Example of using HyperLogLog monoid from Twitter's Algebird together with Spark Streaming's
@r-wheeler
r-wheeler / build.sbt
Created April 27, 2015 16:08
Spark SBT
name := "Spark Testing"
libraryDependencies += "org.apache.spark" %% "spark-core" % "1.3.0"
libraryDependencies += "org.apache.spark" %% "spark-graphx" % "1.3.0"
libraryDependencies += "org.apache.spark" %% "spark-mllib" % "1.3.0" % "provided"
libraryDependencies += "org.apache.spark" %% "spark-sql" % "1.3.0"

As a follow up to my last post about no free lunch I wanted to show more state monad usage and recognize that you can change function return types in the middle of a for-comprehension. This allows each function to return different types of values from the computation along the way.

In the code below, you compose a function by using the for-comprehension. The yield part of the for-comprehension is a State object. When that state object is called, it returns a tuple of (state, value). You can use ._1 or ._2 to obtain the value of interest.

The last composed function shows an example of changing the state type (instead of changing the returned value type mentioned above) in the middle of the for-comprehension using scalaz IndexedState's iPut and iModify. This allows you to change the type of state as you go to adapt to different function's API needs.

import scalaz._
import scalaz.State._
// Requires scalaz to be on the classpath
import scalaz._
import scalaz.State._
// The State monad was hard for me to understand because it
// seems as if you never actually store a value in the state and
// manipulate it inside of a method like you would in java.
// There are really two types of methods in the scalaz library