Skip to content

Instantly share code, notes, and snippets.

View ryanlecompte's full-sized avatar

Ryan LeCompte ryanlecompte

View GitHub Profile
@ryanlecompte
ryanlecompte / gist:5497801
Created May 1, 2013 19:45
Tricky scala collections
// Beware of iterating/yielding over multiple collections with a Set in the middle.
// Due to Scala's desire to keep intermediate type, you'll inadvertantly remove
// duplicates from the inner collection. This may or may not be desirable depending
// on your use case.
// Note Set in the middle. This yields the correct value because the last collection has no duplicates.
scala> for (a <- List(1); b <- Set(1); c <- List(1,2)) yield (a,c)
res6: List[(Int, Int)] = List((1,1), (1,2))
// Note the Set in the middle. This gets rid of duplicates in List(1,1) due to the Set before it! This may or may not
➜ ~ scala
Welcome to Scala version 2.9.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_41).
Type in expressions to have them evaluated.
Type :help for more information.
scala> object & {
| def unapply[A](a: A): Option[(A, A)] = Some((a, a))
| }
defined module $amp
val s = "hello"
def as[A: Manifest](v: Any): Option[A] = {
if (manifest[A].erasure.isInstance(v)) Some(v.asInstanceOf[A])
else None
}
as[Int](s).foreach { _ => println("worked as int") }
as[String](s).foreach { _ => println("worked as string") }
@ryanlecompte
ryanlecompte / gist:5210745
Last active August 25, 2021 13:40
mutable.ArrayBuffer vs immutable.Vector
import scala.collection._
import com.twitter.util._
scala> val buffer = mutable.ArrayBuffer.empty[Int]
scala> println(Time.measure { (0 to 50000000).foreach { buffer += _ } }.inMillis)
17610
scala> var vector = immutable.Vector.empty[Int]
scala> println(Time.measure { (0 to 50000000).foreach { vector :+= _ } }.inMillis)
7865
def foreachUntilDup[A, U](c: Seq[A])(f: A => U) {
@tailrec
def traverse(seen: Set[A], remaining: Seq[A]) {
remaining.headOption match {
case Some(v) if !seen.contains(v) =>
f(v)
traverse(seen + v, remaining.tail)
case _ => // done
}
}
scala> def foo(f: => Unit) {
| val saved = f _
| println("saved it, now going to invoke it")
| saved()
| }
foo: (f: => Unit)Unit
scala> foo { println("haha!") }
saved it, now going to invoke it
haha!
@ryanlecompte
ryanlecompte / gist:4600352
Created January 23, 2013 00:21
Stream.continually vs. traditional while
scala> benchmark { var x = 0; Stream.continually({x += 1; x}).takeWhile(_ < 10000000).foreach(_ / 2) }
res7: Long = 823
scala> benchmark { var x = 0; while (x < 10000000) { x += 1; x / 2 } }
res8: Long = 1
@ryanlecompte
ryanlecompte / gist:4600222
Created January 23, 2013 00:07
Stream.continually from Kafka
./core/src/main/scala/kafka/message/ByteBufferMessageSet.scala
65: Stream.continually(compressed.read(intermediateBuffer)).takeWhile(_ > 0).foreach { dataRead =>
./core/src/main/scala/kafka/producer/async/ProducerSendThread.scala
65: Stream.continually(queue.poll(scala.math.max(0, (lastSend + queueTime) - SystemTime.milliseconds), TimeUnit.MILLISECONDS))
@ryanlecompte
ryanlecompte / gist:4573499
Created January 19, 2013 16:29
silly jetty BlockingArrayQueue doesn't block!
scala> import org.eclipse.jetty.util.BlockingArrayQueue
import org.eclipse.jetty.util.BlockingArrayQueue
scala> val q = new BlockingArrayQueue[Int](3)
q: org.eclipse.jetty.util.BlockingArrayQueue[Int] = []
scala> q.put(1)
scala> q.put(2)
@ryanlecompte
ryanlecompte / sorted_array.rb
Created October 3, 2012 18:30
SortedArray backed by a binary search for insertion/query
require 'delegate'
# SortedArray is backed by a binary search when inserting elements
# via #<< as well as querying for an element with #include?
#
# Example:
# a = SortedArray.new
# 10.times { a << rand(100) }
# puts a # => [3, 24, 30, 40, 42, 43, 49, 67, 81, 88]
# a.include?(49) # => true