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
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) | |
} |
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
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())); |
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
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 } |
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
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) |
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
#!/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" "$@" | |
!# |
OlderNewer