Skip to content

Instantly share code, notes, and snippets.

View ericacm's full-sized avatar

Some Dude ericacm

View GitHub Profile
@kevinwright
kevinwright / ThrottledExecutionContext.scala
Last active October 5, 2018 08:09
Limit number of futures running in parallel (now updated with seize/release implementations suggested by roland)
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.atomic.AtomicInteger
import scala.concurrent.ExecutionContext
import scala.util.control.NonFatal
import scala.annotation.tailrec
//inspired by https://gist.github.com/viktorklang/4552423
object ThrottledExecutionContext {
def apply(maxConcurrents: Int)(implicit context: ExecutionContext): ExecutionContext = {
@etorreborre
etorreborre / gist:5078824
Last active March 24, 2024 14:55
A good summary of Scala types from http://bit.ly/XjSVKw
class Outer {
class Inner
type Type
}
trait Trait
object Object extends Outer {
val inner = new Inner
}
class OuterP[A] {
class InnerP[B]
@noahlz
noahlz / BushLeagueJava.md
Last active December 14, 2015 14:29
Something I wrote back in 2008 when I was dealing with a codebase that was driving me nuts.

Bush league is a general term used to describe an action or thing as being amateur, inferior or crude. In a literal sense, it refers to a low quality minor-league in baseball not associated with any of the major league teams. The term originated from the state of minor-league fields that often were ringed with shrubs and bushes.

- Wikipedia

Secretly using StringBuffer to convert a number to a String

int value = getValue();
String valStr = value + "";
@viktorklang
viktorklang / InterruptibleCancellableFuture.scala
Last active June 1, 2020 13:45
Interruptible-Cancellable-scala.concurrent.Future
/*
Copyright 2018 Viktor Klang
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
@andrewconner
andrewconner / FutureGoodies.scala
Last active May 4, 2016 11:34
SafeFuture, TimeoutFuture, CancelableFuture implementations. See https://eng.42go.com/future-safefuture-timeout-cancelable for further explanation.Thanks to @bretthoerner for spotting an error!
/* We've run into a few common pitfalls when dealing with Futures in Scala, so I wrote these three helpful
* classes to give some baked-in functionality.
*
* I'd love to hear about other helpers you're using like these, or if you have improvement suggestions.
* github@andrewconner.org / @connerdelights
*/
import scala.concurrent.{ExecutionContext, CanAwait, Awaitable, Future, Promise}
import scala.concurrent.duration.Duration
import scala.util.Try
@arschles
arschles / scalaConcurrentPatterns.scala
Last active August 23, 2018 15:00
examples of common concurrency patterns that you can achieve with the scala.concurrent package
import scala.concurrent._
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.Random
import java.util.{Timer, TimerTask}
object Util {
sealed trait BaseResponse
case class Response1(res: Int) extends BaseResponse
case class Response2(res: String) extends BaseResponse
@ericacm
ericacm / MessageSequence.scala
Last active August 22, 2017 16:49
Implements the Message Sequence pattern from EIP (http://www.eaipatterns.com/MessageSequence.html)
package akka.contrib.pattern
import akka.actor.{Cancellable, ActorLogging, ActorRef, Actor}
import java.util
import akka.serialization.SerializationExtension
import scala.concurrent.ExecutionContext
import scala.concurrent.duration._
import scala.util.{Try, Failure, Success}
import scala.language.existentials
import java.util.UUID
@jboner
jboner / how-akka-maps-to-eai-patterns.txt
Last active October 9, 2022 21:57
How Akka maps to EAI Patterns
# How Akka maps to EAI Patterns
Might be up for debate or just plain wrong. Just some notes I scribbled down some time ago.
-----------------------------------------------------------------------------------------------------------------
EAI PATTERN AKKA PATTERN REFERENCE
-----------------------------------------------------------------------------------------------------------------
Point to Point Channel Regular Actor Communication http://www.eaipatterns.com/PointToPointChannel.html
Event-Driven Consumer Regular Actor Receive http://www.eaipatterns.com/EventDrivenConsumer.html
Message Selector Actor with Stash http://www.eaipatterns.com/MessageSelector.html
@ryanlecompte
ryanlecompte / gist:9745983
Created March 24, 2014 18:18
lazily recurse files in a root directory
import java.io.File
/**
* Iterate all files in the given directory recursively.
* @param root the root directory to traverse
* @return an Iterator[File] of traversed files
*/
def listFiles(root: File): Iterator[File] = {
def rec(files: List[File]): Stream[File] = {
files match {
@mnadel
mnadel / DatabaseCallable.java
Last active May 18, 2018 13:43
Dropwizard + Hystrix + Hibernate
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.Callable;
/**
* A database session and transaction-aware Callable
*/
class DatabaseCallable<T> implements Callable<T>