Átomo
28Hf
Funcional e tipos · Scala

funções de ordem superior

Abrir na tabela

Definição

Funções que recebem ou devolvem funções. Base da composição de pipelines.

Exemplos

1. Receber função
def retry[T](attempts: Int)(fn: => T): T =
  try fn catch { case e: Exception if attempts > 1 => retry(attempts - 1)(fn) }

val df = retry(3) { spark.read.parquet(path) }
2. Devolver função
def withPrefix(prefix: String): String => String = name => s"$prefix$name"
val stg = withPrefix("stg_")
stg("sales")   // stg_sales
3. Compor
val clean = (trim _).andThen(toLower).andThen(removeAccents)

No mesmo grupo