Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func At ¶
At returns the element at the given index in the span. Returns false if the index is out of bounds.
Example ¶
package main
import (
"fmt"
"github.com/byExist/spans"
)
func main() {
s := spans.Stride(10, 20, 2)
v, ok := spans.At(s, 2)
fmt.Println(v)
fmt.Println(ok)
_, ok = spans.At(s, 10)
fmt.Println(ok)
}
Output: 14 true false
func Contains ¶
Contains returns true if the given element is contained within the span.
Example ¶
package main
import (
"fmt"
"github.com/byExist/spans"
)
func main() {
s := spans.Stride(0, 6, 2)
fmt.Println(spans.Contains(s, 4))
fmt.Println(spans.Contains(s, 5))
}
Output: true false
func Find ¶
Find returns the index of the element in the span. Returns false if the element is not in the span.
Example ¶
package main
import (
"fmt"
"github.com/byExist/spans"
)
func main() {
s := spans.Stride(0, 10, 2)
i, ok := spans.Find(s, 6)
fmt.Println(i)
fmt.Println(ok)
_, ok = spans.Find(s, 5)
fmt.Println(ok)
}
Output: 3 true false
Types ¶
type Span ¶
type Span struct {
// contains filtered or unexported fields
}
Span represents a range of integers with a start, stop, and step.
func Clone ¶ added in v0.1.0
Clone creates a copy of the given Span.
Example ¶
package main
import (
"fmt"
"github.com/byExist/spans"
)
func main() {
s := spans.Stride(1, 5, 1)
cloned := spans.Clone(s)
for v := range spans.Values(cloned) {
fmt.Print(v, " ")
}
}
Output: 1 2 3 4
func Range ¶
Range returns a Span starting at the given start and ending before stop, with a step of 1.
Example ¶
package main
import (
"fmt"
"github.com/byExist/spans"
)
func main() {
s := spans.Range(1, 4)
for v := range spans.Values(s) {
fmt.Print(v, " ")
}
}
Output: 1 2 3
func Stride ¶
Stride returns a Span with the specified start, stop, and step values. Panics if the step is 0.
Example ¶
package main
import (
"fmt"
"github.com/byExist/spans"
)
func main() {
s := spans.Stride(0, 6, 2)
for v := range spans.Values(s) {
fmt.Print(v, " ")
}
}
Output: 0 2 4
func To ¶
To returns a Span starting at 0 and ending before the given stop value, with a step of 1.
Example ¶
package main
import (
"fmt"
"github.com/byExist/spans"
)
func main() {
s := spans.To(3)
for v := range spans.Values(s) {
fmt.Print(v, " ")
}
}
Output: 0 1 2
func (Span) MarshalJSON ¶ added in v0.3.0
MarshalJSON implements json.Marshaler.
Example ¶
package main
import (
"encoding/json"
"fmt"
"github.com/byExist/spans"
)
func main() {
s := spans.Stride(0, 6, 2)
data, _ := json.Marshal(s)
fmt.Println(string(data))
}
Output: [0,6,2]
func (Span) Start ¶
Start returns the starting value of the span.
Example ¶
package main
import (
"fmt"
"github.com/byExist/spans"
)
func main() {
fmt.Println(spans.Stride(3, 10, 2).Start())
}
Output: 3
func (Span) Step ¶
Step returns the step size of the span.
Example ¶
package main
import (
"fmt"
"github.com/byExist/spans"
)
func main() {
fmt.Println(spans.Stride(3, 10, 2).Step())
}
Output: 2
func (Span) Stop ¶
Stop returns the stopping value of the span.
Example ¶
package main
import (
"fmt"
"github.com/byExist/spans"
)
func main() {
fmt.Println(spans.Stride(3, 10, 2).Stop())
}
Output: 10
func (Span) String ¶ added in v0.3.0
String returns a string representation of the Span.
Example ¶
package main
import (
"fmt"
"github.com/byExist/spans"
)
func main() {
s := spans.Stride(2, 8, 2)
fmt.Println(s.String())
}
Output: Span(2, 8, 2)
func (*Span) UnmarshalJSON ¶ added in v0.3.0
UnmarshalJSON implements json.Unmarshaler.
Example ¶
package main
import (
"encoding/json"
"fmt"
"github.com/byExist/spans"
)
func main() {
var s spans.Span
_ = json.Unmarshal([]byte(`[1,5,1]`), &s)
fmt.Println(s.String())
}
Output: Span(1, 5, 1)