typestar

Currificación en F#

Cada función toma un argumento; la aplicación parcial es gratis.

// Every function takes one argument; more arguments mean curry.
let feed syrupLiters hive =
    sprintf "%s gets %.1f L of syrup" hive syrupLiters

// Partial application bakes in the first argument.
let autumnFeed = feed 2.5
printfn "%s" (autumnFeed "Willow")
printfn "%s" (autumnFeed "Bramble")

// Type annotations are allowed but inference usually wins.
let addSuper (frames: int) hive =
    sprintf "%s grows to %d frames" hive (frames + 10)
printfn "%s" (addSuper 8 "Clover")

Cómo funciona

  1. feed syrupLiters hive son en verdad dos funciones anidadas.
  2. feed 2.5 hornea el primer argumento y devuelve el resto.
  3. Anotaciones como (frames: int) guían la inferencia si hace falta.

Palabras clave y builtins usados aquí

El intento, en números

Líneas
13
Caracteres a escribir
474
Tokens
71
Ritmo de tres estrellas
70 tpm

Al ritmo de tres estrellas de 70 tokens por minuto, este intento toma unos 61 segundos.

Escribe este fragmento

Paso 1 de 3 en Funciones; paso 4 de 27 en Fundamentos del lenguaje.

← Anterior Siguiente →