Skip to content

Instantly share code, notes, and snippets.

View frgomes's full-sized avatar

Richard Gomes frgomes

View GitHub Profile
@frgomes
frgomes / CustomIterator.scala
Last active April 5, 2017 00:41
Scala - Custom Iterator
import scala.collection.AbstractIterator
class CustomIterator[T,U](it: Iterator[T])(f: (T => U)) extends AbstractIterator[U] {
override def hasNext: Boolean = it.hasNext
override def next(): U = f(it.next)
}
private def resourceAsStream(resource: String): Iterator[(String, String)] = {
val stream : java.io.InputStream = getClass.getResourceAsStream(resource)
val it = scala.io.Source.fromInputStream( stream ).getLines
@frgomes
frgomes / DiffMaps.scala
Created June 13, 2016 16:26
Scala - difference of two maps
val x = Map(1 -> "a", 2 -> "b", 3 -> "c")
val y = Map(1 -> "a", 2 -> "b")
val diff : Map[Int, String] = x -- y.keySet
@frgomes
frgomes / grep.scala
Last active November 29, 2017 14:25
Scala - grep-like processing a list of inclusions and a list of exclusions
//-------------------------------------------------------------------------------------------------
// This would be something equivalent to:
// $ cat inputs.txt | fgrep -f includes.txt | fgrep -v -f excludes.txt | uniq | sort
//-------------------------------------------------------------------------------------------------
import scala.util.matching.Regex
def grep(inputs: Seq[String], includes: Set[Regex], excludes: Set[Regex], sort: Boolean = false, uniq: Boolean = false): Iterable[String] = {
def pass(s: String, set: Set[Regex]): Boolean = set.exists(r => r.findFirstIn(s).isDefined)
val grep1: Iterable[String] = if(includes.size==0) inputs else for { c <- inputs if(pass(c, includes)) } yield { c }
@frgomes
frgomes / JsonWrite.scala
Last active July 4, 2016 11:20
Scala - Serialize case class as JSON / Easily print case class contents.
// Jupyter Scala kernel magic
classpath.add("org.json4s" % "json4s-native_2.11" % "3.4.0")
import org.json4s._
import org.json4s.native.Serialization
implicit val formats = Serialization.formats(NoTypeHints)
case class Subject(article: String, page: Int)
case class Speaker(name: String, age: Int, subject: Subject)
val speaker = Speaker("John Doe", 45, Subject("Doing It Yourself", 200))
@frgomes
frgomes / xorg.conf.dual-identical-monitors
Last active July 15, 2016 22:48
Debian - /etc/X11/xorg.conf
Section "ServerLayout"
Identifier "aticonfig Layout"
Screen 0 "aticonfig-Screen[0]-0" 0 0
EndSection
Section "Files"
EndSection
Section "Module"
EndSection
@frgomes
frgomes / bash_50intellij.sh
Last active May 18, 2017 23:01
Bash - IntelliJ clear cache
#!/bin/sh
# IntelliJ can be sluggish when indexing big projects.
# So, I'm bashing IntelliJ on its head here with a huge sledgehammer.
function intellij_clean {
for home in $HOME/.IntelliJ* ;do
for dir in caches jars js_caches jsp_related_caches compiler index compile-server ;do
if [ -d $home/system/$dir ] ;then
rm -r -f $home/system/$dir
fi
@frgomes
frgomes / PatternMatching.java
Last active August 7, 2016 23:24
Java - Pattern matching pre-JDK8 lambda expressions... looking like Scala, sort of.
public enum Category {
DIR (true, false),
CLASS (false, true, true, ".class"),
JAR (false, true, true, ".jar"),
ZIP (false, true, true, ".zip"),
NATIVE (false, true, true, Platform.instance().natives()),
MANIFEST(false, false, false, "/MANIFEST.MF"),
__ (false, false); // should not use '_' since source code compatibility with JDK8 prevents it.
// http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.9
@frgomes
frgomes / MockingQuickfixjSession.scala
Last active August 16, 2016 10:53
Scala - Mocking a QuickFIX/J session
import EasyMock._
def session: quickfix.Session = {
val dictionaryXML = "/FIX42.xml"
val dictionary = new DataDictionary(getClass.getResourceAsStream(dictionaryXML))
"Dictionary version"-{
assert("FIX.4.2"==dictionary.getVersion)
}
@frgomes
frgomes / FakeQuickfixjHeader.scala
Created August 9, 2016 14:29
Scala - Populate QuickFIX/J header with some fake data
implicit class RichMessage(m: quickfix.Message) {
implicit def populate: quickfix.fix42.Message = {
import quickfix.field.BeginString
import quickfix.field.SenderCompID
import quickfix.field.SenderLocationID
import quickfix.field.SenderSubID
import quickfix.field.TargetCompID
import quickfix.field.TargetLocationID
import quickfix.field.TargetSubID
import quickfix.Message.Header
@frgomes
frgomes / RingBuffer.scala
Last active September 1, 2016 21:41
Scala - A simple, no-frills ring buffer
package info.rgomes
//--------------------------------------------------------------------------------
//NOTE: This implementation is "convenient" but it is O(n), which
// is bad if you need realtime (predictable!) performance.
//
// A much better implementation which is O(1) can be found at
// http://github.com/frgomes/scala-ring-buffer
//--------------------------------------------------------------------------------