Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
wiki2:elm [2020/05/09 18:53] root [Destructuring and pattern matching] |
wiki2:elm [2020/05/16 11:53] (actual) |
||
|---|---|---|---|
| Línea 87: | Línea 87: | ||
| </code> | </code> | ||
| - | ==== Destructuring and pattern matching ==== | + | ==== Destructuring ==== |
| - | Destructuring: | + | |
| <code> | <code> | ||
| let | let | ||
| Línea 94: | Línea 94: | ||
| in | in | ||
| lastName -- Result: Doe | lastName -- Result: Doe | ||
| + | |||
| -- ... or ... | -- ... or ... | ||
| second (_, snd) = snd | second (_, snd) = snd | ||
| second (0, 1) -- Result: 1 | second (0, 1) -- Result: 1 | ||
| + | |||
| + | -- ... or.. | ||
| + | userName : User -> String | ||
| + | userName user = | ||
| + | let | ||
| + | (User name) = user | ||
| + | in | ||
| + | String.toLower name | ||
| + | |||
| + | -- ... or... | ||
| + | type alias Vector = { x : Int, y : Int } | ||
| + | length : Vector -> Float | ||
| + | length { x, y } = sqrt <| toFloat <| x^2 + y^2 | ||
| + | length { x = 1, y = 2 } -- Result: 2.2360 | ||
| + | length { x = 1, y = 2, z = 3 } -- Error | ||
| + | -- To do this: | ||
| + | length : { r | x : Int, y : Int } -> Float | ||
| + | length { x, y } = sqrt <| toFloat <| x^2 + y^2 | ||
| + | length { x = 1, y = 2, z = 3 } -- OK | ||
| + | -- or | ||
| + | type alias Vector r = { r | x : Int, y : Int } | ||
| + | length : Vector r -> Float | ||
| + | length { x, y } = sqrt <| toFloat <| x^2 + y^2 | ||
| </code> | </code> | ||
| + | |||
| + | ==== Pattern matching ==== | ||
| + | Pattern matching is a mechanism for choosing the branch of code to execute based on the type or value of a given expression. | ||
| + | |||
| + | ==== Functors, monads and applicatives ==== | ||
| + | |||
| + | * Functors are things you can map on | ||
| + | * Monads things you can andThen on | ||
| + | * Applicatives things you can andMap on | ||
| ===== Basic ===== | ===== Basic ===== | ||
| Línea 127: | Línea 160: | ||
| </code> | </code> | ||
| + | === Constructing records from another === | ||
| + | <code> | ||
| + | > type alias A = { a: String, b: Int } | ||
| + | > type alias B = { a: String, b: Int } | ||
| + | > a : A | ||
| + | | a = A "prueba" 666 | ||
| + | { a = "prueba", b = 666 } : A | ||
| + | > b : B | ||
| + | | b = a | ||
| + | { a = "prueba", b = 666 } : B | ||
| + | </code> | ||
| ==== Type annotation ==== | ==== Type annotation ==== | ||
| Línea 202: | Línea 245: | ||
| Visitor "kate95" | Visitor "kate95" | ||
| -- Visitor "kate95" : User | -- Visitor "kate95" : User | ||
| + | </code> | ||
| + | |||
| + | === Types variable === | ||
| + | |||
| + | When you do not care about the type that is passed. In the next example the ''List'' passed to ''List.length'' can be a ''List String'', ''List Int''... | ||
| + | <code> | ||
| + | > List.length | ||
| + | <function> : List a -> Int | ||
| + | </code> | ||
| + | |||
| + | However it can restrict the output. With ''List.reverse'' we know that we are going to obtain a ''List'' with the same type as it was passed on the first instance. | ||
| + | <code> | ||
| + | > List.reverse | ||
| + | <function> : List a -> List a | ||
| + | > List.reverse [ "a", "b", "c" ] | ||
| + | ["c","b","a"] : List String | ||
| + | > List.reverse [ True, False ] | ||
| + | [False,True] : List Bool | ||
| </code> | </code> | ||