Skip to content

Instantly share code, notes, and snippets.

View lancegatlin's full-sized avatar

Lance Gatlin lancegatlin

View GitHub Profile
Alice's Adventures in Wonderland
ALICE'S ADVENTURES IN WONDERLAND
Lewis Carroll
THE MILLENNIUM FULCRUM EDITION 3.0
@lancegatlin
lancegatlin / submatrix
Last active August 29, 2015 14:06 — forked from ghidalgo3/submatrix
asdf
// note: more efficient way to store matrix is a class that uses internal Buffer[Double] of size row * columns
//submatrix determination for calculating determinants of matrices
def submatrix[A](i : Int, j : Int, matrix : Array[Array[A]]) = {
//returns a new array of size n-1 with the ith index removed
def remove[A](index : Int, arr: Seq[A]): Seq[A] = {
// Note: removing the ith index from a sequence is considered highly inefficient and to be avoided whenever possible
arr.take(index) ++ arr.drop(index+1)
}
import scala.io.Source
//read a file and build a concordance list of the tokens in the file
//there's gotta be a nicer way to do this
def two(fileName:String) = {
val chunkSize = 1024
Source.fromFile(fileName).getLines()
// read in chunkSize lines into memory at once
// From: http://stackoverflow.com/questions/6751463/iterate-over-lines-in-a-file-in-parallel-scala
.grouped(chunkSize)
object FutureOps extends FutureOps
trait FutureOps {
def toTry[A](self: Future[A])(implicit ec: ExecutionContext): Future[Try[A]] = {
val p = Promise[Try[A]]()
self onComplete { case result => p.success(result) }
p.future
}
def takeFirstMatch[A](futures: Traversable[Future[A]], predicate: A => Boolean)(implicit ec:ExecutionContext): Future[Option[A]] = {
if(futures.nonEmpty) {
val promise = Promise[Option[A]]()