Skip to content

Instantly share code, notes, and snippets.

View ryanlecompte's full-sized avatar

Ryan LeCompte ryanlecompte

View GitHub Profile
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 {
// 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.
*/
@johnynek
johnynek / catseq.scala
Created December 16, 2013 05:05
CatSeq: O(1) concatenation of sequences. What is this called?
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)
@andrewconner
andrewconner / TrimString.scala
Created August 16, 2013 16:51
Trimming a string at a certain number of bytes
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)
}
@johnynek
johnynek / compression.scala
Created August 12, 2013 05:40
Compress Lists of any item (as long as they are immutable and have sane equals and hashCode). This is basically the Lempel-Ziv algorithm
/**
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,
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`
@slyphon
slyphon / KeyValueResult.scala
Created July 11, 2013 03:04
A super useful class for dealing with Futures and collections and dealing with results from backend data stores. Shout out to @caniszczyk for giving the OK to share this with the community. Authors: Jeremy Cloud, Kevin Oliver, Glen Sanford, and Evan Meagher
/**
* 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
@patriknw
patriknw / LoggingMailbox.scala
Last active January 5, 2023 08:12
Logs the mailbox size when exceeding the configured limit. Implemented in Scala and Java. Copy one of them to your project and define the configuration. This code is licensed under the Apache 2 license.
/**
* 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 }
@slyphon
slyphon / clipboard.scala
Created May 28, 2013 03:04
This, more than anything, has improved my productivity an embarassing amount (w/ the scala repl)
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()
@Mortimerp9
Mortimerp9 / Retry.scala
Last active July 3, 2022 22:35
A retry implementation for Scala, a bit of explanations here: http://pierreandrews.net/posts/retry-fail-scala.html
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