Skip to content

Instantly share code, notes, and snippets.

@nattsw
Last active July 4, 2016 03:23
Show Gist options
  • Save nattsw/214d2fbff83a787ebd9f95be7724fc9c to your computer and use it in GitHub Desktop.
Save nattsw/214d2fbff83a787ebd9f95be7724fc9c to your computer and use it in GitHub Desktop.

scala

  • immutable expressions

    val two = 1 + 1

  • mutable expressions

    var temperature = 30 temperature = 40

  • functions

    • explicit definition

       def sum(x: Int, y: Int): Int = x + y
      
    • can omit return type, compiler knows it returns a string:

       def inc(x: Int) = (x + 1).toString()
      
    • can omit parenthesis when calling if no parameter:

       def catString() = “cat”
      

      scala> catString

    • something more complicated, the = is often forgotten

       def complicatedFunc(x: Int, y: Int) = {
       	x + y * x + y + x * y * x + y
       }
      
  • anonymous functions

     (x: Int) => x + 1
    
    • save as val

       val anonIncFunc = (x: Int) => x + 1
      
    • complicated

       (x: Int, y: Int) => {
       	x + y * x + y + x * y * x + y
       }
      

      or

       { (x: Int, y: Int) => 
       	x + y * x + y + x * y * x + y
       }
      
  • function vs method

    Some confusion is introduced:

    • method: def isCat(animal: Animal) = …
    • function: val isCat = …

    According to this answer:

    • a plain method generated less overhead than a function…
    • def evaluates every time it gets called while val evaluates only once…
  • wildcard

     val a = Seq(1,2,3,4,5) // generates List(1, 2, 3, 4, 5)
     a.map { _ + 1 }
     output> List(2, 3, 4, 5, 6)
    
  • partial application

     def sum(x: Int, y: Int): Int = x + y
     val inc = sum(1, _: Int)
    

    scala> inc(5) output> 6

  • curry (yum)

     def multiply(x: Int)(y: Int) = x * y
     val double = multiply(2) _
    

    scala> double(5) output> 10

  • variable length args

     def nope(values: String*) = values.map { a => println(s"$a, no”) }
    
  • classes

     class Cat {
     	val name: String = "stray"
     	def meow(sound: String): String = println(sound);
     }
    

    scala> val cat = new Cat scala> cat.meow("hurrooo"

    • constructors

      Unlike most other languages I know, the constructor ‘method’ doesn’t need to have the same name as the class. It’s

       class Cat(breed: String) {
         /* The constructor */
         val characteristic: String = if (breed == “stray”) {
           "skittish"
         } else {
           "weird"
         }
         …
       }		
      
  • Inheritance $_$

     class Kitten extends Cat {
       val eyeColor = “blue”
     }
    
    • overload methods

       //kitten class
       def meow(): Unit = meow(“ᵐᵉᵒʷ”)
      

      Oddly, if you overload a method that returns the same type, you could get an error if you omit the return type, saying: error: overloaded method meow needs result type. This answer tells us when return types are necessary.

      Method return values in the following cases:

      • When you explicitly call return in a method (even at the end).
      • When a method is recursive.
      • When a method is overloaded and one of the methods calls another. The calling method needs a return type annotation.
      • When the inferred return type would be more general than you intended, e.g., Any.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment