Skip to content

Instantly share code, notes, and snippets.

View ryanlecompte's full-sized avatar

Ryan LeCompte ryanlecompte

View GitHub Profile
// 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]]
}
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]"""
// 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
@ryanlecompte
ryanlecompte / gist:5746241
Last active October 31, 2019 05:05
Bounded priority queue in Scala
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 (lecompte@gmail.com)
*/
// 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
}
@ryanlecompte
ryanlecompte / gist:5630168
Created May 22, 2013 19:22
interop with java
@tailrec
def findClickableParent(nodeInfo: AccessibilityNodeInfo): AccessibilityNodeInfo = {
nodeInfo match {
case null => null
case n if n.isClickable => n
case n => findClickableParent(n.getParent)
}
}
➜ 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
@ryanlecompte
ryanlecompte / gist:5608353
Created May 19, 2013 17:31
Spark 0.8 dependency tree generated from sbt-dependency-graph
> 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] |
@ryanlecompte
ryanlecompte / gist:5537190
Last active December 17, 2015 02:38
scala self referential types
// 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
@ryanlecompte
ryanlecompte / gist:5537188
Last active December 17, 2015 02:38
Pimping Iterable to have zipMany/unzipMany operations
// 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