Skip to content

Instantly share code, notes, and snippets.

Avatar

Richard Gomes frgomes

View GitHub Profile
@frgomes
frgomes / bootstrap_setup.py
Last active Aug 27, 2017
Python - Installing packages programmatically
View bootstrap_setup.py
#!/usr/bin/env python
from __future__ import print_function
REQUIREMENTS = [ 'distribute', 'version', 'Cython', 'sortedcollection' ]
try:
from setuptools import find_packages
from distutils.core import setup
from Cython.Distutils import build_ext as cython_build
import sortedcollection
@frgomes
frgomes / ChainingTraits.scala
Last active Jun 29, 2016
Scala - Chaining traits
View ChainingTraits.scala
trait Irrelevant {}
trait Base {
def text: String = "Base"
def enter: () => Any =
() => {
println("enter: Some logic")
}
@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 / FileSystem.scala
Last active Mar 9, 2018
Scala - List files recursively
View FileSystem.scala
trait FileSystem {
import java.io.File
import scala.util.matching.Regex
final def listFiles(base: File, recursive: Boolean = true): Seq[File] = {
val files = base.listFiles
val result = files.filter(_.isFile)
result ++
files
.filter(_.isDirectory)
@frgomes
frgomes / Enum.scala
Last active May 25, 2017
Scala - Enumerations made easy... well... more or less
View Enum.scala
// THIS IS Scala 2.11
// see: https://github.com/d6y/enumeration-examples
object Status {
sealed abstract class enum(val id: Int, val name: String) extends Ordered[enum] {
def compare(that: enum) = this.id - that.id
}
case object Starting extends enum(0, "Starting")
case object Running extends enum(1, "Running")
case object Finished extends enum(2, "Finished")
val values = Seq(Starting, Running, Finished)
@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 / 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 / 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)
}