Documentation ¶
Index ¶
- func ToSlice[T any](itb Iterable[T]) []T
- type Iterable
- func Concat[T any](itb1 Iterable[T], itb2 Iterable[T]) Iterable[T]
- func Distinct[T comparable](itb Iterable[T]) Iterable[T]
- func Filter[T any](itb Iterable[T], filterFunc func(v T) bool) Iterable[T]
- func FlatMap[T any, R any](itb Iterable[T], mapFunc func(v T) []R) Iterable[R]
- func FromSlice[T any](s []T) Iterable[T]
- func FromSliceImmutable[T any](s []T) Iterable[T]
- func Map[T any, R any](itb Iterable[T], mapFunc func(v T) R) Iterable[R]
- func Range[T constraints.Integer](start, end, step T) (Iterable[T], error)
- func Repeat[T any](value T, count int) Iterable[T]
- func RepeatIterable[T any](itb Iterable[T], count int) Iterable[T]
- func Reverse[T any](itb Iterable[T]) Iterable[T]
- func Skip[T any](itb Iterable[T], count int) Iterable[T]
- func SkipLast[T any](itb Iterable[T], count int) Iterable[T]
- func SkipLastWhile[T any](itb Iterable[T], whileFunc func(v T) bool) Iterable[T]
- func SkipWhile[T any](itb Iterable[T], whileFunc func(v T) bool) Iterable[T]
- func SortAsc[T constraints.Ordered](itb Iterable[T]) Iterable[T]
- func SortBy[T any](itb Iterable[T], less func(x, y T) bool) Iterable[T]
- func SortDesc[T constraints.Ordered](itb Iterable[T]) Iterable[T]
- func Take[T any](itb Iterable[T], count int) Iterable[T]
- func TakeLast[T any](itb Iterable[T], count int) Iterable[T]
- func TakeLastWhile[T any](itb Iterable[T], whileFunc func(v T) bool) Iterable[T]
- func TakeWhile[T any](itb Iterable[T], whileFunc func(v T) bool) Iterable[T]
- type Iterator
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Iterable ¶
Iterable provide Iterator creation.
func Concat ¶
Concat makes Iterable elements concatenated of itb1 and itb2.
itb1 := gcf.FromSlice([]int{1, 2, 3}) itb2 := gcf.FromSlice([]int{4, 5, 6}) itbc := gcf.Concat(itb1, itb2)
Example ¶
package main import ( "fmt" "github.com/meian/gcf" ) func main() { itb1 := gcf.FromSlice([]int{1, 2, 3}) itb2 := gcf.FromSlice([]int{5, 6, 7}) itb := gcf.Concat(itb1, itb2) fmt.Println(gcf.ToSlice(itb)) // [ 1 2 3 5 6 7 ] }
Output:
func Distinct ¶
func Distinct[T comparable](itb Iterable[T]) Iterable[T]
Distinct makes Iterable contains unique elements. Inner elements is restrict by comparable constraint.
itb := gcf.FromSlice([]int{1, 2, 3, 3, 4, 2, 5}) itb = gcf.Distinct(itb)
Currently, result order is determined, but on spec, is undefined.
Example ¶
package main import ( "fmt" "github.com/meian/gcf" ) func main() { itb := gcf.FromSlice([]int{1, 4, 2, 3, 2, 3, 1, 2}) itb = gcf.Distinct(itb) for it := itb.Iterator(); it.MoveNext(); { fmt.Println(it.Current()) } }
Output: 1 2 3 4
func Filter ¶
Filter makes Iterable with elements which filterFunc is true.
itb := gcf.FromSlice([]int{1, 2, 3}) itb = gcf.Filter(itb, func(v int) bool { return v%2 > 0 })
If filterFunc is nil, returns original Iteratable.
Example ¶
package main import ( "fmt" "github.com/meian/gcf" ) func main() { s := []string{"dog", "cat", "mouse", "rabbit"} itb := gcf.FromSlice(s) itb = gcf.Filter(itb, func(v string) bool { return len(v) > 3 }) for it := itb.Iterator(); it.MoveNext(); { fmt.Println(it.Current()) } }
Output: mouse rabbit
func FlatMap ¶
FlatMap makes Iterable in elements in slice converted by mapFunc.
itbs := gcf.Func([]string{"a", "ab", "abc"}) itbi := gcf.Map(itbs, func(v string) int[] { var r := make([]int, 0) for _, c := range []rune(v) { r = append(r, int(c)) } })
If mapFunc is nil, return empty Iterable.
Example ¶
package main import ( "fmt" "github.com/meian/gcf" ) func main() { s := []string{"dog", "cat", "mouse", "rabbit"} itb := gcf.FromSlice(s) itbs := gcf.FlatMap(itb, func(v string) []string { r := make([]string, 0, len(v)) for _, c := range v { r = append(r, fmt.Sprintf("%c", c)) } return r }) fmt.Println(gcf.ToSlice(itbs)) }
Output: [d o g c a t m o u s e r a b b i t]
func FromSlice ¶
FromSlice makes Iterable from slice.
s := []int{1, 2, 3} itb := gcf.FromSlice(s)
By change elements in base slice afrer this called, change is affected to Iterator. If you want no affects by change, you can use FromSliceImmutable.
func FromSliceImmutable ¶
FromSliceImmutable makes Iterable from slice with immutable.
s := []int{1, 2, 3} itb := gcf.FromSliceImmutable(s)
Input slice is duplicated to make immutable, so have some performance bottleneck.
func Map ¶
Map makes Iterable in elements convert by mapFunc.
itbs := gcf.Func([]string{"a", "ab", "abc"}) itbi := gcf.Map(itbs, func(v string) int { return len(v) })
If mapFunc is nil, return Iterable in zero value elements.
Example ¶
package main import ( "fmt" "github.com/meian/gcf" ) func main() { s := []string{"dog", "cat", "mouse", "rabbit"} itb := gcf.FromSlice(s) itbs := gcf.Map(itb, func(v string) string { return fmt.Sprintf("%s:%d", v, len(v)) }) for it := itbs.Iterator(); it.MoveNext(); { fmt.Println(it.Current()) } }
Output: dog:3 cat:3 mouse:5 rabbit:6
func Range ¶
func Range[T constraints.Integer](start, end, step T) (Iterable[T], error)
Range makes Iterable with increasing or decreasing elements according to step.
itb := gcf.Range(1, 10, 3)
If step is positive, elements is enumerated from start to end with increment by step. If step is negative, elements is enumerated from start to end with decrement by step. If step is zero, returns error. If start equals to end, makes Iterable with one element. If direction of step is opposite to start and end, returns empty Iterable.
Example ¶
package main import ( "fmt" "github.com/meian/gcf" ) func main() { itb, _ := gcf.Range(2, 10, 2) fmt.Println(gcf.ToSlice(itb)) itb, _ = gcf.Range(10, 1, -2) fmt.Println(gcf.ToSlice(itb)) }
Output: [2 4 6 8 10] [10 8 6 4 2]
func Repeat ¶
Repeat makes Iterable that repeat value a count times.
itb = gcf.Repeat(1, 3)
If count is 0, returns empty Iterable. If count is negative, raises panic.
Example ¶
package main import ( "fmt" "github.com/meian/gcf" ) func main() { itb := gcf.Repeat(1, 3) fmt.Println(gcf.ToSlice(itb)) }
Output: [1 1 1]
func RepeatIterable ¶
RepeatIterable makes Iterable that repeat elements in itb a count times.
s := []int{1, 2, 3} itb := gcf.FromSlice(s) itb = gcf.RepeatIterable(itb, 3)
If count is 0, returns empty Iterable. If count is negative, raises panic.
Example ¶
package main import ( "fmt" "github.com/meian/gcf" ) func main() { s := []int{1, 2, 3} itb := gcf.FromSlice(s) itb = gcf.RepeatIterable(itb, 3) fmt.Println(gcf.ToSlice(itb)) }
Output: [1 2 3 1 2 3 1 2 3]
func Reverse ¶
Reverse makes Iterable with reverse order elements.
itb := gcf.FromSlice([]int{1, 2, 3}) itb = gcf.Reverse(itb)
Example ¶
package main import ( "fmt" "github.com/meian/gcf" ) func main() { itb := gcf.FromSlice([]int{1, 3, 2, 4}) itb = gcf.Reverse(itb) fmt.Println(gcf.ToSlice(itb)) }
Output: [4 2 3 1]
func Skip ¶
Skip makes Iterable with elements excepting counted elements from ahead.
itb := gcf.FromSlice([]{1, 2, 3}) itb = gcf.Skip(itb, 2)
If count is 0, returns original Iterable. If count is negative, raises panic.
Example ¶
package main import ( "fmt" "github.com/meian/gcf" ) func main() { itb := gcf.FromSlice([]int{1, 2, 3, 4, 5}) itb = gcf.Skip(itb, 2) fmt.Println(gcf.ToSlice(itb)) }
Output: [3 4 5]
func SkipLast ¶ added in v0.0.6
SkipLast makes Iterable with elements excepting counted elements from end.
itb := gcf.FromSlice([]{1, 2, 3}) itb = gcf.SkipLast(itb, 2)
If count is 0, returns original Iterable. If count is negative, raises panic.
Example ¶
package main import ( "fmt" "github.com/meian/gcf" ) func main() { itb := gcf.FromSlice([]int{1, 2, 3, 4, 5}) itb = gcf.SkipLast(itb, 3) fmt.Println(gcf.ToSlice(itb)) }
Output: [1 2]
func SkipLastWhile ¶ added in v0.0.6
SkipLastWhile makes Iterable with elements excepting elements that whileFunc is true from end.
itb := gcf.FromSlice([]{1, 2, 3}) itb = gcf.SkipLastWhile(itb, func(v int) bool { return v <= 2 })
If whileFunc is nil, returns original Iterable.
Example ¶
package main import ( "fmt" "github.com/meian/gcf" ) func main() { itb := gcf.FromSlice([]int{1, 3, 5, 7, 2, 4, 6, 8, 9}) itb = gcf.SkipLastWhile(itb, func(v int) bool { return v > 3 }) s := gcf.ToSlice(itb) fmt.Println(s) }
Output: [1 3 5 7 2]
func SkipWhile ¶
SkipWhile makes Iterable with elements excepting elements that whileFunc is true from ahead.
itb := gcf.FromSlice([]{1, 2, 3}) itb = gcf.SkipWhile(itb, func(v int) bool { return v <= 2 })
If whileFunc is nil, returns original Iterable.
Example ¶
package main import ( "fmt" "github.com/meian/gcf" ) func main() { itb := gcf.FromSlice([]int{1, 3, 5, 7, 2, 4, 6, 8, 9}) itb = gcf.SkipWhile(itb, func(v int) bool { return v%2 > 0 }) s := gcf.ToSlice(itb) fmt.Println(s) }
Output: [2 4 6 8 9]
func SortAsc ¶
func SortAsc[T constraints.Ordered](itb Iterable[T]) Iterable[T]
SortAsc makes Iterable with sorted by ascending elements.
itb := gcf.FromSlice([]int{1, 3, 2}) itb = gcf.SortAsc(itb)
Example ¶
package main import ( "fmt" "github.com/meian/gcf" ) func main() { itb := gcf.FromSlice([]int{3, 6, 7, 1, 5, 6, 2, 4, 5}) itb = gcf.SortAsc(itb) fmt.Println(gcf.ToSlice(itb)) }
Output: [1 2 3 4 5 5 6 6 7]
func SortBy ¶
SortBy makes iterable with elements sorted by provided less function.
type data struct { id int } itb := gcf.FromSlice([]data{{1}, {3}, {2}}) itb = gcf.SortBy(itb, func(x, y data) bool { return x.id < y.id })
The less function takes x element and y element, and returns true if x is less than y.
Example ¶
package main import ( "fmt" "github.com/meian/gcf" ) func main() { type data struct{ v int } itbi := gcf.FromSlice([]int{3, 6, 7, 1, 5, 6, 2, 4, 5}) itb := gcf.Map(itbi, func(v int) data { return data{v} }) itb = gcf.SortBy(itb, func(x, y data) bool { return x.v < y.v }) fmt.Println(gcf.ToSlice(itb)) }
Output: [{1} {2} {3} {4} {5} {5} {6} {6} {7}]
func SortDesc ¶
func SortDesc[T constraints.Ordered](itb Iterable[T]) Iterable[T]
SortDesc makes Iterable with sorted by descending elements.
itb := gcf.FromSlice([]int{1, 3, 2}) itb = gcf.SortDesc(itb)
Example ¶
package main import ( "fmt" "github.com/meian/gcf" ) func main() { itb := gcf.FromSlice([]int{3, 6, 7, 1, 5, 6, 2, 4, 5}) itb = gcf.SortDesc(itb) fmt.Println(gcf.ToSlice(itb)) }
Output: [7 6 6 5 5 4 3 2 1]
func Take ¶
Take makes Iterable with count elements from ahead.
itb := gcf.FromSlice([]{1, 2, 3}) itb = gcf.Take(itb, 2)
If count is 0, returns empty Iterable. If count is negative, raises panic.
Example ¶
package main import ( "fmt" "github.com/meian/gcf" ) func main() { itb := gcf.FromSlice([]int{1, 2, 3, 4, 5}) itb = gcf.Take(itb, 3) fmt.Println(gcf.ToSlice(itb)) }
Output: [1 2 3]
func TakeLast ¶ added in v0.0.6
TakeLast makes Iterable with count elements from end.
itb := gcf.FromSlice([]{1, 2, 3}) itb = gcf.TakeLast(itb, 2)
If count is 0, returns empty Iterable. If count is negative, raises panic.
Example ¶
package main import ( "fmt" "github.com/meian/gcf" ) func main() { itb := gcf.FromSlice([]int{1, 2, 3, 4, 5}) itb = gcf.TakeLast(itb, 3) fmt.Println(gcf.ToSlice(itb)) }
Output: [3 4 5]
func TakeLastWhile ¶ added in v0.0.6
TakeLastWhile makes Iterable with elements in which whileFunc is true from end.
itb := gcf.FromSlice([]{1, 2, 3}) itb = gcf.TakeLastWhile(itb, func(v int) bool { return v >= 2 })
If whileFunc is nil, returns empty Iterable.
Example ¶
package main import ( "fmt" "github.com/meian/gcf" ) func main() { itb := gcf.FromSlice([]int{1, 3, 5, 7, 2, 4, 6, 8, 9}) itb = gcf.TakeLastWhile(itb, func(v int) bool { return v > 3 }) s := gcf.ToSlice(itb) fmt.Println(s) }
Output: [4 6 8 9]
func TakeWhile ¶
TakeWhile makes Iterable with elements in which whileFunc is true from ahead.
itb := gcf.FromSlice([]{1, 2, 3}) itb = gcf.TakeWhile(itb, func(v int) bool { return v <= 2 })
If whileFunc is nil, returns empty Iterable.
Example ¶
package main import ( "fmt" "github.com/meian/gcf" ) func main() { itb := gcf.FromSlice([]int{1, 3, 5, 7, 2, 4, 6, 8, 9}) itb = gcf.TakeWhile(itb, func(v int) bool { return v%2 > 0 }) s := gcf.ToSlice(itb) fmt.Println(s) }
Output: [1 3 5 7]
type Iterator ¶
type Iterator[T any] interface { // MoveNext proceed element position to next element. // // var itb Iterator[int] // it := itb.Iterator() // for it.MoveNext() { // v := it.Current() // // some processes for iteration values // } // // Return true if next value is exists or false if no next value. MoveNext() bool // Current return current element value. // // Return current value if iterable position or should get zero value if out of iterable position. // Note that return zero value before MoveNext is called. Current() T }
Iterator provide iterative process for collections.