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 / 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
@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) =
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 / 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()
@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 / 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 / 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)
case class StoreViewSummaryResponse(
invoiceData: Iterable[(Invoice, ServiceProvider, Address, ContactInformationData, Sku, Option[String])],
orderTotal: org.joda.money.Money)
lazy val viewSummary: Service[StoreViewSummaryRequest, StoreViewSummaryResponse] = monitored {
//get data from db as Iterables[T]
invoice, serviceProviders primaryAddresses, transactionIds
// get from remote server (returns a list of Future[Sku] )
@ryandavidhartman
ryandavidhartman / try_example8.scala
Last active August 29, 2015 14:22
try_example8
object Program {
def apply(): Program = new Program()
}
trait Program {
def collectData(): Try[List[Data]] = {
val data = dataGetter()
if(data.length < 1)
throw new DataException("Data Collection Fails")
else
@ryandavidhartman
ryandavidhartman / try_example7.scala
Last active August 29, 2015 14:22
try_example7
def processData(data: Try[List[Data]]): Try[Int] = for {
x <- data
results <- x. x.sumBy(i => i.value)
} yield results