import scala.collection.mutable object Sort { def bubble(input: mutable.ListBuffer[Int]): Unit = { if(input.isEmpty) () else { for(i <- input.indices) { for(j <- input.indices.drop(i).reverse) { val a = input(i) val b = input(j) if(a > b) { val buffer = a input(i) = input(j) input(j) = buffer } } } } } } val arr = mutable.ListBuffer(3, 2, 6, 4, 6, 1) // ListBuffer(3, 2, 6, 4, 6, 1) Sort.bubble(arr) arr // ListBuffer(1, 2, 3, 4, 6, 6)
object Math { var pi = 3.14d def circleArea(radius: Double) = radius * radius * pi } Math.circleArea(10) // 314.0 Math.pi = 3.1415926d Math.circleArea(10) // 314.15926
object Math { def circleArea(radius: Double, pi: Double) = radius * radius * pi } Math.circleArea(10, 3.14) // 314.00
object Math { private var pi = 3.14d def setPI(newPi: Double) = pi = newPi def getPI() = pi def circleArea(radius: Double) = radius * radius * getPi() }
var sumOfValues = 0 def findSum(list: Seq[Int]): Unit = for(item <- list) { sumOfValues = sumOfValues + item }
def findSumImmutable(list: Seq[Int], sumOfValues: Int = 0): Int = if(list.isEmpty) sumOfValue else findSumImmutable(list.tail, sumOfValues + list.head)
val values = Seq(1, 2) findSumImmutable(values) // 3 findSumImmutable(Seq(1, 2)) // 3 3
object Math { def sqr(n: Int): Int = n * n } object ListFunctor { def map[A, B](f: A => B): List[A] => List[B] = _.map(f) } val sqr: Int => Int = Math.sqr _ val sqrList: List[Int] => List[Int] = ListFunctor.map(sqr) sqrList(List(2, 3, 4)) // List(4, 9, 16)
abstract class Function1[A, B] { def apply(a: A): B } (A => B) ~ Function1[A, B]
val product: (Int, Int) => Int = tuple => tuple._1 * tuple._2 val sqr: Int => Int = x => product (x, x)
def decode: Array[Byte] => String def parseString[T](parser: Parser[T]): String => T def parseBytes[T](parser: Parse[T]): Array[Byte] => T = decode andThen parseString(parser) // ~ bytes => parseString(parser)(decode(bytes))
//curr - операция каррирования foo: (A, B, C) => D curr(foo): A => ((B, C) => D) curr(curr(foo)): A => (B => (C => D))
def log(time: DateTime, level: Level, msg: String): String def curriedLog: DateTime => Level => String => String = time: DateTime => level: Level => msg: String => log(time, level, msg) val fixedTimeLogger: Level => String => String = curriedLog(DateTime.now()) val fixedTimeErrorLogger: String => String = fixedTimeLogger(Level.error)
def log(time: DateTime, level: Level, msg: String): String def curriedLog(time: DateTime) (level: Level) (msg: String): String = log(time, level, msg)
def map[A, B](f: A => B, list: List[A]): List[B] def compose[A, B, C](f: A => B, g: B => C): A => C = (x: A) => g(f(x))
def map[A, B](f: A => B)(list: List[A]): List[B] = if(list.nonEmpty) f(list.head) +: map(f)(list.tail) else Nil
at map.appy
at map.appy
at map.appy
at map.appy
at map.appy
def iterativeMap[A, B](f: A => B)(list: List[A]): List[B] = { import scala.collection.mutable val buffer = muttable.ListBuffer[B] = muttable.ListBuffer.empty for(item <- list) { buffer += f(item) } buffer } def tailMap[A, B](f: A => B)(list: List[A]): List[B] = { @tailrec def loop(l: List[A], buffer: List[B]): List[B] = if(l.isEmpty) { buffer } else { val head = l.head val tail = l.tail loop(tail, buffer :+ f(head)) } loop(list, List.empty) }
at map.loop
at map.appy
Возможность | switch-case | match-case |
---|---|---|
сопоставление с вычисляемым значением (литерал, выражение) | + | + |
сопоставление с типом | - | + |
"Распаковка" объектов | - | + |
Сопоставление с xml | - | + |
Завершения проверок после совпадения | break | + |
и много прочего | - | + |
def foo[A](a: A): Unit = a match { case x => () // x is A }
def foo[A](a: A): Unit = a match { case x: B => () // x is B if a is B case _: C => // there are no new names and we know that a is C }
def foo[A](a: Option[A]): A = a match { case Some(x) => x // x is A case _ => A.default }
def foo[A](a: Option[A]): A = a match { case some@Some(x) => x // x is A, some is Some[A] case _ => A.default }
def foo[A](a: Option[A]): Int = a match { case Some(x: Int) if x > 0 => x // x is positive Int case _ => 1 }
def tailMap[A, B](f: A => B)(list: List[A]): List[B] = { @tailrec def loop(l: List[A], buffer: List[B]): List[B] = if(l.isEmpty) { buffer } else { val head = l.head val tail = l.tail loop(tail, buffer :+ f(head)) } loop(list, List.empty) } def newTailMap[A, B](f: A => B)(list: List[A]): List[B] = { @tailrec def loop(l: List[A], buffer: List[B]): List[B] = l match { case head :: tail => loop(tail, buffer :+ f(head)) // ::(head, tail) case _ => buffer } loop(list, List.empty) }
sealed trait List[+T] final case class ::[+T](head: T, tail: List[T]) extends List[T] case object Nil extends List[Nothing]
sealed trait Boolean case object True extends Boolean case object False extends Boolean
case class Database(url: String, username: String, password: String) case class StandAloneApp(socketConf: (Host, Port)) case class BasicAppEnvironment(appConf: StandAloneApp, db: Database)
def startApp( hostAllowed: Host, listenPort: Port, db: Database): Unit def run(env: BasicAppEnvironment) = env match { case BasicAppEnvironment(StandAloneApp((host, port)), db: Database) => startApp(host, port, db) }
def describeValue(opt: Option[_]): String = opt match { case Some(value) => s"There is a \"${value.toString}\"" case None => "There is nothing" } def describe(opt: Option[_]): String = opt match { case _: Some[_] => "There is something" case _ => "There is nothing" }