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" 30Funciones
Todas las funciones están curried; los tipos usan la notación ->.
elm
add : Int -> Int -> Int
add a b =
a + bArquitectura 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) numsMaybe
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 } -- updateWas this helpful?