hi

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 7, 2024 License: MIT Imports: 5 Imported by: 0

README

hi - utility functions for collections

Go Reference

hi is a Lodash-style Go library based on Go 1.18+ Generics inspired by lo.

Usage

Install the package:

go get github.com/shogo82148/hi

and import it:

import (
    "github.com/shogo82148/hi"
)

Experimental support of range over func

Experimental support of range over func is available from Go 1.22.

GOEXPERIMENT=rangefunc go1.22.0 run main.go

utility functions for iterators are available with it package.

import (
    "github.com/shogo82148/hi/it"
)

Reference

SliceValues, MapKeys, MapValues, ChanValues

Experimental: SliceValues, MapKeys, MapValues and ChanValues creates iterators from slices, maps and channels.

for v := range it.SliceValues([]string{"a", "b", "c"}) {
    fmt.Println(v)
}

// Output:
// a
// b
// c
SliceIter, MapIter

Experimental: SliceIter and MapIter create iterators that return key-value pairs.

for i, v := range it.SliceIter([]string{"a", "b", "c"}) {
    fmt.Printf("%d: %s\n", i, v)
}

// Output:
// 0: a
// 1: b
// 2: c
Range

Experimental: Range creates an iterator that returns a sequence of integers.

for v := range it.Range(5) {
    fmt.Println(v)
}

// Output:
// 0
// 1
// 2
// 3
// 4
Enumerate

Experimental: Enumerate returns an iterator that returns (index, value) tuples from seq.

seq := it.Enumerate(it.SliceValues([]string{"a", "b", "c"}))
// it.SliceIter([]string{"a", "b", "c"})
Cycle, Cycle2

Experimental: Cycle makes an iterator returning elements from the iterable and saving a copy of each.

seq := it.Cycle(it.Range(3))
// it.SliceValues([]int{0, 1, 2, 0, 1, 2, 0, ...})

seq := it.Cycle2(it.SliceIter([]string{"zero", "one", "two"}))
// it.Zip(
//     it.SliceValues([]int{0, 1, 2, 0, 1, 2, 0, ...}),
//     it.SliceValues([]string{"zero", "one", "two", "zero", "one", "two", "zero", ...}),
// )
Tee, Tee2

Experimental: Tee makes n iterators from seq.

s := it.Tee(it.Range(3), 2)
// []iter.Seq[int]{it.Range(3), it.Range(3)}
s := it.Tee2(it.SliceIter([]string{"zero", "one", "two"}), 2)
// []iter.Seq2[int, string]{
//     it.SliceIter([]string{"zero", "one", "two"}),
//     it.SliceIter([]string{"zero", "one", "two"}),
// }
Zip

Experimental: it.Zip converts a pair of iter.Seq to iter.Seq2.

it.Zip(it.Range(3), it.SliceValues([]string{"zero", "one", "two"}))
// it.SliceIter([]string{"zero", "one", "two"})
ZipLongest

Experimental:

it.Zip(it.Range(5), it.SliceValues([]string{"zero", "one", "two"}))
// it.SliceIter([]string{"zero", "one", "two", "", ""})
Zip2,... ,Zip16
hi.Zip2([]int{1, 2, 3}, []string{"one", "two", "three"})
// []tuple.Tuple2{
//     tuple.New2(1, "one"),
//     tuple.New2(2, "two"),
//     tuple.New2(3, "three"),
// }

Experimental: zip on iterators

it.Zip2(it.Range(3), it.SliceValues([]string{"zero", "one", "two"}))
// it.SliceValues([]tuple.Tuple2{
//     tuple.New2(0, "zero"),
//     tuple.New2(1, "one"),
//     tuple.New2(2, "two"),
// })
Unzip

Experimental: it.Unzip converts a iter.Seq2 to a pair of iter.Seq.

it.Unzip(it.SliceIter([]string{"zero", "one", "two"}))
// it.Range(3), it.SliceValues([]string{"zero", "one", "two"})
Unzip2,... , Unzip16
hi.Unzip2([]tuple.Tuple2{tuple.New2(1, "one"), tuple.New2(2, "two"), tuple.New2(3, "three")})
// []int{1, 2, 3}, []string{"one", "two", "three"}

Experimental: Unzip2,..., Unzip16 on iterators

seq := it.Unzip(it.SliceValues([]tuple.Tuple2{
    tuple.New2(0, "zero"),
    tuple.New2(1, "one"),
    tuple.New2(2, "two"),
}))
// it.Range(3), it.SliceValues([]string{"zero", "one", "two"})
Map
hi.Map([]int{1, 2, 3, 4, 5}, func(_, v int) string {
    return fmt.Sprintf("(%d)", v)
})
// []string{"(1)", "(2)", "(3)", "(4)", "(5)"}

Experimental: Map on iterators

it.Map(it.Range(5), func(v int) string {
    return fmt.Sprintf("(%d)", v)
})
// it.SliceValues([]string{"(0)", "(1)", "(2)", "(3)", "(4)"})
Filter
even := hi.Filter([]int{1, 2, 3, 4, 5}, func(_, v int) bool {
    return v%2 == 0
})
// []int{2, 4}

Experimental: Filter on iterators

even := it.Filter(it.Range(5), func(v int) bool {
    return v%2 == 0
})
// it.SliceValues([]int{0, 2, 4})
Chunk
hi.Chunk([]int{1, 2, 3, 4, 5}, 2)
// [][]int{{1, 2}, {3, 4}, {5}}

Experimental: Chunk on iterators

it.Chunk(it.Range(5), 2)
// it.SliceValues([][]int{{0, 1}, {2, 3}, {4}})
Slice

Python-like slice.

hi.Slice([]int{0, 1, 2, 3, 4}, 1, 3, 1)
// []int{1, 2}

Experimental: Slice on iterators

it.Slice(it.Range(5), 1, 3, 1)
// it.SliceValues([]int{1, 2})

it.Slice2(it.SliceIter([]string{"a", "b", "c", "d", "e", "f", "g"}), 2, 4, 1)
// it.Zip(it.SliceValues([]int{2, 3}), it.SliceValues([]string{"c", "d"}))
Chain

Chain returns a slice of all elements from all slices.

hi.Chain([]int{1, 2, 3}, []int{4, 5, 6})
// []int{1, 2, 3, 4, 5, 6}

Experimental: Chain on iterators

it.Chain(it.Range(3), it.Range(3))
// it.SliceValues([]int{0, 1, 2, 0, 1, 2})

it.Chain2(
    it.SliceIter([]string{"zero", "one", "two"}),
    it.SliceIter([]string{"null", "eins", "zwei"}),
)
// it.Zip([]int{0, 1, 2, 0, 1, 2}, []string{"zero", "one", "two", "null", "eins", "zwei"})
Compress

Compress makes a slice of the elements in a for which the corresponding element in selectors is true.

hi.Compress([]int{1, 2, 3, 4, 5}, []bool{true, false, true, false, true})
// []int{1, 3, 5}

Experimental: Compress on iterators

it.Compress(it.Range(5), it.SliceValues([]bool{true, false, true, false, true}))
// it.SliceValues([]int{0, 2, 4})

it.Compress2(it.SliceIter([]string{"zero", "one", "two"}), it.SliceValues([]bool{true, false, true}))
// it.Zip(it.SliceValues([]int{0, 2}), it.SliceValues([]string{"zero", "two"}))
DropWhile

DropWhile returns a slice of the remaining elements after dropping the longest prefix of elements that satisfy predicate.

l := hi.DropWhile([]int{1, 4, 6, 4, 1}, func(_, val int) bool {
    return v < 5
})
// []int{6, 4, 1}

Experimental: DropWhile on iterators

it.DropWhile(it.SliceValues([]int{1, 4, 6, 4, 1}), func(v int) bool { return v < 5 })
// it.SliceValues([]int{6, 4, 1})

it.DropWhile2(it.SliceIter([]int{1, 4, 6, 4, 1}), func(_, v int) bool { return v < 5 })
// it.Zip(it.SliceValues([]int{2, 3, 4}), it.SliceValues([]int{6, 4, 1}))
Pairwise

Pairwise makes a slice of all adjacent pairs of elements.

l := hi.Pairwise([]int{1, 2, 3, 4, 5})
// []tuple.Tuple2{tuple.New2(1, 2), tuple.New2(2, 3), tuple.New2(3, 4), tuple.New2(4, 5)}

Experimental: Pairwise on iterators

seq := it.Pairwise(it.Range(5))
// it.SliceValues([]tuple.Tuple2{tuple.New2(1, 2), tuple.New2(2, 3), tuple.New2(3, 4), tuple.New2(4, 5)})

seq := it.Pairwise2(it.SliceIter([]string{"zero", "one", "two"}))
// it.Zip(
//     it.SliceValues([]tuple.Tuple2{tuple.New2(0, 1), tuple.New2(1, 2)})
//     it.SliceValues([]tuple.Tuple2{tuple.New2("zero", "one"), tuple.New2("onw", "two")})
// )
TakeWhile

TakeWhile returns a slice of the longest prefix of elements that satisfy predicate f.

l := hi.TakeWhile([]int{1, 4, 6, 4, 1}, func(_, val int) bool {
    return v < 5
})
// []int{1, 4}

Experimental: TakeWhile on iterators

it.TakeWhile(it.SliceValues([]int{1, 4, 6, 4, 1}), func(v int) bool { return v < 5 })
// it.SliceValues([]int{1, 4})

it.TakeWhile2(it.SliceIter([]int{1, 4, 6, 4, 1}), func(_, v int) bool { return v < 5 })
// it.Zip(it.SliceValues([]int{0, 1}), it.SliceValues([]int{1, 4}))
Repeat

Experimental: Range on iterators

for v := range it.Repeat("hello") {
    fmt.Println(v)
}
// hello
// hello
// hello
// ... (infinite)
RepeatN
hi.RepeatN("hello", 3)
// []string{"hello", "hello", "hello"}

Experimental: RepeatN on iterators

it.RepeatN("hello", 3)
// it.SliceValues([]string{"hello", "hello", "hello"})
Count
hi.Count([]int{1, 2, 3, 4, 5}, 3)
// 1

Experimental: Count on iterators

it.Count(it.Range(5), 3)
// 1
CountBy
numberOfEven := hi.CountBy([]int{1, 2, 3, 4, 5}, func(_, v int) bool { return v % 2 == 0 })
// 2

Experimental: CountBy on iterators

numberOfEven := it.CountBy(it.Range(5), func(_, v int) bool { return v % 2 == 0 })
// 3
Any

Any returns whether l has value at least one.

hi.Any([]int{1, 2, 3, 4, 5}, 5)
// true

Experimental: Any on iterators

it.Any(it.Range(5), 4)
// true

it.Any2(it.SliceIter([]int{1, 2, 3, 4, 5}), 4, 5)
// true
AnyBy

AnyBy returns whether a has an element for that f returns true.

hi.AnyBy([]int{1, 2, 3, 4, 5}, func(_, v int) bool { return v > 3 })
// true

Experimental: AnyBy on iterators

it.AnyBy(it.Range(5), func(v int) bool { return v > 3 })
// true

it.AnyBy2(it.SliceIter([]int{1, 2, 3, 4, 5}), func(_, v int) bool { return v > 3 })
// true
All

All returns whether all elements of a are value.

hi.All([]int{1, 2, 3, 4, 5}, 5)
// false

hi.All([]int{5, 5, 5, 5, 5}, 5)
// true

Experimental: All on iterators

it.All(it.Range(1), 0)
// true

it.Any2(it.SliceIter([]int{1}), 0, 1)
// true
AllBy

AllBy returns whether f returns true for all elements in a.

hi.AllBy([]int{1, 2, 3, 4, 5}, func(_, v int) bool { return v > 3 })
// false

hi.AllBy([]int{4, 5, 6, 7, 8}, func(_, v int) bool { return v > 3 })
// true

Experimental: AllBy on iterators

it.AllBy(it.Range(5), func(v int) bool { return v < 5 })
// true

it.AllBy2(it.MapIter(map[int]string{0: "0", 1: "1", 2: "2", 3: "3", 4: "4"}), func(_ int, v string) bool { return v < "5" })
// true
Max

Max returns the maximum element of arguments.

hi.Max([]int{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5})
// optional.New(9)

Experimental: Max on iterators

it.Max(it.SliceValues([]int{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}))
// optional.New(9)
Min

Min returns the minimum element of arguments.

hi.Min([]int{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5})
// optional.New(1)

Experimental: Min on iterators

it.Min(it.SliceValues([]int{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}))
// optional.New(1)
Shuffle

Shuffle returns a shuffled copy of a.

s := hi.Shuffle([]int{1, 2, 3, 4, 5})
// []int{2, 3, 1, 5, 4}

Experimental: Shuffle on iterators

s := it.Shuffle(it.Range(5))
// it.SliceValues([]int{1, 2, 0, 4, 3})

s := it.Shuffle(it.SliceIter([]string{"zero", "one", "two"}))
// it.SliceValues([]tuple.Tuple2{tuple.New(1, "one"), tuple.New(0, "zero"), tuple.New(2, "two")})
Sample
v := hi.Sample([]int{1, 2, 3, 4, 5})
// optional.New(2)

Experimental: Shuffle on iterators

v := it.Sample(it.Range(5))
// optional.New(5)

v := it.Sample2(it.SliceIter([]string{"zero", "one", "two", "three", "four"}))
// optional.New(tuple.New2(2, "two"))
SampleN
s := hi.Sample([]int{1, 2, 3, 4, 5}, 3)
// []int{4, 5, 2}

Experimental: ShuffleN on iterators

s := it.SampleN(it.Range(5))
// []int{3, 4, 1}

s := it.SampleN2(it.SliceIter([]string{"zero", "one", "two", "three", "four"}), 3)
// []tuple.Tuple2{tuple.New2(3, "three"), tuple.New2(4, "four"), tuple.New2(1, "one")}
Ptr
p := hi.Ptr("hello")
// *p = "hello"

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T comparable](a []T, value T) bool

All returns whether all elements of a are value.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	fmt.Println(hi.All([]int{1, 2, 3, 4, 5}, 5))
	fmt.Println(hi.All([]int{5, 5, 5, 5, 5}, 5))
}
Output:

false
true

func AllBy

func AllBy[T any](a []T, f func(int, T) bool) bool

AllBy returns whether f returns true for all elements in a.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	fmt.Println(hi.AllBy([]int{1, 2, 3, 4, 5}, func(_, v int) bool { return v > 3 }))
	fmt.Println(hi.AllBy([]int{4, 5, 6, 7, 8}, func(_, v int) bool { return v > 3 }))
}
Output:

false
true

func Any

func Any[T comparable](a []T, value T) bool

Any returns whether a has value at least one.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	input := []int{1, 2, 3, 4, 5}
	fmt.Println(hi.Any(input, 5))
	fmt.Println(hi.Any(input, 6))
}
Output:

true
false

func AnyBy

func AnyBy[T any](a []T, f func(index int, value T) bool) bool

AnyBy returns whether a has an element for that f returns true.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	input := []int{1, 2, 3, 4, 5}
	fmt.Println(hi.AnyBy(input, func(_, v int) bool { return v > 3 }))
	fmt.Println(hi.AnyBy(input, func(_, v int) bool { return v > 10 }))
}
Output:

true
false

func Chain

func Chain[S ~[]T, T any](a ...S) []T

Chain returns a slice of all elements from all slices.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	input1 := []int{1, 2, 3}
	input2 := []int{4, 5, 6}
	output := hi.Chain(input1, input2)
	for _, v := range output {
		fmt.Println(v)
	}
}
Output:

1
2
3
4
5
6

func Chunk

func Chunk[S ~[]T, T any](a S, size int) []S

Chunk creates a slice of elements split into groups the length of size.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	input := []int{1, 2, 3, 4, 5}
	output := hi.Chunk(input, 2)
	for _, v := range output {
		fmt.Println(v)
	}
}
Output:

[1 2]
[3 4]
[5]

func Compress

func Compress[S ~[]T, T any](a S, selectors []bool) S

Compress makes a slice of the elements in a for which the corresponding element in selectors is true.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	input := []int{1, 2, 3, 4, 5}
	selectors := []bool{true, false, true, false, true}
	output := hi.Compress(input, selectors)
	for _, v := range output {
		fmt.Println(v)
	}
}
Output:

1
3
5

func Count

func Count[T comparable](a []T, value T) int

Count counts the number of elements in the collection that compare equal to value.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	input := []int{1, 2, 3, 4, 5}
	fmt.Println(hi.Count(input, 3))
}
Output:

1

func CountBy

func CountBy[T any](a []T, counter func(int, T) bool) int

CountBy counts the number of elements that counter returns true.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	input := []int{1, 2, 3, 4, 5}
	fmt.Println(hi.CountBy(input, func(_, v int) bool { return v%2 == 0 }))
}
Output:

2

func DropWhile

func DropWhile[S ~[]T, T any](a S, predicate func(int, T) bool) S

DropWhile returns a slice of the remaining elements after dropping the longest prefix of elements that satisfy predicate.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	input := []int{1, 4, 6, 4, 1}
	output := hi.DropWhile(input, func(_, v int) bool {
		return v < 5
	})
	for _, v := range output {
		fmt.Println(v)
	}
}
Output:

6
4
1

func Filter

func Filter[S ~[]T, T any](a S, filter func(int, T) bool) S

Filter iterates over elements of collection, returning a slice of all elements predicate returns true for.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	input := []int{1, 2, 3, 4, 5}
	output := hi.Filter(input, func(_, v int) bool {
		return v%2 == 0
	})
	for _, v := range output {
		fmt.Println(v)
	}
}
Output:

2
4

func FilterFalse

func FilterFalse[S ~[]T, T any](a S, filter func(int, T) bool) S

FilterFalse iterates over elements of collection, returning a slice of all elements predicate returns false for.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	input := []int{1, 2, 3, 4, 5}
	output := hi.FilterFalse(input, func(_, v int) bool {
		return v%2 == 0
	})
	for _, v := range output {
		fmt.Println(v)
	}
}
Output:

1
3
5

func GroupBy

func GroupBy[S ~[]T, T any, K comparable](a S, f func(int, T) K) map[K]S

GroupBy returns a map of slices, where each slice contains elements of a grouped by the result of f.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	input := []int{1, 2, 3, 4, 5}
	output := hi.GroupBy(input, func(_, v int) int {
		return v % 2
	})
	for k, v := range output {
		fmt.Println(k, v)
	}
}
Output:

0 [2 4]
1 [1 3 5]

func If

func If[T any](cond bool, trueValue, falseValue T) T

If is a 1 line if/else expression.

Example (Even)
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	v := 124
	fmt.Println(hi.If(v%2 == 0, "even", "odd"))
}
Output:

even
Example (Odd)
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	v := 125
	fmt.Println(hi.If(v%2 == 0, "even", "odd"))
}
Output:

odd

func Map

func Map[S1 ~[]T1, S2 []T2, T1, T2 any](a S1, mapper func(int, T1) T2) S2

Map calls mapper on each element of a and returns the result of its result in a slice.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	input := []int{1, 2, 3, 4, 5}
	output := hi.Map(input, func(_, v int) string {
		return fmt.Sprintf("(%d)", v)
	})
	for _, v := range output {
		fmt.Println(v)
	}
}
Output:

(1)
(2)
(3)
(4)
(5)

func Max

func Max[S ~[]T, T cmp.Ordered](a S) optional.Optional[T]

Max returns the maximal value in a.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	max := hi.Max([]int{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5})
	fmt.Println(max.Get())
}
Output:

9

func Min

func Min[S ~[]T, T cmp.Ordered](a S) optional.Optional[T]

Min returns the minimal value in a.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	min := hi.Min([]int{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5})
	fmt.Println(min.Get())
}
Output:

1

func Must

func Must(err error)

Must panics if err is not nil.

func Must0

func Must0(err error)

Must0 panics if err is not nil.

func Must1

func Must1[T1 any](v1 T1, err error) T1

Must1 returns v1 if err is nil, otherwise it panics.

func Must2

func Must2[T1, T2 any](v1 T1, v2 T2, err error) (T1, T2)

Must2 returns (v1, v2) if err is nil, otherwise it panics.

func Must3

func Must3[T1, T2, T3 any](v1 T1, v2 T2, v3 T3, err error) (T1, T2, T3)

Must3 returns (v1, v2, v3) if err is nil, otherwise it panics.

func Must4

func Must4[T1, T2, T3, T4 any](v1 T1, v2 T2, v3 T3, v4 T4, err error) (T1, T2, T3, T4)

Must4 returns (v1, v2, v3, v4) if err is nil, otherwise it panics.

func Must5

func Must5[T1, T2, T3, T4, T5 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, err error) (T1, T2, T3, T4, T5)

Must5 returns (v1, v2, v3, v4, v5) if err is nil, otherwise it panics.

func Must6

func Must6[T1, T2, T3, T4, T5, T6 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, v6 T6, err error) (T1, T2, T3, T4, T5, T6)

Must6 returns (v1, v2, v3, v4, v5, v6) if err is nil, otherwise it panics.

func Must7

func Must7[T1, T2, T3, T4, T5, T6, T7 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, v6 T6, v7 T7, err error) (T1, T2, T3, T4, T5, T6, T7)

Must7 returns (v1, v2, v3, v4, v5, v6, v7) if err is nil, otherwise it panics.

func Must8

func Must8[T1, T2, T3, T4, T5, T6, T7, T8 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, v6 T6, v7 T7, v8 T8, err error) (T1, T2, T3, T4, T5, T6, T7, T8)

Must8 returns (v1, v2, v3, v4, v5, v6, v7, v8) if err is nil, otherwise it panics.

func Negate

func Negate[F ~func(T) bool, T any](f F) F

Negate returns a function that negates the result of f.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	f := func(v int) bool { return v > 3 }
	g := hi.Negate(f)

	for i := 0; i < 5; i++ {
		fmt.Printf("%d: %t, %t\n", i, f(i), g(i))
	}

}
Output:

0: false, true
1: false, true
2: false, true
3: false, true
4: true, false

func Negate2

func Negate2[F ~func(T, U) bool, T, U any](f F) F

Negate2 returns a function that negates the result of f.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	f := func(a, b int) bool { return a > b }
	g := hi.Negate2(f)

	for i := 0; i < 5; i++ {
		fmt.Printf("%d: %t, %t\n", i, f(i, 3), g(i, 3))
	}

}
Output:

0: false, true
1: false, true
2: false, true
3: false, true
4: true, false

func Pairwise

func Pairwise[S ~[]T, T any](a S) []tuple.Tuple2[T, T]

Pairwise makes a slice of all adjacent pairs of elements.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	input := []int{1, 2, 3, 4, 5}
	output := hi.Pairwise(input)
	for _, v := range output {
		fmt.Println(v)
	}
}
Output:

(1, 2)
(2, 3)
(3, 4)
(4, 5)

func Ptr

func Ptr[T any](v T) *T

Ptr returns a pointer to v.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	p := hi.Ptr("hello")
	fmt.Println(*p)

}
Output:

hello

func RepeatN

func RepeatN[T any](v T, n int) []T

RepeatN returns a slice consisting of n copies of v.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	output := hi.RepeatN("hello", 3)
	for _, v := range output {
		fmt.Println(v)
	}
}
Output:

hello
hello
hello

func Sample added in v0.1.1

func Sample[S ~[]T, T any](a S) optional.Optional[T]

Sample returns a random element from a.

func SampleN added in v0.1.1

func SampleN[S ~[]T, T any](a S, n int) S

SampleN returns n random elements from a.

func Shuffle added in v0.1.1

func Shuffle[S ~[]T, T any](a S) S

Shuffle returns a shuffled copy of a.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	input := []int{1, 2, 3, 4, 5}
	output := hi.Shuffle(input)
	for _, v := range output {
		fmt.Println(v)
	}
}
Output:

1
2
3
4
5

func Slice

func Slice[S ~[]T, T any](s S, start, stop, step int) S

Slice returns a Python-like slice of s. Unlike Go's builtin slice, Slice can handle negative indices and step, and always returns a new slice. Negative indices count from the end of the slice (-1 means the last element).

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	input := []int{1, 2, 3, 4, 5}
	output := hi.Slice(input, 1, 3, 1)
	fmt.Println(output)
}
Output:

[2 3]

func TakeWhile

func TakeWhile[S ~[]T, T any](a S, f func(int, T) bool) S

TakeWhile returns a slice of the longest prefix of elements that satisfy predicate f.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	input := []int{1, 4, 6, 4, 1}
	output := hi.TakeWhile(input, func(_, v int) bool {
		return v < 5
	})
	for _, v := range output {
		fmt.Println(v)
	}
}
Output:

1
4

func Unzip10

func Unzip10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10 any](s []tuple.Tuple10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]) ([]T1, []T2, []T3, []T4, []T5, []T6, []T7, []T8, []T9, []T10)

Unzip10 converts a slice of 10-tuple to slices of each elements.

func Unzip11

func Unzip11[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11 any](s []tuple.Tuple11[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11]) ([]T1, []T2, []T3, []T4, []T5, []T6, []T7, []T8, []T9, []T10, []T11)

Unzip11 converts a slice of 11-tuple to slices of each elements.

func Unzip12

func Unzip12[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12 any](s []tuple.Tuple12[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12]) ([]T1, []T2, []T3, []T4, []T5, []T6, []T7, []T8, []T9, []T10, []T11, []T12)

Unzip12 converts a slice of 12-tuple to slices of each elements.

func Unzip13

func Unzip13[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13 any](s []tuple.Tuple13[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13]) ([]T1, []T2, []T3, []T4, []T5, []T6, []T7, []T8, []T9, []T10, []T11, []T12, []T13)

Unzip13 converts a slice of 13-tuple to slices of each elements.

func Unzip14

func Unzip14[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14 any](s []tuple.Tuple14[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14]) ([]T1, []T2, []T3, []T4, []T5, []T6, []T7, []T8, []T9, []T10, []T11, []T12, []T13, []T14)

Unzip14 converts a slice of 14-tuple to slices of each elements.

func Unzip15

func Unzip15[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15 any](s []tuple.Tuple15[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15]) ([]T1, []T2, []T3, []T4, []T5, []T6, []T7, []T8, []T9, []T10, []T11, []T12, []T13, []T14, []T15)

Unzip15 converts a slice of 15-tuple to slices of each elements.

func Unzip16

func Unzip16[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16 any](s []tuple.Tuple16[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16]) ([]T1, []T2, []T3, []T4, []T5, []T6, []T7, []T8, []T9, []T10, []T11, []T12, []T13, []T14, []T15, []T16)

Unzip16 converts a slice of 16-tuple to slices of each elements.

func Unzip2

func Unzip2[T1, T2 any](s []tuple.Tuple2[T1, T2]) ([]T1, []T2)

Unzip2 converts a slice of 2-tuple to slices of each elements.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
	"github.com/shogo82148/hi/tuple"
)

func main() {
	slice := []tuple.Tuple2[int, string]{
		tuple.New2(1, "one"),
		tuple.New2(2, "two"),
		tuple.New2(3, "three"),
		tuple.New2(4, "four"),
		tuple.New2(5, "five"),
	}
	s1, s2 := hi.Unzip2(slice)
	for _, v := range s1 {
		fmt.Println(v)
	}
	for _, v := range s2 {
		fmt.Println(v)
	}
}
Output:

1
2
3
4
5
one
two
three
four
five

func Unzip3

func Unzip3[T1, T2, T3 any](s []tuple.Tuple3[T1, T2, T3]) ([]T1, []T2, []T3)

Unzip3 converts a slice of 3-tuple to slices of each elements.

func Unzip4

func Unzip4[T1, T2, T3, T4 any](s []tuple.Tuple4[T1, T2, T3, T4]) ([]T1, []T2, []T3, []T4)

Unzip4 converts a slice of 4-tuple to slices of each elements.

func Unzip5

func Unzip5[T1, T2, T3, T4, T5 any](s []tuple.Tuple5[T1, T2, T3, T4, T5]) ([]T1, []T2, []T3, []T4, []T5)

Unzip5 converts a slice of 5-tuple to slices of each elements.

func Unzip6

func Unzip6[T1, T2, T3, T4, T5, T6 any](s []tuple.Tuple6[T1, T2, T3, T4, T5, T6]) ([]T1, []T2, []T3, []T4, []T5, []T6)

Unzip6 converts a slice of 6-tuple to slices of each elements.

func Unzip7

func Unzip7[T1, T2, T3, T4, T5, T6, T7 any](s []tuple.Tuple7[T1, T2, T3, T4, T5, T6, T7]) ([]T1, []T2, []T3, []T4, []T5, []T6, []T7)

Unzip7 converts a slice of 7-tuple to slices of each elements.

func Unzip8

func Unzip8[T1, T2, T3, T4, T5, T6, T7, T8 any](s []tuple.Tuple8[T1, T2, T3, T4, T5, T6, T7, T8]) ([]T1, []T2, []T3, []T4, []T5, []T6, []T7, []T8)

Unzip8 converts a slice of 8-tuple to slices of each elements.

func Unzip9

func Unzip9[T1, T2, T3, T4, T5, T6, T7, T8, T9 any](s []tuple.Tuple9[T1, T2, T3, T4, T5, T6, T7, T8, T9]) ([]T1, []T2, []T3, []T4, []T5, []T6, []T7, []T8, []T9)

Unzip9 converts a slice of 9-tuple to slices of each elements.

func Zero

func Zero[T any]() T

Zero returns a zero value of the type.

func Zip10

func Zip10[S []tuple.Tuple10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10], S1 ~[]T1, S2 ~[]T2, S3 ~[]T3, S4 ~[]T4, S5 ~[]T5, S6 ~[]T6, S7 ~[]T7, S8 ~[]T8, S9 ~[]T9, S10 ~[]T10, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10 any](s1 S1, s2 S2, s3 S3, s4 S4, s5 S5, s6 S6, s7 S7, s8 S8, s9 S9, s10 S10) S

Zip10 returns a slice of 10-tuples. The returned slice have the length of the shortest slice.

func Zip11

func Zip11[S []tuple.Tuple11[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11], S1 ~[]T1, S2 ~[]T2, S3 ~[]T3, S4 ~[]T4, S5 ~[]T5, S6 ~[]T6, S7 ~[]T7, S8 ~[]T8, S9 ~[]T9, S10 ~[]T10, S11 ~[]T11, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11 any](s1 S1, s2 S2, s3 S3, s4 S4, s5 S5, s6 S6, s7 S7, s8 S8, s9 S9, s10 S10, s11 S11) S

Zip11 returns a slice of 11-tuples. The returned slice have the length of the shortest slice.

func Zip12

func Zip12[S []tuple.Tuple12[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12], S1 ~[]T1, S2 ~[]T2, S3 ~[]T3, S4 ~[]T4, S5 ~[]T5, S6 ~[]T6, S7 ~[]T7, S8 ~[]T8, S9 ~[]T9, S10 ~[]T10, S11 ~[]T11, S12 ~[]T12, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12 any](s1 S1, s2 S2, s3 S3, s4 S4, s5 S5, s6 S6, s7 S7, s8 S8, s9 S9, s10 S10, s11 S11, s12 S12) S

Zip12 returns a slice of 12-tuples. The returned slice have the length of the shortest slice.

func Zip13

func Zip13[S []tuple.Tuple13[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13], S1 ~[]T1, S2 ~[]T2, S3 ~[]T3, S4 ~[]T4, S5 ~[]T5, S6 ~[]T6, S7 ~[]T7, S8 ~[]T8, S9 ~[]T9, S10 ~[]T10, S11 ~[]T11, S12 ~[]T12, S13 ~[]T13, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13 any](s1 S1, s2 S2, s3 S3, s4 S4, s5 S5, s6 S6, s7 S7, s8 S8, s9 S9, s10 S10, s11 S11, s12 S12, s13 S13) S

Zip13 returns a slice of 13-tuples. The returned slice have the length of the shortest slice.

func Zip14

func Zip14[S []tuple.Tuple14[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14], S1 ~[]T1, S2 ~[]T2, S3 ~[]T3, S4 ~[]T4, S5 ~[]T5, S6 ~[]T6, S7 ~[]T7, S8 ~[]T8, S9 ~[]T9, S10 ~[]T10, S11 ~[]T11, S12 ~[]T12, S13 ~[]T13, S14 ~[]T14, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14 any](s1 S1, s2 S2, s3 S3, s4 S4, s5 S5, s6 S6, s7 S7, s8 S8, s9 S9, s10 S10, s11 S11, s12 S12, s13 S13, s14 S14) S

Zip14 returns a slice of 14-tuples. The returned slice have the length of the shortest slice.

func Zip15

func Zip15[S []tuple.Tuple15[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15], S1 ~[]T1, S2 ~[]T2, S3 ~[]T3, S4 ~[]T4, S5 ~[]T5, S6 ~[]T6, S7 ~[]T7, S8 ~[]T8, S9 ~[]T9, S10 ~[]T10, S11 ~[]T11, S12 ~[]T12, S13 ~[]T13, S14 ~[]T14, S15 ~[]T15, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15 any](s1 S1, s2 S2, s3 S3, s4 S4, s5 S5, s6 S6, s7 S7, s8 S8, s9 S9, s10 S10, s11 S11, s12 S12, s13 S13, s14 S14, s15 S15) S

Zip15 returns a slice of 15-tuples. The returned slice have the length of the shortest slice.

func Zip16

func Zip16[S []tuple.Tuple16[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16], S1 ~[]T1, S2 ~[]T2, S3 ~[]T3, S4 ~[]T4, S5 ~[]T5, S6 ~[]T6, S7 ~[]T7, S8 ~[]T8, S9 ~[]T9, S10 ~[]T10, S11 ~[]T11, S12 ~[]T12, S13 ~[]T13, S14 ~[]T14, S15 ~[]T15, S16 ~[]T16, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16 any](s1 S1, s2 S2, s3 S3, s4 S4, s5 S5, s6 S6, s7 S7, s8 S8, s9 S9, s10 S10, s11 S11, s12 S12, s13 S13, s14 S14, s15 S15, s16 S16) S

Zip16 returns a slice of 16-tuples. The returned slice have the length of the shortest slice.

func Zip2

func Zip2[S []tuple.Tuple2[T1, T2], S1 ~[]T1, S2 ~[]T2, T1, T2 any](s1 S1, s2 S2) S

Zip2 returns a slice of 2-tuples. The returned slice have the length of the shortest slice.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	ints := []int{1, 2, 3, 4, 5}
	strings := []string{"one", "two", "three", "four", "five"}
	slice := hi.Zip2(ints, strings)
	for _, v := range slice {
		fmt.Println(v)
	}
}
Output:

(1, one)
(2, two)
(3, three)
(4, four)
(5, five)

func Zip3

func Zip3[S []tuple.Tuple3[T1, T2, T3], S1 ~[]T1, S2 ~[]T2, S3 ~[]T3, T1, T2, T3 any](s1 S1, s2 S2, s3 S3) S

Zip3 returns a slice of 3-tuples. The returned slice have the length of the shortest slice.

func Zip4

func Zip4[S []tuple.Tuple4[T1, T2, T3, T4], S1 ~[]T1, S2 ~[]T2, S3 ~[]T3, S4 ~[]T4, T1, T2, T3, T4 any](s1 S1, s2 S2, s3 S3, s4 S4) S

Zip4 returns a slice of 4-tuples. The returned slice have the length of the shortest slice.

func Zip5

func Zip5[S []tuple.Tuple5[T1, T2, T3, T4, T5], S1 ~[]T1, S2 ~[]T2, S3 ~[]T3, S4 ~[]T4, S5 ~[]T5, T1, T2, T3, T4, T5 any](s1 S1, s2 S2, s3 S3, s4 S4, s5 S5) S

Zip5 returns a slice of 5-tuples. The returned slice have the length of the shortest slice.

func Zip6

func Zip6[S []tuple.Tuple6[T1, T2, T3, T4, T5, T6], S1 ~[]T1, S2 ~[]T2, S3 ~[]T3, S4 ~[]T4, S5 ~[]T5, S6 ~[]T6, T1, T2, T3, T4, T5, T6 any](s1 S1, s2 S2, s3 S3, s4 S4, s5 S5, s6 S6) S

Zip6 returns a slice of 6-tuples. The returned slice have the length of the shortest slice.

func Zip7

func Zip7[S []tuple.Tuple7[T1, T2, T3, T4, T5, T6, T7], S1 ~[]T1, S2 ~[]T2, S3 ~[]T3, S4 ~[]T4, S5 ~[]T5, S6 ~[]T6, S7 ~[]T7, T1, T2, T3, T4, T5, T6, T7 any](s1 S1, s2 S2, s3 S3, s4 S4, s5 S5, s6 S6, s7 S7) S

Zip7 returns a slice of 7-tuples. The returned slice have the length of the shortest slice.

func Zip8

func Zip8[S []tuple.Tuple8[T1, T2, T3, T4, T5, T6, T7, T8], S1 ~[]T1, S2 ~[]T2, S3 ~[]T3, S4 ~[]T4, S5 ~[]T5, S6 ~[]T6, S7 ~[]T7, S8 ~[]T8, T1, T2, T3, T4, T5, T6, T7, T8 any](s1 S1, s2 S2, s3 S3, s4 S4, s5 S5, s6 S6, s7 S7, s8 S8) S

Zip8 returns a slice of 8-tuples. The returned slice have the length of the shortest slice.

func Zip9

func Zip9[S []tuple.Tuple9[T1, T2, T3, T4, T5, T6, T7, T8, T9], S1 ~[]T1, S2 ~[]T2, S3 ~[]T3, S4 ~[]T4, S5 ~[]T5, S6 ~[]T6, S7 ~[]T7, S8 ~[]T8, S9 ~[]T9, T1, T2, T3, T4, T5, T6, T7, T8, T9 any](s1 S1, s2 S2, s3 S3, s4 S4, s5 S5, s6 S6, s7 S7, s8 S8, s9 S9) S

Zip9 returns a slice of 9-tuples. The returned slice have the length of the shortest slice.

Types

type Addable

type Addable interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
		~float32 | ~float64 |
		~complex64 | ~complex128
}

Addable is a type that can be added and subtracted.

type SwitchCase

type SwitchCase[T any, P comparable] struct {
	// contains filtered or unexported fields
}

func Switch

func Switch[T any, P comparable](predicate P) SwitchCase[T, P]
Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi"
)

func main() {
	v := 2
	fmt.Println(hi.Switch[string](v).
		Case(1, "one").
		Case(2, "two").
		Case(3, "three").
		Default("four or greater"))
}
Output:

two

func (SwitchCase[T, P]) Case

func (s SwitchCase[T, P]) Case(val P, result T) SwitchCase[T, P]

func (SwitchCase[T, P]) Default

func (s SwitchCase[T, P]) Default(result T) T

Directories

Path Synopsis
Package it provides utilities for iterators.
Package it provides utilities for iterators.
Package list implements a generic doubly linked list.
Package list implements a generic doubly linked list.
The package sync provides same features as the standard sync package, but with generics.
The package sync provides same features as the standard sync package, but with generics.
Code generated by generate-tuples.pl; DO NOT EDIT.
Code generated by generate-tuples.pl; DO NOT EDIT.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL