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
// 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 |
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
➜ ~ 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 |
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
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") } |
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.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 |
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
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 | |
} | |
} |
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
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! |
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
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 |
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
./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)) |
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
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) |
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
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 |