ranges

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T any](r golang.Range[T], values ...T) bool

All return true if all value in range

检查是否全部值均存在在范围内/区间内

func Any

func Any[T any](r golang.Range[T], values ...T) bool

Any return true if any value in range

检查是否有存在在范围内/区间内的值

func Between

func Between[T cmp.Ordered](lower, upper T) golang.Range[T]

Between create a new range of ordered value

创建一个有序值的区间

Example
package main

import (
	"fmt"

	"github.com/keepitlight/golang/ranges"
)

func main() {
	r := ranges.Between(90, 100)
	l, u := ranges.Bounds(r)
	fmt.Println(l, u)
	fmt.Println(ranges.In(r, 50), ranges.In(r, 95))
	fmt.Println(ranges.Pick(r, 1, 2, 3, 4, 5, 99, 101))
	fmt.Println(ranges.Unpick(r, 1, 2, 3, 4, 5, 99, 101))
}
Output:

90 100
false true
[99]
[1 2 3 4 5 101]

func Bounds

func Bounds[R golang.Range[T], T any](r R) (lower, upper T)

Bounds get bounds of range

获取范围边界值

Example
package main

import (
	"fmt"
	"time"

	"github.com/keepitlight/golang"
	"github.com/keepitlight/golang/ranges"
)

func main() {
	n := time.Now()
	s := golang.AddTime(n, -1, 0, 0, 0)
	r := ranges.Time(&s, &n)
	l, u := ranges.Bounds(r)
	fmt.Println(u.Sub(*l))
}
Output:

1h0m0s

func Duration

func Duration(t golang.Range[*time.Time]) time.Duration

Duration return duration of time range

返回时间范围的持续时长

Example
package main

import (
	"fmt"
	"time"

	"github.com/keepitlight/golang/ranges"
)

func main() {
	s, e := time.Now(), time.Now().Add(time.Hour)
	// 实际运行时有运行时时间差
	t := ranges.Time(&s, &e)
	fmt.Println(ranges.Duration(t).Round(time.Second))
	// 舍掉时间计算的时间差
	

func In

func In[T any](r golang.Range[T], v T) bool

In checks value v weather is in the range r.

检查值 v 是否在范围 r 内。

func Intersect

func Intersect[T any](a, b golang.Range[T]) (golang.Range[T], bool)

Intersect returns the intersection of two ranges, a and b, or nil if one of any is `nil` or non-overlapping.

返回 a 和 b 的交集,如果任一一个是 `nil` 或者不相交,则返回 nil。

func Interval

func Interval(r golang.Range[*time.Time], options ...TimeOption) golang.Interval[*time.Time]

func Merge

func Merge[T any](a, b golang.Range[T]) (golang.Range[T], bool)

Merge combines two ranges, a and b, returning nil if they're both `nil` or non-overlapping, and the non-nil range if one of them is nil.

合并两个范围 a 和 b,如果它们都是 `nil` 或者不相交,则返回 nil,否则返回非空的。

Example
package main

import (
	"fmt"
	"time"

	"github.com/keepitlight/golang"
	"github.com/keepitlight/golang/ranges"
)

func main() {
	s, _ := time.Parse(golang.DateTimeZone, "2023-07-07 08:10:00 UTC")
	e1 := s.Add(time.Hour)
	e2 := s.Add(-time.Hour)
	i := s.Add(10 * time.Minute)
	t1 := ranges.Time(&s, &e1, ranges.WithInitialValue(&i), ranges.WithInterval(time.Minute))
	t2 := ranges.Time(&s, &e2, ranges.WithInitialValue(&i), ranges.WithInterval(time.Minute))
	m, ok := ranges.Merge[*time.Time](t1, t2)
	l, u := m.Bounds()
	fmt.Println(l, u, ok)
}
Output:

2023-07-07 07:10:00 +0000 UTC 2023-07-07 09:10:00 +0000 UTC true

func New

func New[T any](lower, upper T, comparer func(a, b T) int) golang.Range[T]

New create a new range, comparer is a function to compare two value a and b, if a < b return -1, a == b return 0, a > b return 1, nil if comparer is nil.

使用比较函数创建一个范围,函数 comparer 比较两个值 a 和 b,如果 a < b 返回 -1,a == b 返回 0,a > b 返回 1。 如果 compare 为 nil,则返回 nil。

func Pick

func Pick[T any](r golang.Range[T], values ...T) (found []T)

Pick all value in range

挑选在范围内/区间内的值

func Since

func Since(start *time.Time) golang.Range[*time.Time]

Since like time.Since to create a new time range from start to now, nil if start is nil or start is after now.

从 start 到现在创建一个时间范围,如果 start 为 nil 或 start 在现在之后,则返回 nil

Example
package main

import (
	"fmt"
	"time"

	"github.com/keepitlight/golang/ranges"
)

func main() {
	s := time.Now().Add(-time.Hour)
	// 实际运行时有运行时时间差
	t := ranges.Since(&s)
	fmt.Println(ranges.Duration(t).Round(time.Second))
	// 舍掉时间计算的时间差
	

func Time

func Time(start, end *time.Time, options ...TimeOption) golang.Interval[*time.Time]

Time creates a new time range, swap if the argument start is after the argument end.

创建一个时间范围,如果 start 在 end 之后,则交换 start 和 end

func Unpick

func Unpick[T any](r golang.Range[T], values ...T) (found []T)

Unpick picks all value not in range

挑选不在范围内/区间内的值

Types

type Incremented

type Incremented struct {
}

type TimeOption

type TimeOption func(*timeInterval)

func WithInitialValue

func WithInitialValue(v *time.Time) TimeOption

WithInitialValue set initial value of time range, default is start

设置时间范围的初始值,默认为 start

func WithInterval

func WithInterval(duration time.Duration) TimeOption

WithInterval set interval of time range, default is 1 hour

设置时间范围的间隔时长,默认为 1 小时

Jump to

Keyboard shortcuts

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