Skip to content

Commit

Permalink
Add seq function (#205)
Browse files Browse the repository at this point in the history
* Add seq function for generating a sequence of numbers

* Use untilStep to build the seq command response
  • Loading branch information
thadc23 authored and technosophos committed Jan 24, 2020
1 parent b1852fc commit 0aefeb2
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
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 @@ -163,6 +163,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)
}
}
}

0 comments on commit 0aefeb2

Please sign in to comment.