adrianfc
5/1/2020 - 8:53 AM

Lambdas

{ println("Hello")}()

// returns swim
val swim = { println("swim \n")}
swim()

// lambda with arguments (returns 10)
var dirty = 20
val waterFilter = { dirty: Int -> dirty /2 }
waterFilter(dirty)

// 
val waterFilter: (Int) -> Int { dirty -> dirty /2}

// Higher order function (function as a parameter)
fun feedFish(dirty: Int) = dirty + 10

fun updateDirty(dirty: Int, operation: (Int) -> Int): Int {
  return operation(dirty)
}

fun dirtyProcessor() {
  dirty = updateDirty(dirty, waterFilter)

  // feedfish is a named function, not a lambda
  dirty = updateDirty(dirty, ::feedFish )
}