typestar

El pipeline en F#

|> alimenta el valor izquierdo hacia adelante; los datos se leen en orden de flujo.

// |> feeds the left value to the right function: data flows forward.
let weightsKg = [28.4; 31.0; 19.7; 35.2; 22.1]

let heavyCount =
    weightsKg
    |> List.filter (fun w -> w > 25.0)
    |> List.length

printfn "Hives ready for winter: %d" heavyCount

// Without the pipe the same code reads inside out.
let total = List.sum (List.map (fun w -> w * 1000.0) weightsKg)
printfn "Total grams the hard way: %.0f" total
weightsKg |> List.max |> printfn "Heaviest: %.1f kg"

Cómo funciona

  1. weightsKg |> List.filter ... |> List.length se lee como oración.
  2. La versión sin pipe anida de adentro hacia afuera — mismo código, lectura dura.
  3. Los pipelines terminan en cualquier función: |> printfn cierra el circuito.

Palabras clave y builtins usados aquí

El intento, en números

Líneas
14
Caracteres a escribir
460
Tokens
94
Ritmo de tres estrellas
65 tpm

Al ritmo de tres estrellas de 65 tokens por minuto, este intento toma unos 87 segundos.

Escribe este fragmento

Paso 2 de 3 en Funciones; paso 5 de 27 en Fundamentos del lenguaje.

← Anterior Siguiente →