Skip to content

Instantly share code, notes, and snippets.

Avatar

Richard Gomes frgomes

View GitHub Profile
@frgomes
frgomes / SampleFold.scala
Last active Aug 29, 2015
Scala - Sample usage of fold() instead of getOrElse()
View SampleFold.scala
// This sample demonstrates how fold(...) can be employed instead
// of getOrElse(...) in cases you need to process the value of an
// Option.
def informTaxDue(tax: Option[Float]) : String =
tax.fold("not declared") {
v: Float =>
if(v==0.0) "nothing to pay" else s"tax due is ${v}"
}
@frgomes
frgomes / SampleFoldLeft.scala
Last active Aug 29, 2015
Scala - Sample usage of foldLeft
View SampleFoldLeft.scala
val init="aaa"
val s1 = List("b", "c", "d").foldLeft(init) { (acc, item) => acc + item }
println(s1) // prints: aaabcd
val s2 = ( "aaa" /: List("b", "c", "d")) { (acc, item) => acc + item }
println(s2) // prints: aaabcd
@frgomes
frgomes / Object2Map.scala
Last active Aug 29, 2015
Scala - Transforms an Object into a Map
View Object2Map.scala
trait Object2Map {
implicit class RichObject(o: AnyRef) {
def asMap : Map[String, Option[Any]] =
(Map[String, Option[Any]]() /: o.getClass.getDeclaredFields) {
(acc, field) =>
field.setAccessible(true)
acc + (field.getName -> Option(field.get(o)))
}
}
@frgomes
frgomes / FileSystemConversions.scala
Last active Aug 29, 2015
Scala - String to an absolute File
View FileSystemConversions.scala
object FileSystemConverters extends FileSystemConverters
trait FileSystemConverters {
import java.io.File
implicit class RichOptionString2OptionFile(path: Option[String]) {
implicit def asFile: Option[File] =
Option(
path
.getOrElse(".")
@frgomes
frgomes / UriSchemaConverters.scala
Created Mar 1, 2015
Scala - Convert String to URI or Proxy
View UriSchemaConverters.scala
object UriSchemaConverters extends UriSchemaConverters
trait UriSchemaConverters {
import java.net.{InetSocketAddress, Proxy}
import com.netaporter.uri.Uri
implicit class RichOptionString2OptionUri(s: Option[String]) {
implicit def asUri: Option[Uri] = Option(s.getOrElse("/").asUri)
}
@frgomes
frgomes / PropertyConverters.scala
Created Mar 1, 2015
Scala - Convert java.util.Properties to Map[String, Option[Map]]
View PropertyConverters.scala
object PropertyConverters extends PropertyConverters
trait PropertyConverters {
import java.util.Properties
implicit class RichOptionString2File(props: Properties) {
implicit def asMap: Map[String, Option[Any]] =
(Map[String, Option[Any]]() /: props.keySet.toArray) {
(acc, key) =>
acc + (key.toString -> Option(props.get(key)))
@frgomes
frgomes / PropertyChangeListenerAdapter.java
Last active Aug 29, 2015
Java - ChangeListener adapter to PropertyChangeListener
View PropertyChangeListenerAdapter.java
class PropertyChangeListenerAdapter implements PropertyChangeListener {
final private ChangeListener changeListener;
public PropertyChangeListenerAdapter(ChangeListener changeListener) {
this.changeListener = changeListener;
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
changeListener.stateChanged(new ChangeEvent(evt.getSource()));
@frgomes
frgomes / ReportingValidationsLately.scala
Last active Aug 29, 2015
Scala - Reporting validations
View ReportingValidationsLately.scala
def transmit[R: ClassTag](message: R, validations: (R => Seq[Option[Throwable]])*) = {
val conditions: Seq[Option[Throwable]] =
(Seq.empty[Option[Throwable]] /: (for { validation <- validations } yield { validation(message)} )) {
case (acc, item) => acc ++ item }
val lines =
(0 /: conditions.flatten) {
case (n, e) =>
val line = n + 1
log.error(s"VALIDATION #${line} :: " + e.getMessage + s"\n" + e.getStackTraceString)
line }
@frgomes
frgomes / ConvertDataStructures.scala
Last active Aug 29, 2015
Scala - Generic way to convert data structures
View ConvertDataStructures.scala
import collection.generic.CanBuildFrom
import collection.immutable.TreeMap
object test {
class TraversableW[A](t: Traversable[A]) {
def as[CC[X] <: Traversable[X]](implicit cbf: CanBuildFrom[Nothing, A, CC[A]]): CC[A] = t.map(identity)(collection.breakOut)
def to[Result](implicit cbf: CanBuildFrom[Nothing, A, Result]): Result = t.map(identity)(collection.breakOut)
}
implicit def ToTraverseableW[A](t: Traversable[A]): TraversableW[A] = new TraversableW[A](t)
@frgomes
frgomes / pom2deps.scala
Last active Sep 7, 2015
Scala - Converts a list of pom.xml files onto list of variables containing versions and dependencies
View pom2deps.scala
#!/bin/bash
#-*- mode: scala; -*-
######################################################################
# This Scala scripts extracts versions and dependencies from a list
# of pom.xml files passed as argument to the script.
######################################################################
exec scala "$0" "$@"
!#