feat: Add guide article for elm lists (#19059)
This commit is contained in:
committed by
Quincy Larson
parent
014e23eb3d
commit
3587cc0c4d
@ -3,11 +3,37 @@ title: Arrays
|
||||
---
|
||||
## Arrays
|
||||
|
||||
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/elm/arrays/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.
|
||||
Elm does not offer arrays in the strict sense of the word, having lists instead. This is an important distinction, as lists and arrays offer different guarantees regarding the time complexity of their operations. The full API for Elm lists can be found in the [official documentation](https://package.elm-lang.org/packages/elm/core/latest/List).
|
||||
|
||||
<a href='https://github.com/freecodecamp/guides/blob/master/README.md' target='_blank' rel='nofollow'>This quick style guide will help ensure your pull request gets accepted</a>.
|
||||
### Syntax
|
||||
|
||||
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
|
||||
List literals can be declared in a similar fashion to many other languages:
|
||||
|
||||
#### More Information:
|
||||
<!-- Please add any articles you think might be helpful to read before writing the article -->
|
||||
```elm
|
||||
[1, 2, 3]
|
||||
```
|
||||
|
||||
The language also offers other utility functions to build lists, like [`singleton`](https://package.elm-lang.org/packages/elm/core/latest/List#singleton), [`repeat`](https://package.elm-lang.org/packages/elm/core/latest/List#repeat), [`range`](https://package.elm-lang.org/packages/elm/core/latest/List#range) and the [cons operator](https://package.elm-lang.org/packages/elm/core/latest/List#::):
|
||||
|
||||
```elm
|
||||
singleton 1 == [1]
|
||||
repeat 3 0 == [0, 0, 0]
|
||||
range 0 2 == [0, 1, 2]
|
||||
1 :: 2 :: 3 :: [] == [1, 2, 3]
|
||||
```
|
||||
|
||||
### Operations
|
||||
|
||||
Like expected from a functional language, Elm offers functions like `map`, `filter` and `reduce` (called `foldl` and `foldr` in Elm) for transforming lists. The only difference from JavaScript here is the reduce functions:
|
||||
|
||||
- `foldl` applies the reducing function from left to right (i.e. starting from the first element of the list)
|
||||
- `foldr` applies the reducing function from right to left (i.e. starting from the last element of the list)
|
||||
|
||||
Here is an example below (You can try this in the elm repl to understand it better in practice):
|
||||
|
||||
```elm
|
||||
List.foldl (::) [] [1, 2, 3] == [3, 2, 1] -- [] |> (::) 1 |> (::) 2 |> (::) 3
|
||||
List.foldr (::) [] [1, 2, 3] == [1, 2, 3] -- [] |> (::) 3 |> (::) 2 |> (::) 1
|
||||
```
|
||||
|
||||
The core List library also offers several other functions for sorting, combining and deconstructing lists. A more detailed description of those can be found in the [official documentation](https://package.elm-lang.org/packages/elm/core/latest/List).
|
||||
|
Reference in New Issue
Block a user