mirror of
https://github.com/gohugoio/hugoDocs.git
synced 2025-09-10 11:54:52 -04:00
Clarify seq function
This commit is contained in:
parent
59f8a1f48e
commit
ad65d2931d
@ -1,6 +1,6 @@
|
|||||||
---
|
---
|
||||||
title: seq
|
title: seq
|
||||||
description: Creates a sequence of integers.
|
description: Returns a slice of integers.
|
||||||
categories: [functions]
|
categories: [functions]
|
||||||
menu:
|
menu:
|
||||||
docs:
|
docs:
|
||||||
@ -10,32 +10,19 @@ signature: ["seq LAST", "seq FIRST LAST", "seq FIRST INCREMENT LAST"]
|
|||||||
relatedfuncs: []
|
relatedfuncs: []
|
||||||
---
|
---
|
||||||
|
|
||||||
It's named and used in the model of [GNU's seq].
|
```go-html-template
|
||||||
|
{{ seq 2 }} → [1 2]
|
||||||
```
|
{{ seq 0 2 }} → [0 1 2]
|
||||||
3 → 1, 2, 3
|
{{ seq -2 2 }} → [-2 -1 0 1 2]
|
||||||
1 2 4 → 1, 3
|
{{ seq -2 2 2 }} → [-2 0 2]
|
||||||
-3 → -1, -2, -3
|
|
||||||
1 4 → 1, 2, 3, 4
|
|
||||||
1 -2 → 1, 0, -1, -2
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Example: `seq` with `range` and `after`
|
Iterate over a sequence of integers:
|
||||||
|
|
||||||
You can use `seq` in combination with `range` and `after`. The following will return 19 elements:
|
|
||||||
|
|
||||||
```go-html-template
|
```go-html-template
|
||||||
{{ range after 1 (seq 20) }}
|
{{ $product := 1 }}
|
||||||
|
{{ range seq 4 }}
|
||||||
|
{{ $product = mul $product . }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
{{ $product }} → 24
|
||||||
```
|
```
|
||||||
|
|
||||||
However, when ranging with an index, the following may be less confusing in that `$indexStartingAt1` and `$num` will return `1,2,3 ... 20`:
|
|
||||||
|
|
||||||
```go-html-template
|
|
||||||
{{ range $index, $num := seq 20 }}
|
|
||||||
{{ $indexStartingAt1 := add $index 1 }}
|
|
||||||
{{ end }}
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
[GNU's seq]: https://www.gnu.org/software/coreutils/manual/html_node/seq-invocation.html#seq-invocation
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user