Skip to content

Instantly share code, notes, and snippets.

View malzzz's full-sized avatar

Mallory M. malzzz

  • Asymmetric Finance
  • /dev/null
View GitHub Profile
@viktorklang
viktorklang / Future-retry.scala
Last active July 23, 2023 23:48
Asynchronous retry for Future in Scala
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import akka.pattern.after
import akka.actor.Scheduler
/**
* Given an operation that produces a T, returns a Future containing the result of T, unless an exception is thrown,
* in which case the operation will be retried after _delay_ time, if there are more possible retries, which is configured through
* the _retries_ parameter. If the operation does not succeed and there is no retries left, the resulting Future will contain the last failure.
@kafecho
kafecho / Reader.scala
Created May 8, 2014 08:20
Example of using the Reader Monad.
package org.kafecho.learning.monad
import java.util.UUID
/** My attempt at implementing the Reader Monad concept.
* The Reader Monad encapsulates a computation which:
* - requires a dependency of type D
* - produces values of type A.
*/
case class Reader[D, A](computation: D => A) {
@MishaelRosenthal
MishaelRosenthal / DirectedGraphTraversals.scala
Created May 15, 2014 11:02
Directed Graph Traversals Implementations are tail recursive and uses only immutable data structures. These implementations are not more efficient than non tail recursive implementations.
package com.liveperson.predictivedialer.examples.misc
import scala.annotation.tailrec
import scala.util.Try
/**
* Created with IntelliJ IDEA.
* User: mishaelr
* Date: 5/14/14
* Time: 5:18 PM
@zackthehuman
zackthehuman / QuadTree.scala
Created June 9, 2014 13:32
Working generic quadtree written in Scala
case class Identifier(id: Long)
case class Rectangle(x: Int, y: Int, width: Int, height: Int)
object QuadTree {
private val MAX_OBJECTS = 2
private val MAX_LEVELS = 5
private val PARENT_NODE = -1
private val TOP_RIGHT = 0
private val TOP_LEFT = 1
@runarorama
runarorama / gist:a8fab38e473fafa0921d
Last active April 13, 2021 22:28
Compositional application architecture with reasonably priced monads
sealed trait Interact[A]
case class Ask(prompt: String)
extends Interact[String]
case class Tell(msg: String)
extends Interact[Unit]
trait Monad[M[_]] {
def pure[A](a: A): M[A]
@ahoy-jon
ahoy-jon / Transducer.scala
Last active April 27, 2017 07:49
Trying to transduce in Scala
package ahoy
import scala.collection.mutable
object transducer {
type Reducer[A, R] = R => A => R
trait Transducer[A, B] {
@Klasu
Klasu / DbTransactionMonad.scala
Last active October 14, 2016 07:03
Monad for Database Transactions
// Simple monad to deal with multiple database transactions with same connection, their commits and rollbacks
// Something similar done in http://advorkovyy.blogspot.com.au/2010/10/transactional-monad-for-scala.html
import java.sql.Connection
trait DbTransaction[A] {
def unit: Connection => A
def map[B](f: A => B): DbTransaction[B] = DbTransaction {
connection => f(unit(connection))
}
package scalax.collection
import scala.collection.mutable.ListBuffer
/** FoldTransformers and the views based on them are a Scala
* adaptation, and to some degree an extension, of Rich Hickey's
* transducers for Clojure. They show that the concepts can be
* implemented in a type-safe way, and that the implementation is
* quite beautiful.
*/
object FoldingViews {
@non
non / transducers.scala
Created October 30, 2014 00:39
Slightly simpler example of typed transducers in Scala. See http://blog.podsnap.com/ducers2.html for more context.
object Transducer {
type RF[R, A] = (R, A) => R
def apply[A, B](f: A => B) =
new Transducer[B, A] {
def apply[R](rf: RF[R, B]) = (r, a) => rf(r, f(a))
}
}
import Transducer.RF
object TransducerUniversal {
type Reduct[-A, R] = (R, A) => R
trait Trans[+A, -B] { def apply[R](f: Reduct[A, R]): Reduct[B, R] }
def map[A, B](f: A => B): Trans[B, A] = new Trans[B, A] { def apply[R](rf: Reduct[B, R]) = (r, a) => rf(r, f(a)) }
def filter[A](p: A => Boolean): Trans[A, A] = new Trans[A, A] { def apply[R](rf: Reduct[A, R]) = (r, a) => if (p(a)) rf(r, a) else r }
def comp[A,B,C](t1 : Trans[A, B], t2 : Trans[C, A]): Trans[C, B] = new Trans[C, B] { def apply[R](rf: Reduct[C, R]) = t1(t2(rf)) }
def sequence[A, B](t: Trans[B, A], data: Seq[A]) = data.foldLeft(Seq[B]())(t(_ :+ _))
implicit class Compable[A,B](t1: Trans[A, B]) {