Skip to content

Instantly share code, notes, and snippets.

View ryandavidhartman's full-sized avatar

Ryan Hartman ryandavidhartman

  • InterPayments
  • Bloomington, IN, USA
View GitHub Profile
@ryandavidhartman
ryandavidhartman / FutureFlatmap.scala
Created August 17, 2015 01:40
Scala Future flatmap
trait Future[+T] extends Awaitable[T] {
def flatMap[S](f: T => Future[S])(implicit executor: ExecutionContext): Future[S] = {
import impl.Promise.DefaultPromise
val p = new DefaultPromise[S]()
onComplete {
case f: Failure[_] => p complete f.asInstanceOf[Failure[S]]
case Success(v) => try f(v) match {
// If possible, link DefaultPromises to avoid space leaks
case dp: DefaultPromise[_] => dp.asInstanceOf[DefaultPromise[S]].linkRootOf(p)
case fut => fut.onComplete(p.complete)(internalExecutor)
@ryandavidhartman
ryandavidhartman / SimpleFutureFlatmap.scala
Created August 17, 2015 01:42
Simplified Future Flatmap
trait Future[+T] {
def flatMap[S](f: T => Future[S])
(implicit executor: ExecutionContext): Future[S] = {
val promise = new Promise[S]()
this.onComplete {
// The first Future (this) failed
@ryandavidhartman
ryandavidhartman / BasicKafkaProcducer.scala
Last active June 10, 2021 12:39
A demonstration of the simplest Scala Kafka Producer
package com.example.basic
import java.util.Properties
import org.apache.kafka.clients.producer.{Callback, RecordMetadata, ProducerRecord, KafkaProducer}
import scala.concurrent.Promise
case class BasicProducer(topic: String, brokerList:String, sync: Boolean) {
val kafkaProps = new Properties()
@ryandavidhartman
ryandavidhartman / BasicKafkaConsumer.scala
Last active March 30, 2017 22:19
The most basic Scala Kafka Consumer
package com.example.basic
import java.util.Properties
import kafka.consumer.{ConsumerIterator, Consumer, ConsumerConfig, KafkaStream}
import kafka.serializer.StringDecoder
import kafka.utils.VerifiableProperties
case class BasicConsumer(zooKeeper: String, groupId: String, waitTime: String) {
val kafkaProps = new Properties()
def factorial(n: Int): Int = if (n == 0) 1 else n * factorial(n - 1)
factorial(4) =
if (4 == 0) 1 else 4 * factorial(4 - 1) =
4 * factorial(4 - 1) =
4 * factorial(3) =
4 * { if (3 == 0) 1 else 3 * factorial(3 - 1) } =
4 * { 3 * factorial(3 - 1) } =
4 * { 3 * factorial(2) } =
@ryandavidhartman
ryandavidhartman / tailrecursion.scala
Created February 18, 2016 02:27
Using the substitution model to visualize tail recursion
def factorial(n: Int) : Int = {
def factorialTR(acc: Int, n: Int) : Int = if (n == 1) acc else factorialTR(acc * n, n - 1)
factorialTR(1, n)
}
factorial(4) =
factorialTR(1, 4) =
if (4 == 1) 1 else factorialTR(1 * 4, 4 - 1) =
factorialTR(1 * 4, 4 - 1) =
@ryandavidhartman
ryandavidhartman / functionDefinitions.scala
Last active March 21, 2016 00:50
Different ways of defining functions in Scala
def f1(x: Int): Int = x*x
val f2 = (x: Int) => x*x
def f3 = (x: Int) => x*x
def f4:(Int => Int) = x => x*x
trait A {
def foo(text: String) = printf("a says:" + text)
}
trait B extends A {
override def foo(text: String) = printf("b says:" + text)
}
trait C extends A {
override def foo(text: String) = printf("c says:" + text)
package com.angieslist.common.play.controller
import com.angieslist.common.http.DispatchHttpClientComponent
import com.angieslist.common.{Logger, LoggerComponent}
import com.ning.http.client.FluentCaseInsensitiveStringsMap
import com.typesafe.config.{Config, ConfigFactory}
import dispatch.{url, _}
import play.api.mvc._
import scala.collection.JavaConversions._
@ryandavidhartman
ryandavidhartman / wordcount.scala
Created March 12, 2017 01:34
Basics Scala word count
def counter(file: String) = {
scala.io.Source.fromFile(file)
.getLines
.flatMap(_.split("\\W+"))
.foldLeft(Map.empty[String, Int]){
(count, word) => count + (word -> (count.getOrElse(word, 0) + 1))
}
}