Skip to content

Elm Hoja de referencia

Lenguaje funcional para aplicaciones web fiables — sin excepciones en tiempo de ejecución.

01

Primeros pasos

Hello World

Las aplicaciones Elm comienzan con una función main.

elm
module Main exposing (main)

main =
  text "Hello, World!"

Tipos

Los alias de tipo definen la forma de los records.

elm
type alias Person =
  { name : String
  , age : Int
  }

p = Person "Alice" 30

Funciones

Todas las funciones están curried; los tipos usan la notación ->.

elm
add : Int -> Int -> Int
add a b =
  a + b

Arquitectura Elm

The Elm Architecture: patrón Model-View-Update.

elm
-- Model
type alias Model = { count : Int }

-- Update
type Msg = Increment | Decrement

update msg model =
  case msg of
    Increment -> { model | count = model.count + 1 }
    Decrement -> { model | count = model.count - 1 }
02

Estructuras de datos

Lists

El módulo List incluye map, filter, foldl, etc.

elm
nums = [1, 2, 3]
List.map (\n -> n * 2) nums
List.filter (\n -> n > 1) nums

Maybe

Maybe maneja los valores nulos de forma segura — Just o Nothing.

elm
type Maybe a = Just a | Nothing

safeDiv a b =
  if b == 0 then Nothing else Just (a // b)

Result

Result sirve para el manejo de errores — Ok o Err.

elm
type Result e a = Ok a | Err e

parse s =
  if String.length s > 0 then Ok s else Err "empty"

Records

Acceso a record con . y actualización con |.

elm
p = { name = "Alice", age = 30 }
p.name
{ p | age = 31 }  -- update

Was this helpful?