This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Irrelevant {} | |
trait Base { | |
def text: String = "Base" | |
def enter: () => Any = | |
() => { | |
println("enter: Some logic") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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}" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object FileSystemConverters extends FileSystemConverters | |
trait FileSystemConverters { | |
import java.io.File | |
implicit class RichOptionString2OptionFile(path: Option[String]) { | |
implicit def asFile: Option[File] = | |
Option( | |
path | |
.getOrElse(".") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
OlderNewer