Skip to content

Instantly share code, notes, and snippets.

View diegopacheco's full-sized avatar

Diego Pacheco diegopacheco

View GitHub Profile
class BeanShort(
@BeanProperty var name:String = "Nothing",
@BeanProperty var steps:List[String] = List("ZERO"),
@BeanProperty var args:Map[Int,String] = Map(1 -> "zero")
){
override def toString() : String = "Name: " + getName() + ", Args: " + getArgs() + ",Steps: " + getSteps()
}
@diegopacheco
diegopacheco / DSL.scala
Created January 7, 2012 03:15
Scala Implicits Conversions DSL
import java.util.Date
import java.util.Calendar
object ImplicitsConversions extends App {
class DateHelper(n:Int){
def days(when:String):Date = {
var date =Calendar.getInstance()
when match {
case "ago" => date.add(Calendar.DAY_OF_MONTH,-n)
@diegopacheco
diegopacheco / RecursionMatcher.scala
Created January 7, 2012 21:43
Scala Pattern Matcher Recursion
object RecursionMatcher extends App{
def factorial(i:BigInt):BigInt = i match {
case _ if i == 1 => i
case _ => i * factorial(i - 1)
}
for(i <- 1 to 10) printf("%s: %s\n", i, factorial(i))
}
@diegopacheco
diegopacheco / MatcherOnTuples.scala
Created January 7, 2012 21:58
Scala Pattern Matcher Tuples
object MatcherOnTuples extends App {
def pointRef(i:Any) = i match {
case (x,y, 55) => printf("x y and 55")
case (x,y) => printf("Point X %s Y %s \n",x,y)
case (3) => printf("This is 3")
case (x) => printf("Point X %s \n",x)
}
pointRef( (1,2) )
@diegopacheco
diegopacheco / matcher_hof.scala
Created January 7, 2012 22:10
Scala Pattern Matcher with High Order Functions
def whatTodo(on:String):Unit = on match {
case "Monday" => println("Hard Work")
case "Friday" => println("Lazy Work")
case "Saturday" => println("Fun")
case "Sunday" => println("REST")
case _ => println("Code Scala")
}
val days = List("Monday","Wendsday","Friday","Sunday","Saturday")
@diegopacheco
diegopacheco / tmhof.scala
Created January 7, 2012 22:14
Scala Pattern Matcher High Order Functions Explicit
days.foreach(whatTodo _)
// Or a even more explicit version
days.foreach( o => whatTodo(o) )
@diegopacheco
diegopacheco / advancedmatcher.scala
Created January 7, 2012 22:35
Scala Pattern Matcher Advanced
trait Color
case class Red(saturation: Int) extends Color
case class Green(saturation: Int) extends Color
case class Blue(saturation: Int) extends Color
def matcher(arg:Any): String = arg match {
case "Chowder" => "Make with clams"
case x: Int => "An Int with value " + x
case Red(100) => "Red sat 100"
@diegopacheco
diegopacheco / MatcherCaseClasses.scala
Created January 7, 2012 22:42
Scala Pattern Matcher Case Class
object MatcherCaseClasses extends App {
import scala.reflect.BeanProperty
trait DayOfWeek{
@BeanProperty var whatTodo:String
}
case class Monday(@BeanProperty var whatTodo:String) extends DayOfWeek
case class Saturday(@BeanProperty var whatTodo:String) extends DayOfWeek
case class Sunday(@BeanProperty var whatTodo:String) extends DayOfWeek
@diegopacheco
diegopacheco / MatcherThisVar.scala
Created January 7, 2012 22:50
Scala Pattern Matcher with var
object MatcherThisVar extends App {
class Sample{
val max = 100
val MIN = 0
def process(input:Int){
input match{
case this.max => println("This max 100")
case MIN => println("MIN 0")
@diegopacheco
diegopacheco / reduce.clj
Created January 10, 2012 01:40
Reduce + Clojure
(reduce + '(1 2 3 4 5 6)) ;;; Result will be 21