Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add seq function #205

Merged
merged 2 commits into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/integer_slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,19 @@ untilStep 3 6 2

The above will produce `[3 5]` by starting with 3, and adding 2 until it is equal
or greater than 6. This is similar to Python's `range` function.

## seq

Works like the bash `seq` command.
* 1 parameter (end) - will generate all counting integers between 1 and `end` inclusive.
* 2 parameters (start, end) - will generate all counting integers between `start` and `end` inclusive incrementing or decrementing by 1.
* 3 parameters (start, step, end) - will generate all counting integers between `start` and `end` inclusive incrementing or decrementing by `step`.

```
seq 5 => 0 1 2 3 4 5
seq -3 => 1 0 -1 -2 -3
seq 0 2 => 0 1 2
seq 2 -2 => 2 1 0 -1 -2
seq 0 2 10 => 0 2 4 6 8 10
seq 0 -2 -5 => 0 -2 -4
```
1 change: 1 addition & 0 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ var genericMap = map[string]interface{}{
"int64": toInt64,
"int": toInt,
"float64": toFloat64,
"seq": seq,
"toDecimal": toDecimal,

//"gt": func(a, b int) bool {return a > b},
Expand Down
41 changes: 41 additions & 0 deletions numeric.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"math"
"strconv"
"strings"

"github.com/spf13/cast"
)
Expand Down Expand Up @@ -112,3 +113,43 @@ func toDecimal(v interface{}) int64 {
}
return result
}

func seq(params ...int) string {
increment := 1
switch len(params) {
case 0:
return ""
case 1:
start := 1
end := params[0]
if end < start {
increment = -1
}
return intArrayToString(untilStep(start, end+increment, increment), " ")
case 3:
start := params[0]
end := params[2]
step := params[1]
if end < start {
increment = -1
if step > 0 {
return ""
}
}
return intArrayToString(untilStep(start, end+increment, step), " ")
case 2:
start := params[0]
end := params[1]
step := 1
if end < start {
step = -1
}
return intArrayToString(untilStep(start, end+step, step), " ")
default:
return ""
}
}

func intArrayToString(slice []int, delimeter string) string {
return strings.Trim(strings.Join(strings.Fields(fmt.Sprint(slice)), delimeter), "[]")
}
21 changes: 21 additions & 0 deletions numeric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,24 @@ func TestRound(t *testing.T) {
assert.Equal(t, 123.23, round(123.2329999, 2, .3))
assert.Equal(t, 123.24, round(123.233, 2, .3))
}

func TestSeq(t *testing.T) {
tests := map[string]string{
`{{seq 0 1 3}}`: "0 1 2 3",
`{{seq 0 3 10}}`: "0 3 6 9",
`{{seq 3 3 2}}`: "",
`{{seq 3 -3 2}}`: "3",
`{{seq}}`: "",
`{{seq 0 4}}`: "0 1 2 3 4",
`{{seq 5}}`: "1 2 3 4 5",
`{{seq -5}}`: "1 0 -1 -2 -3 -4 -5",
`{{seq 0}}`: "1 0",
`{{seq 0 1 2 3}}`: "",
`{{seq 0 -4}}`: "0 -1 -2 -3 -4",
}
for tpl, expect := range tests {
if err := runt(tpl, expect); err != nil {
t.Error(err)
}
}
}