Skip to content

Instantly share code, notes, and snippets.

View ghidalgo3's full-sized avatar

Gustavo Hidalgo ghidalgo3

View GitHub Profile
@ghidalgo3
ghidalgo3 / gist:159f5dffe3319e148cbd
Last active August 29, 2015 14:08
crazy replacement
def findRegexReplaceMatch(s: String, zomRegex: collection.immutable.Seq[(Regex, Match => String)]) : String = {
import scala.language.implicitConversions
//I really needed the start and end points of a regex match, this makes it less awkward in expressions
implicit def match2tuple(m : Match) : (Int, Int) = (m.start, m.end)
//detects overlaps in a set of regions.
def overlaps(set :scala.collection.mutable.Set[(Int,Int)], region : (Int, Int)) : Boolean = {
set.exists { case (begin, end) =>
region._1 > begin && region._1 < end ||
region._2 > begin && region._2 < end
}
@ghidalgo3
ghidalgo3 / gist:25e0d88f5f495d47f73b
Created October 28, 2014 18:36
Slick db implicits
val table : Table[Product] = Products.products //this is the DTO
private def database(f : => Unit) = {
Database.forURL("jdbc:mysql://127.0.0.1:3306/products", driver = "com.mysql.jdbc.Driver") withSession {
implicit session => f
}
}
def add(product : Product) = {
database {//the compiler complains that there is no implicit session to run this with
@ghidalgo3
ghidalgo3 / regex replace
Created October 12, 2014 18:45
What should this do?
/** @return string with all occurrences of regex replaced with the paired string returned by supplying the match generated by the regex. Ensures recursive replacements cannot occur. */
def findRegexReplaceMatch(
s: String,
zomRegex: Seq[(Regex, Match => String)]
) : String = {
var t = s
for((regex, matchingFunc) <- zomRegex) {
t = regex.replaceAllIn(t, matchingFunc)
}
t
package s_mach.string
import scala.collection.mutable.ArrayBuffer
import scala.util.matching.Regex
trait WordSplitter {
def split(s: String) : Iterator[String]
/**
//submatrix determination for calculating determinants of matrices
def submatrix(i : Int, j : Int, matrix : Array[Array]) = {
//returns a new array of size n-1 with the ith index removed
def removeIndex(index : Int, arr: Array): Array = {
arr
.zipWithIndex //what you call if you need an indexed loop
.filter{ case (_,index) => index != r }
.map { case (value, _) => value }
}
removeIndex(i , matrix.map{ case row => removeIndex(j, row) } )
//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): Unit = {
val scanner = new Scanner(new File(fileName))
val concordanceList = new mutable.HashMap[String, Int]()
while(scanner hasNext) {
scanner.nextLine()
.split(" ")
.foreach {
word => if(concordanceList contains word) {