Created
December 15, 2010 14:27
-
-
Save gclaramunt/741994 to your computer and use it in GitHub Desktop.
Scala demo funcional
This file contains hidden or 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
package demo | |
//mejor en la REPL | |
class FP { | |
/* | |
new Function1[Int, Int] { | |
def apply(x: Int): Int = x + 1 | |
} | |
*/ | |
(x: Int) => x + 1 | |
(x: Int, y: Int) => "(" + x + ", " + y + ")" | |
() => { System.getProperty("user.dir") } | |
//HOF | |
// con currying | |
def o1[A,B,C](f:A=>B)(g:B=>C)(x:A)=g(f(x)) | |
//o currying con closures | |
def o2[A,B,C](f:A=>B)(g:B=>C)={ x => g(f(x)) } | |
//o simplemente usando el metodo compose del objeto funcion | |
def o3[A,B,C](f:A=>B)(g:B=>C)=g compose f | |
//case classes | |
abstract class Term | |
case class Var(name: String) extends Term | |
case class Fun(arg: String, body: Term) extends Term | |
case class App(f: Term, v: Term) extends Term | |
//no precisa "new" | |
Fun("x", Fun("y", App(Var("x"), Var("y")))) | |
//los parametros del constructor son publicos | |
val x = Var("x") | |
Console.println(x.name) | |
//genera un equals (por igualdad estructural) y un toString | |
val x1 = Var("x") | |
val x2 = Var("x") | |
val y1 = Var("y") | |
println("" + x1 + " == " + x2 + " => " + (x1 == x2)) | |
println("" + x1 + " == " + y1 + " => " + (x1 == y1)) | |
//en general se usan con pattern matching | |
object TermTest extends Application { | |
def printTerm(term: Term) { | |
term match { | |
case Var(n) => | |
print(n) | |
case Fun(x, b) => | |
print("^" + x + ".") | |
printTerm(b) | |
case App(f, v) => | |
Console.print("(") | |
printTerm(f) | |
print(" ") | |
printTerm(v) | |
print(")") | |
} | |
} | |
def isIdentityFun(term: Term): Boolean = term match { | |
case Fun(x, Var(y)) if x == y => true | |
case _ => false | |
} | |
val id = Fun("x", Var("x")) | |
val t = Fun("x", Fun("y", App(Var("x"), Var("y")))) | |
printTerm(t) | |
println | |
println(isIdentityFun(id)) | |
println(isIdentityFun(t)) | |
} | |
//pattern matching | |
object MatchTest1 extends Application { | |
def matchTest(x: Int): String = x match { | |
case 1 => "one" | |
case 2 => "two" | |
case _ => "many" | |
} | |
println(matchTest(3)) | |
} | |
object MatchTest2 extends Application { | |
def matchTest(x: Any): Any = x match { | |
case 1 => "one" | |
case "two" => 2 | |
case y: Int => "scala.Int" | |
} | |
println(matchTest("two")) | |
} | |
//permite definir Extractor Objects (unnapply) | |
object Twice { | |
def apply(x: Int): Int = x * 2 | |
def unapply(z: Int): Option[Int] = if (z%2 == 0) Some(z/2) else None | |
} | |
object TwiceTest extends Application { | |
val x = Twice(21) | |
x match { case Twice(n) => Console.println(n) } // prints 21 | |
} | |
//comprehensions | |
object ComprehensionTest1 extends Application { | |
def even(from: Int, to: Int): List[Int] = | |
for (i <- List.range(from, to) if i % 2 == 0) yield i | |
Console.println(even(0, 20)) | |
} | |
object ComprehensionTest2 extends Application { | |
def foo(n: Int, v: Int) = | |
for (i <- 0 until n; | |
j <- i + 1 until n if i + j == v) yield | |
Pair(i, j); | |
foo(20, 32) foreach { | |
case (i, j) => | |
println("(" + i + ", " + j + ")") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment