| 1 |
# Lists and List Functions |
| 2 |
|
| 3 |
Sprig provides a simple `list` type that can contain arbitrary sequential lists |
| 4 |
of data. This is similar to arrays or slices, but lists are designed to be used |
| 5 |
as immutable data types. |
| 6 |
|
| 7 |
Create a list of integers: |
| 8 |
|
| 9 |
``` |
| 10 |
$myList := list 1 2 3 4 5 |
| 11 |
``` |
| 12 |
|
| 13 |
The above creates a list of `[1 2 3 4 5]`. |
| 14 |
|
| 15 |
## first, mustFirst |
| 16 |
|
| 17 |
To get the head item on a list, use `first`. |
| 18 |
|
| 19 |
`first $myList` returns `1` |
| 20 |
|
| 21 |
`first` panics if there is a problem while `mustFirst` returns an error to the |
| 22 |
template engine if there is a problem. |
| 23 |
|
| 24 |
## rest, mustRest |
| 25 |
|
| 26 |
To get the tail of the list (everything but the first item), use `rest`. |
| 27 |
|
| 28 |
`rest $myList` returns `[2 3 4 5]` |
| 29 |
|
| 30 |
`rest` panics if there is a problem while `mustRest` returns an error to the |
| 31 |
template engine if there is a problem. |
| 32 |
|
| 33 |
## last, mustLast |
| 34 |
|
| 35 |
To get the last item on a list, use `last`: |
| 36 |
|
| 37 |
`last $myList` returns `5`. This is roughly analogous to reversing a list and |
| 38 |
then calling `first`. |
| 39 |
|
| 40 |
## initial, mustInitial |
| 41 |
|
| 42 |
This compliments `last` by returning all _but_ the last element. |
| 43 |
`initial $myList` returns `[1 2 3 4]`. |
| 44 |
|
| 45 |
`initial` panics if there is a problem while `mustInitial` returns an error to the |
| 46 |
template engine if there is a problem. |
| 47 |
|
| 48 |
## append, mustAppend |
| 49 |
|
| 50 |
Append a new item to an existing list, creating a new list. |
| 51 |
|
| 52 |
``` |
| 53 |
$new = append $myList 6 |
| 54 |
``` |
| 55 |
|
| 56 |
The above would set `$new` to `[1 2 3 4 5 6]`. `$myList` would remain unaltered. |
| 57 |
|
| 58 |
`append` panics if there is a problem while `mustAppend` returns an error to the |
| 59 |
template engine if there is a problem. |
| 60 |
|
| 61 |
## prepend, mustPrepend |
| 62 |
|
| 63 |
Push an element onto the front of a list, creating a new list. |
| 64 |
|
| 65 |
``` |
| 66 |
prepend $myList 0 |
| 67 |
``` |
| 68 |
|
| 69 |
The above would produce `[0 1 2 3 4 5]`. `$myList` would remain unaltered. |
| 70 |
|
| 71 |
`prepend` panics if there is a problem while `mustPrepend` returns an error to the |
| 72 |
template engine if there is a problem. |
| 73 |
|
| 74 |
## concat |
| 75 |
|
| 76 |
Concatenate arbitrary number of lists into one. |
| 77 |
|
| 78 |
``` |
| 79 |
concat $myList ( list 6 7 ) ( list 8 ) |
| 80 |
``` |
| 81 |
|
| 82 |
The above would produce `[1 2 3 4 5 6 7 8]`. `$myList` would remain unaltered. |
| 83 |
|
| 84 |
## reverse, mustReverse |
| 85 |
|
| 86 |
Produce a new list with the reversed elements of the given list. |
| 87 |
|
| 88 |
``` |
| 89 |
reverse $myList |
| 90 |
``` |
| 91 |
|
| 92 |
The above would generate the list `[5 4 3 2 1]`. |
| 93 |
|
| 94 |
`reverse` panics if there is a problem while `mustReverse` returns an error to the |
| 95 |
template engine if there is a problem. |
| 96 |
|
| 97 |
## uniq, mustUniq |
| 98 |
|
| 99 |
Generate a list with all of the duplicates removed. |
| 100 |
|
| 101 |
``` |
| 102 |
list 1 1 1 2 | uniq |
| 103 |
``` |
| 104 |
|
| 105 |
The above would produce `[1 2]` |
| 106 |
|
| 107 |
`uniq` panics if there is a problem while `mustUniq` returns an error to the |
| 108 |
template engine if there is a problem. |
| 109 |
|
| 110 |
## without, mustWithout |
| 111 |
|
| 112 |
The `without` function filters items out of a list. |
| 113 |
|
| 114 |
``` |
| 115 |
without $myList 3 |
| 116 |
``` |
| 117 |
|
| 118 |
The above would produce `[1 2 4 5]` |
| 119 |
|
| 120 |
Without can take more than one filter: |
| 121 |
|
| 122 |
``` |
| 123 |
without $myList 1 3 5 |
| 124 |
``` |
| 125 |
|
| 126 |
That would produce `[2 4]` |
| 127 |
|
| 128 |
`without` panics if there is a problem while `mustWithout` returns an error to the |
| 129 |
template engine if there is a problem. |
| 130 |
|
| 131 |
## has, mustHas |
| 132 |
|
| 133 |
Test to see if a list has a particular element. |
| 134 |
|
| 135 |
``` |
| 136 |
has 4 $myList |
| 137 |
``` |
| 138 |
|
| 139 |
The above would return `true`, while `has "hello" $myList` would return false. |
| 140 |
|
| 141 |
`has` panics if there is a problem while `mustHas` returns an error to the |
| 142 |
template engine if there is a problem. |
| 143 |
|
| 144 |
## compact, mustCompact |
| 145 |
|
| 146 |
Accepts a list and removes entries with empty values. |
| 147 |
|
| 148 |
``` |
| 149 |
$list := list 1 "a" "foo" "" |
| 150 |
$copy := compact $list |
| 151 |
``` |
| 152 |
|
| 153 |
`compact` will return a new list with the empty (i.e., "") item removed. |
| 154 |
|
| 155 |
`compact` panics if there is a problem and `mustCompact` returns an error to the |
| 156 |
template engine if there is a problem. |
| 157 |
|
| 158 |
## slice, mustSlice |
| 159 |
|
| 160 |
To get partial elements of a list, use `slice list [n] [m]`. It is |
| 161 |
equivalent of `list[n:m]`. |
| 162 |
|
| 163 |
- `slice $myList` returns `[1 2 3 4 5]`. It is same as `myList[:]`. |
| 164 |
- `slice $myList 3` returns `[4 5]`. It is same as `myList[3:]`. |
| 165 |
- `slice $myList 1 3` returns `[2 3]`. It is same as `myList[1:3]`. |
| 166 |
- `slice $myList 0 3` returns `[1 2 3]`. It is same as `myList[:3]`. |
| 167 |
|
| 168 |
`slice` panics if there is a problem while `mustSlice` returns an error to the |
| 169 |
template engine if there is a problem. |
| 170 |
|
| 171 |
## chunk |
| 172 |
|
| 173 |
To split a list into chunks of given size, use `chunk size list`. This is useful for pagination. |
| 174 |
|
| 175 |
``` |
| 176 |
chunk 3 (list 1 2 3 4 5 6 7 8) |
| 177 |
``` |
| 178 |
|
| 179 |
This produces list of lists `[ [ 1 2 3 ] [ 4 5 6 ] [ 7 8 ] ]`. |
| 180 |
|
| 181 |
## A Note on List Internals |
| 182 |
|
| 183 |
A list is implemented in Go as a `[]interface{}`. For Go developers embedding |
| 184 |
Sprig, you may pass `[]interface{}` items into your template context and be |
| 185 |
able to use all of the `list` functions on those items. |