This file contains 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
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 { |
This file contains 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
// CONTEXT: https://groups.google.com/forum/#!topic/scala-user/8YpX1VkIkDs | |
import scala.annotation.implicitNotFound | |
import scala.language.experimental.macros | |
import scala.reflect.macros.whitebox | |
/** | |
* Macro-driven type for proving that an implicit parameter does not exist | |
* in scope. | |
*/ |
This file contains 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.collection.immutable.Seq | |
object CatSeq { | |
val empty: CatSeq[Nothing] = CatSeq[Nothing](0, Seq.empty, Seq.empty) | |
} | |
/** Data structure to do O(1) concatenation of lists | |
*/ | |
case class CatSeq[+T](leftLen: Int, left: Seq[T], right: Seq[T]) extends Seq[T] { | |
override def isEmpty = left.isEmpty && right.isEmpty | |
override def head = if(left.isEmpty) right.head else left.head | |
override def tail = if(left.isEmpty) right.tail else CatSeq(leftLen - 1, left.tail, right) |
This file contains 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 java.nio.{ByteBuffer, CharBuffer} | |
import java.nio.charset.Charset | |
def trimAtBytes(str: String, len: Int, charset: Charset) = { | |
val outBuf = ByteBuffer.wrap(new Array[Byte](len)) | |
val inBuf = CharBuffer.wrap(str.toCharArray()) | |
charset.newEncoder().encode(inBuf, outBuf, true) | |
new String(outBuf.array, 0, outBuf.position(), charset) | |
} |
This file contains 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
/** | |
scala> compress(List.fill(1000)(1)) | |
res17: List[Either[Int,Int]] = List(Left(1), Right(0), Left(1), Right(1), Left(1), Right(2), Left(1), Right(3), Left(1), Right(4), Left(1), Right(5), Left(1), Right(6), Left(1), Right(7), Left(1), Right(8), Left(1), Right(9), Left(1), Right(10), Left(1), Right(11), Left(1), Right(12), Left(1), Right(13), Left(1), Right(14), Left(1), Right(15), Left(1), Right(16), Left(1), Right(17), Left(1), Right(18), Left(1), Right(19), Left(1), Right(20), Left(1), Right(21), Left(1), Right(22), Left(1), Right(23), Left(1), Right(24), Left(1), Right(25), Left(1), Right(26), Left(1), Right(27), Left(1), Right(28), Left(1), Right(29), Left(1), Right(30), Left(1), Right(31), Left(1), Right(32), Left(1), Right(33), Left(1), Right(34), Left(1), Right(35), Left(1), Right(36), Left(1), Right(37), Left(1), Ri... | |
scala> compress(List.fill(1000)(1)).size | |
res18: Int = 88 | |
scala> decompress(res17) | |
res19: List[Int] = List(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
This file contains 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
case class Counter(x: Int = 0) | |
/** A type class for combining types through aggregation */ | |
sealed trait Aggregate[As, Elem] { | |
def apply(el: Elem): As | |
def empty: As | |
def apply(as: As, el: Elem): As | |
} | |
/** A companion to Aggregate exposing a single method, `values` |
This file contains 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 2013 Twitter, Inc. | |
* | |
* 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 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-2014 Typesafe Inc. <http://www.typesafe.com> | |
*/ | |
package akka.contrib.mailbox | |
import scala.concurrent.duration._ | |
import java.util.concurrent.atomic.AtomicInteger | |
import java.util.concurrent.atomic.AtomicLong | |
import com.typesafe.config.Config | |
import akka.actor.{ ActorContext, ActorRef, ActorSystem, ExtendedActorSystem } |
This file contains 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 java.io.{PrintWriter, OutputStreamWriter, BufferedOutputStream} | |
// copy given string to the mac clipboard by launching pbcopy in a subprocess | |
object Clipboard { | |
def pbcopy(string: String) { | |
val pb = new ProcessBuilder("/usr/bin/pbcopy") | |
val p = pb.start() | |
val stdin = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(p.getOutputStream))) | |
stdin.print(string) | |
stdin.close() |
This file contains 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.Await | |
import scala.concurrent.ExecutionContext | |
import scala.concurrent.Future | |
import scala.concurrent.blocking | |
import scala.concurrent.duration.Deadline | |
import scala.concurrent.duration.Duration | |
import scala.concurrent.duration.DurationInt | |
import scala.concurrent.duration.DurationLong | |
import scala.concurrent.future | |
import scala.concurrent.promise |
NewerOlder