Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.05 KB | None | 0 0
  1. import scala.collection.mutable
  2. import scala.util.Random
  3. object Tasks {
  4.   object EverythingIsExpression {
  5.     val x = 123
  6.     val y = if (x > 10) 1 else "string" // тип
  7.   }
  8.   EverythingIsExpression.y
  9.  
  10.   object FunctionAsValue {
  11.     def sum(a: Int, b: Int, f: Int => Int): Int =
  12.       if (a > b) 0 else f(a) + sum(a + 1, b, f)
  13.     def sqr(n: Int) = n * n
  14.     val sum1: Int = sum(-1, 2, sqr)
  15.     val sum2: Int = sum(-1, 2, x => x)
  16.     def mul(m: Int, n: Int) = m * n
  17.     val sum3: Int = sum(-1, 2, mul(_, 12))
  18.   }
  19.   FunctionAsValue.sum1
  20.   FunctionAsValue.sum2
  21.   FunctionAsValue.sum3
  22.  
  23.   object ImmutabilityAndSideEffects {
  24.     def getRandomNumberTimesX(x: Int) = x * Random.nextDouble()
  25.     def getSqrtOfX(x: Int) = Math.sqrt(x)
  26.  
  27.     class MutableComplexNumber(var re: Double, var im: Double)
  28.     /*case */class ComplexNumber(val re: Double, val im: Double)
  29.     val mutVal = new MutableComplexNumber(12, 13)
  30.     mutVal.re = 123
  31.     val immutVal = new ComplexNumber(12, 13)
  32.     val mutList = new mutable.MutableList[ComplexNumber]
  33.     mutList += immutVal
  34.     val immutList = List(immutVal, immutVal)
  35.     // применение в жизни. предсказуемость. кэширование
  36.   }
  37.   ImmutabilityAndSideEffects.getRandomNumberTimesX(12)
  38.   ImmutabilityAndSideEffects.getSqrtOfX(100)
  39.   ImmutabilityAndSideEffects.mutList
  40.   ImmutabilityAndSideEffects.immutList
  41.   object CaseClasses {
  42.     abstract class Try[T]
  43.     case class Success[T](result: T) extends Try[T]
  44.     case class Failure[T](e: Throwable) extends Try[T]
  45.     object Try {
  46.       def apply[T](operation: () => T): Try[T] = {
  47.         try {
  48.           Success(operation())
  49.         } catch {
  50.           case e: Throwable => Failure(e)
  51.         }
  52.       }
  53.     }
  54.   }
  55.   CaseClasses.Try(() => 1/0)
  56.  
  57.   object ByNameParameter {
  58.     abstract class Try[T]
  59.     case class Success[T](result: T) extends Try[T]
  60.     case class Failure[T](e: Throwable) extends Try[T]
  61.     object Try {
  62.       def apply[T](operation: => T): Try[T] = {
  63.         try {
  64.           Success(operation)
  65.         } catch {
  66.           case e: Throwable => Failure(e)
  67.         }
  68.       }
  69.     }
  70.     val try2 = Try(1/0)
  71.     def sqr(n: => Int) = n * n
  72.     def x = {
  73.       println("getting x")
  74.       12
  75.     }
  76.     val squared = sqr(x)
  77.   }
  78.   ByNameParameter.try2
  79.   ByNameParameter.squared
  80.  
  81.   object CollectionFunctions {
  82.     case class Person(name: String, age: Int)
  83.     val list = Seq(new Person("Matt", 12), new Person("Patt", 11))
  84.   }
  85.  
  86.   CollectionFunctions.list.filter(p => p.name.charAt(0) == 'M')
  87.   CollectionFunctions.list.foldLeft(0)((z,p) => z + p.age)
  88.  
  89.   object ForYield {
  90.     import scala.util.Try
  91.     val oddsSquares = for (x <- 1 to 15 if x % 2 == 1) yield x * x
  92.     val anotherOddsSquares = (1 to 15)
  93.       .withFilter { case (x) => x % 2 == 1}
  94.       .map { case x => x * x }
  95.     val tryResult = for (
  96.         x <- Try(12);
  97.         y <- Try(4 / x);
  98.         z <- Try(throw new Exception)
  99.     ) yield y / 0
  100.   }
  101.   ForYield.oddsSquares
  102.   ForYield.anotherOddsSquares
  103.   ForYield.tryResult
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement