This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.concurrent._ | |
import scala.collection.mutable.Builder | |
import scala.collection.generic.CanBuildFrom | |
import language.higherKinds | |
/** | |
* Linearize asynchrnously applies a given function in-order to a sequence of values, producing a Future with the result of the function applications. | |
* Execution of subsequent entries will be aborted if an exception is thrown in the application of the function. | |
*/ | |
def linearize[T, U, C[T] <: Traversable[T]](s: C[T])(f: T => U)(implicit cbf: CanBuildFrom[C[T], U, C[U]], e: ExecutionContext): Future[C[U]] = { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ©2012 Viktor Klang | |
// 4,192 bytes jarred | |
package java.klang | |
import java.util.concurrent. { Executor, ConcurrentHashMap, ConcurrentLinkedQueue } | |
import java.util.concurrent.atomic. { AtomicBoolean } | |
import scala.annotation.tailrec | |
trait Striped { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright 2012-2021 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// © 2012 Viktor Klang | |
import akka.dispatch.ExecutionContext | |
import javax.swing.SwingUtilities | |
import java.util.concurrent.Executor | |
// | |
object SwingExecutionContext { | |
implicit val swingExecutionContext: ExecutionContext = ExecutionContext.fromExecutor(new Executor { | |
def execute(command: Runnable): Unit = SwingUtilities invokeLater command |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ©2012 Viktor Klang | |
package akka.klang | |
import akka.dispatch.{ DispatcherPrerequisites, ExecutorServiceFactory, ExecutorServiceConfigurator } | |
import com.typesafe.config.Config | |
import java.util.concurrent.{ ExecutorService, AbstractExecutorService, ThreadFactory, TimeUnit } | |
import java.util.Collections | |
import javax.swing.SwingUtilities |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright 2012-2021 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Scala extends App { | |
import scala.collection.mutable.ArrayBuffer | |
import scala.annotation.tailrec | |
val items = List("foo", 23, true) | |
val before = System.currentTimeMillis() | |
@tailrec def adds(n: Int, absoluteResult: ArrayBuffer[Any] = ArrayBuffer()): ArrayBuffer[Any] = { | |
def foo(obj : Any) = obj match { | |
case _:String => "String" | |
case _:Boolean => "Boolean" | |
case _:Integer => "Integer" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//An interface we'll secretly inject into the mix, in order to get Java Serialization to think we've implemented writeReplace | |
trait Replaceable { | |
@throws(classOf[java.io.ObjectStreamException]) | |
def writeReplace(): AnyRef | |
} | |
//An external representation of our Proxy | |
case class SerializedProxy(...) { //Whatever you need to store away in order to rematerialize | |
@throws(classOf[java.io.ObjectStreamException]) | |
def readResolve(): AnyRef = { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com> | |
*/ | |
package akka.zeromq | |
import akka.actor.{Actor, ActorRef} | |
import akka.dispatch.MessageDispatcher | |
import akka.util.Duration | |
import akka.zeromq.SocketType._ | |
import java.util.concurrent.atomic.AtomicReference |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Enum { //DIY enum type | |
import java.util.concurrent.atomic.AtomicReference //Concurrency paranoia | |
type EnumVal <: Value //This is a type that needs to be found in the implementing class | |
private val _values = new AtomicReference(Vector[EnumVal]()) //Stores our enum values | |
//Adds an EnumVal to our storage, uses CCAS to make sure it's thread safe, returns the ordinal | |
private final def addEnumVal(newVal: EnumVal): Int = { import _values.{get, compareAndSet => CAS} | |
val oldVec = get |