Last active
December 15, 2015 13:48
-
-
Save netologist/5269426 to your computer and use it in GitHub Desktop.
fonksiyonel programlama
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 makeIncrementer(inc: Int): (Int => Int) = (x: Int) => x + inc | |
val a = makeIncrementer(10) | |
// a: (Int) => Int = | |
val b = a(25) | |
// res4: Int = 35 |
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 scala.util.continuations._ | |
def reify[A, M[+_]](x : => A @cpsParam[M[A], M[A]])(implicit monad : Monad[M]) : M[A] = | |
reset { monad.unit(x) } | |
class Reflective[+A, M[_]](m : M[A], monad : Monad[M]) { | |
def reflect[B]() : A @cpsParam[M[B], M[B]] = { | |
shift { (k : A => M[B]) => | |
monad.bind(m, k) | |
} | |
} | |
} | |
implicit def reflective[A](xs : Option[A])(implicit monad : Monad[Option]) = | |
new Reflective[A, Option](xs, monad) | |
implicit def reflective[A](xs : List[A])(implicit monad : Monad[List]) = | |
new Reflective[A, List](xs, monad) | |
reify { | |
val left = List("x", "y", "z").reflect[(String, Int)] | |
val right = List(4, 5, 6).reflect[(String, Int)] | |
(left, right) | |
} |
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 add(x:Int, y:Int) = x + y | |
val addCurried = Function.curried(add _) | |
add(1, 2) // 3 | |
addCurried(1)(2) // 3 |
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 divisibleby(factor : Int) (value : Int) = value % factor == 0 | |
val evens = (1 to 10).filter(divisibleby(2)) | |
// divisibleby: (factor: Int)(value: Int)Boolean | |
// evens: scala.collection.immutable.IndexedSeq[Int] = Vector(2, 4, 6, 8, 10) |
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
# "IMPERATIVE TERMINAL" | |
$ ./program1 | |
$ ./program2 --param1=1 | |
$ ./program3 | |
# "FUNCTIONAL TERMINAL" | |
$ ./program1 | ./program2 --param1=1 | ./program3 |
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 apply(f: Int => String, v: Int) = f(v) | |
def layout[A](x: A) = "[" + x.toString() + "]" | |
// println( apply( layout, 10) ) | |
//[10] |
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
(x: Int) => x + 1 |
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
// tuples example | |
val tuple = new Tuple3(1, "hello", Console) | |
// list example | |
val days = List("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday") |
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 Monad[M[_]] { | |
def unit[A](x : A) : M[A] | |
def bind[A, B](m : M[A], f : A => M[B]) : M[B] | |
} | |
implicit object OptionMonad extends Monad[Option] { | |
def unit[A](x : A) = Some(x) | |
def bind[A, B](m : Option[A], f : A => Option[B]) = m.flatMap(f) | |
} | |
implicit object ListMonad extends Monad[List] { | |
def unit[A](x : A) = List(x) | |
def bind[A, B](m : List[A], f : A => List[B]) = m.flatMap(f) | |
} |
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 nonPureFunc(x : Int, y : Int) = println(x + y) | |
// because write to screen (side-effect alert!) |
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 pureFunc(x : Int, y : Int) = x + y |
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 whileLoop(cond : =>Boolean)(block : =>Unit) : Unit = | |
if(cond) { | |
block | |
whileLoop(cond)(block) | |
} | |
//whileLoop(i < a.length) { | |
// println(a(i)) | |
// i += 1 | |
//} |
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 whileLoop(cond: =>Boolean, block: =>Unit) : Unit = | |
if(cond) { | |
block | |
whileLoop(cond, block) | |
} | |
//scala> val a = Array(1, 2, 3) | |
//scala> var i = 0 | |
//scala> whileLoop(i < a.length, { println(i); i += 1 }) | |
//1 | |
//2 | |
//3 |
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 add(x:Int)(y:Int) = x + y | |
val addUncurried = Function.uncurried(add _) | |
add(3)(4) // 7 | |
addUncurried(3, 4) // 7 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment