Skip to content

Instantly share code, notes, and snippets.

View frgomes's full-sized avatar

Richard Gomes frgomes

View GitHub Profile
@frgomes
frgomes / bootstrap_setup.py
Last active August 27, 2017 17:07
Python - Installing packages programmatically
#!/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 June 29, 2016 06:42
Scala - Chaining traits
trait Irrelevant {}
trait Base {
def text: String = "Base"
def enter: () => Any =
() => {
println("enter: Some logic")
}
@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 / FileSystem.scala
Last active March 9, 2018 10:17
Scala - List files recursively
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 22:03
Scala - Enumerations made easy... well... more or less
// 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 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)
}