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
// cheap way to deep clone an arbitrarily-deep nested array | |
def deepCopy[T](array: Array[T]): Array[T] = { | |
import java.io._ | |
val bos = new ByteArrayOutputStream() | |
val oos = new ObjectOutputStream(bos) | |
oos.writeObject(array) | |
val bis = new ByteArrayInputStream(bos.toByteArray) | |
val ois = new ObjectInputStream(bis) | |
ois.readObject().asInstanceOf[Array[T]] | |
} |
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 net.liftweb.json._ | |
import net.liftweb.json._ | |
scala> import net.liftweb.json.JsonDSL._ | |
import net.liftweb.json.JsonDSL._ | |
scala> implicit val formats = DefaultFormats | |
formats: net.liftweb.json.DefaultFormats.type = net.liftweb.json.DefaultFormats$@158faa04 | |
scala> val json = """[1,2,3,4,5]""" |
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 implementation of simple linear regression as implemented in Ruby | |
// in the blog post: http://www.sharethrough.com/2012/09/linear-regression-using-ruby/ | |
import scala.io.Source | |
class SimpleLinearRegression(xs: Seq[Double], ys: Seq[Double]) { | |
require(xs.size == ys.size, "xs and ys must be same length") | |
def mean(values: Seq[Double]): Double = values.sum / values.size |
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.mutable | |
/** | |
* Bounded priority queue trait that is intended to be mixed into instances of | |
* scala.collection.mutable.PriorityQueue. By default PriorityQueue instances in | |
* Scala are unbounded. This trait modifies the original PriorityQueue's | |
* enqueue methods such that we only retain the top K elements. | |
* The top K elements are defined by an implicit Ordering[A]. | |
* @author Ryan LeCompte ([email protected]) | |
*/ |
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
// Modified version of the example from http://hseeberger.github.io/blog/2013/05/31/implicits-unchained-type-safe-equality-part2/ that shows that you don't need to use implicit evidence parameters. | |
object TypeWiseBalancedEquality { | |
implicit class Equal[A](val left: A) { | |
def ===[B](right: B)(implicit equality: Equality[A, B]): Boolean = | |
equality.areEqual(left, right) | |
} | |
trait Equality[A, B] { | |
def areEqual(left: A, right: B): Boolean | |
} |
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
@tailrec | |
def findClickableParent(nodeInfo: AccessibilityNodeInfo): AccessibilityNodeInfo = { | |
nodeInfo match { | |
case null => null | |
case n if n.isClickable => n | |
case n => findClickableParent(n.getParent) | |
} | |
} | |
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.3 (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> val extractor: Int => Option[Int] = { i => println("checking i = %d".format(i)); if (i == 10) Some(i) else None } | |
extractor: Int => Option[Int] = <function1> | |
scala> val seq = Seq.range(1,1000000) | |
seq: Seq[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148 |
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
> dependency-tree | |
[info] Updating {file:/Users/ryan/dev/research/spark-new/}core... | |
[info] Resolving cglib#cglib-nodep;2.2.2 ... | |
[info] Done updating. | |
[info] org.spark-project:spark-core_2.9.3:0.8.0-SNAPSHOT [S] | |
[info] +-asm:asm-all:3.3.1 | |
[info] +-cc.spray:spray-can:1.0-M2.1 [S] | |
[info] | +-cc.spray:spray-io:1.0-M2.1 [S] | |
[info] | +-cc.spray:spray-util:1.0-M2.1 [S] | |
[info] | |
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
// based on http://blog.jessitron.com/2013/02/scala-talking-about-yourself.html | |
trait Foo[A <: Foo[A]] { self: A => | |
def |[B](x: B): (A, B) = (this, x) | |
} | |
case class Text(t: String) extends Foo[Text] | |
Text("scala is cool")|100 |
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
// Usage examples: | |
val a = Vector(1,2,3).zipMany(Vector(1,2,3), Vector(5,6,7), Vector(8,9,10), Vector(11,12,13)) | |
Vector(Vector(1, 1, 5, 8, 11), Vector(2, 2, 6, 9, 12), Vector(3, 3, 7, 10, 13)) | |
a.unzipMany | |
Vector(Vector(1, 2, 3), Vector(1, 2, 3), Vector(5, 6, 7), Vector(8, 9, 10), Vector(11, 12, 13)) | |
// Implementation | |
import scala.collection._ | |
import scala.collection.generic.CanBuildFrom |