Skip to content

Instantly share code, notes, and snippets.

@netologist
Last active December 15, 2015 13:48
Show Gist options
  • Save netologist/5269426 to your computer and use it in GitHub Desktop.
Save netologist/5269426 to your computer and use it in GitHub Desktop.
fonksiyonel programlama
def makeIncrementer(inc: Int): (Int => Int) = (x: Int) => x + inc
val a = makeIncrementer(10)
// a: (Int) => Int =
val b = a(25)
// res4: Int = 35
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)
}
def add(x:Int, y:Int) = x + y
val addCurried = Function.curried(add _)
add(1, 2) // 3
addCurried(1)(2) // 3
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)
# "IMPERATIVE TERMINAL"
$ ./program1
$ ./program2 --param1=1
$ ./program3
# "FUNCTIONAL TERMINAL"
$ ./program1 | ./program2 --param1=1 | ./program3
def apply(f: Int => String, v: Int) = f(v)
def layout[A](x: A) = "[" + x.toString() + "]"
// println( apply( layout, 10) )
//[10]
(x: Int) => x + 1
// tuples example
val tuple = new Tuple3(1, "hello", Console)
// list example
val days = List("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
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)
}
def nonPureFunc(x : Int, y : Int) = println(x + y)
// because write to screen (side-effect alert!)
def pureFunc(x : Int, y : Int) = x + y
def whileLoop(cond : =>Boolean)(block : =>Unit) : Unit =
if(cond) {
block
whileLoop(cond)(block)
}
//whileLoop(i < a.length) {
// println(a(i))
// i += 1
//}
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
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