Skip to content

Instantly share code, notes, and snippets.

View frgomes's full-sized avatar

Richard Gomes frgomes

View GitHub Profile
@frgomes
frgomes / SampleFold.scala
Last active August 29, 2015 14:14
Scala - Sample usage of fold() instead of getOrElse()
// 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 August 29, 2015 14:16
Scala - Sample usage of foldLeft
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 August 29, 2015 14:16
Scala - Transforms an Object into a Map
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 August 29, 2015 14:16
Scala - String to an absolute File
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 / PropertyConverters.scala
Created March 1, 2015 15:50
Scala - Convert java.util.Properties to Map[String, Option[Map]]
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 / UriSchemaConverters.scala
Created March 1, 2015 16:05
Scala - Convert String to URI or Proxy
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 / PropertyChangeListenerAdapter.java
Last active August 29, 2015 14:22
Java - ChangeListener adapter to PropertyChangeListener
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 August 29, 2015 14:27
Scala - Reporting validations
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 August 29, 2015 14:28
Scala - Generic way to convert data structures
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 September 7, 2015 15:38
Scala - Converts a list of pom.xml files onto list of variables containing versions and dependencies
#!/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" "$@"
!#