Skip to content

Instantly share code, notes, and snippets.

View frgomes's full-sized avatar

Richard Gomes frgomes

View GitHub Profile
@frgomes
frgomes / RegexOnSeveralFiles.scala
Last active October 18, 2016 23:25
Scala - Regex on contents of several files
/** my projects are named GuinePigModels, GuineaPigController, etc */
private val myProjectsPrefix = "GuineaPig"
/** jar names appear on lines which may end by a comma followed by a line continuation mark */
private val jarRef = """^([A-Za-z0-9_.\-]+)(\.jar)(,\\)*""".r
/** list files recursively */
private def listOfFiles(dir: File): Seq[File] = {
if (dir.exists && dir.isDirectory) {
dir.listFiles.filter(f => f.isFile).toList
@frgomes
frgomes / EvaluationSupport.scala
Created October 16, 2015 12:23
Scala / SBT - Evaluation and task execution helpers
trait EvaluationSupport {
import sbt._
protected def fail(errorMessage: String, state: State): Nothing = {
state.log.error(errorMessage)
throw new IllegalArgumentException()
}
protected def log(implicit state: State) = state.log
@frgomes
frgomes / EarlyInitializationAfterNew.scala
Last active May 25, 2017 22:22
Scala - Early initialization block after new
trait Logger {
def log(msg: String)
def info(msg: String) { log("INFO: " + msg) }
def warn(msg: String) { log("WARN: " + msg) }
def severe(msg: String) { log("SEVERE: " + msg) }
}
trait FileLogger extends Logger {
val filename: String
@frgomes
frgomes / EarlyInitializationAfterExtends.scala
Last active May 25, 2017 22:22
Scala - Early initialization block after extends
trait Logger {
def log(msg: String)
def info(msg: String) { log("INFO: " + msg) }
def warn(msg: String) { log("WARN: " + msg) }
def severe(msg: String) { log("SEVERE: " + msg) }
}
trait FileLogger extends Logger {
val filename: String
@frgomes
frgomes / translate.scala
Last active July 8, 2017 21:38
Scala SBT Scripting - Run Scala program from SBT and parses command line arguments
#!/bin/bash
#-*- mode: scala; -*-
exec java \
-Dsbt.main.class=sbt.ScriptMain \
-Dsbt.boot.directory=~/.sbt/boot \
-Dsbt.log.noformat=true \
-jar $(which sbt-launch.jar) $0 "+ $*" # see: https://github.com/sbt/sbt/issues/2257
!#
/***
@frgomes
frgomes / install-jupyter.sh
Last active April 27, 2019 12:46
Debian - Install Jupyter and Scala kernel on Debian Stretch
#!/bin/bash
## NOTE: This script is deprecated!
## See: https://github.com/frgomes/bash-scripts/blob/master/user-install/install-jupyter.sh
@frgomes
frgomes / SourceAsStream.scala
Last active October 30, 2015 13:12
Scala - Read file as Stream
def matches(text: String, is: InputStream = System.in): Int =
scala.io.Source.fromInputStream(is)
.getLines
.toStream
.map(line => if(line.contains(text)) 1 else 0)
.reduce(_ + _)
@frgomes
frgomes / postactivate
Last active July 26, 2016 21:55
virtualenv - ~/.virtualenvs/j8s11/bin/postactivate
#!/bin/bash
# ---- Location where all tools are installed ----
TOOLS_HOME=/opt/developer
# ---- These tools are available in the PATH ----
JAVA_VERSION=1.8.0_101
SCALA_VERSION_MAJOR=2.11
SCALA_VERSION=${SCALA_VERSION_MAJOR}.8
ANT_VERSION=1.9.6
@frgomes
frgomes / AnyToDouble.scala
Last active January 23, 2022 23:15
Scala - Converts Any to Double, to LocalDate and Date
// this flavour is pure magic...
def toDouble: (Any) => Double = { case i: Int => i case f: Float => f case d: Double => d }
// whilst this flavour is longer but you are in full control...
object any2Double extends Function[Any,Double] {
def apply(any: Any): Double =
any match { case i: Int => i case f: Float => f case d: Double => d }
}
// like when you can invoke any2Double from another similar conversion...
@frgomes
frgomes / MonadExtractor.scala
Last active November 27, 2015 23:35
Scala - For comprehension monad with extractor magic
for(row <- sheet.rows if (row.index == index); Cell(cindex, data) <- row.cells) yield { (cindex, data) }