• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang ty.Check函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/BurntSushi/ty.Check函数的典型用法代码示例。如果您正苦于以下问题:Golang Check函数的具体用法?Golang Check怎么用?Golang Check使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了Check函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: MinMaxInt

// MinMaxInt has a parametric type:
//
//  func MinMaxInt(f func(A) int64, xs []A) (int64, int64)
//
// MinMaxInt returns the minimum and maximum values returned from f, if the list is
// of length 0, it will return 0 and 0
func MinMaxInt(f, xs interface{}) (int64, int64) {
	chk := ty.Check(
		new(func(func(ty.A) int64, []ty.A)),
		f, xs)
	vp, vxs := chk.Args[0], chk.Args[1]

	xsLen := vxs.Len()
	if xsLen > 0 {
		min := call1(vp, vxs.Index(0)).Int()
		max := min
		for i := 1; i < xsLen; i++ {
			vx := vxs.Index(i)
			local := call1(vp, vx).Int()
			if local < min {
				min = local
			}
			if local > max {
				max = local
			}
		}
		return min, max
	}

	return 0, 0
}
开发者ID:acsellers,项目名称:ty,代码行数:31,代码来源:min_max_sum.go


示例2: ParMapN

// ParMapN has a parametric type:
//
//	func ParMapN(f func(A) B, xs []A, n int) []B
//
// ParMapN is just like Map, except it applies `f` to each element in `xs`
// concurrently using `n` worker goroutines.
//
// It is important that `f` not be a trivial operation, otherwise the overhead
// of executing it concurrently will result in worse performance than using
// a `Map`.
func ParMapN(f, xs interface{}, n int) interface{} {
	chk := ty.Check(
		new(func(func(ty.A) ty.B, []ty.A) []ty.B),
		f, xs)
	vf, vxs, tys := chk.Args[0], chk.Args[1], chk.Returns[0]

	xsLen := vxs.Len()
	ys := reflect.MakeSlice(tys, xsLen, xsLen)

	if n < 1 {
		n = 1
	}
	work := make(chan int, n)
	wg := new(sync.WaitGroup)
	for i := 0; i < n; i++ {
		wg.Add(1)
		go func() {
			for j := range work {
				// Good golly miss molly. Is `reflect.Value.Index`
				// safe to access/set from multiple goroutines?
				// XXX: If not, we'll need an extra wave of allocation to
				// use real slices of `reflect.Value`.
				ys.Index(j).Set(call1(vf, vxs.Index(j)))
			}
			wg.Done()
		}()
	}
	for i := 0; i < xsLen; i++ {
		work <- i
	}
	close(work)
	wg.Wait()
	return ys.Interface()
}
开发者ID:johnvilsack,项目名称:golang-stuff,代码行数:44,代码来源:list.go


示例3: Sort

// Sort has a parametric type:
//
//	func Sort(less func(x1 A, x2 A) bool, []A)
//
// Sort uses the standard library `sort` package to sort `xs` in place.
//
// `less` should be a function that returns true if and only if `x1` is less
// than `x2`.
func Sort(less, xs interface{}) {
	chk := ty.Check(
		new(func(func(ty.A, ty.A) bool, []ty.A)),
		less, xs)

	vless, vxs := chk.Args[0], chk.Args[1]
	sort.Sort(&sortable{vless, vxs, swapperOf(vxs.Type().Elem())})
}
开发者ID:ngaut,项目名称:ty,代码行数:16,代码来源:sort.go


示例4: Copy

// Copy has a parametric type:
//
//	func Copy(xs []A) []A
//
// Copy returns a copy of `xs` using Go's `copy` operation.
func Copy(xs interface{}) interface{} {
	chk := ty.Check(
		new(func([]ty.A) []ty.A),
		xs)
	vxs, tys := chk.Args[0], chk.Returns[0]

	xsLen := vxs.Len()
	vys := reflect.MakeSlice(tys, xsLen, xsLen)
	reflect.Copy(vys, vxs)
	return vys.Interface()
}
开发者ID:johnvilsack,项目名称:golang-stuff,代码行数:16,代码来源:list.go


示例5: Each

// Each has a parametric type:
//
//  func Each(f func(A), xs []A)
//
// Each runs `f` across each element in `xs`.
func Each(f, xs interface{}) {
	chk := ty.Check(
		new(func(func(ty.A), []ty.A)),
		f, xs)
	vf, vxs := chk.Args[0], chk.Args[1]

	xsLen := vxs.Len()
	for i := 0; i < xsLen; i++ {
		call(vf, vxs.Index(i))
	}
}
开发者ID:acsellers,项目名称:ty,代码行数:16,代码来源:list.go


示例6: First

func First(slice interface{}) (elem interface{}) {
	chk := ty.Check(
		new(func([]ty.A) ty.A),
		slice)
	sliceVal, elemTyp := chk.Args[0], chk.Returns[0]

	if sliceVal.IsNil() || sliceVal.Len() == 0 {
		return reflect.Zero(elemTyp).Interface()
	}
	return sliceVal.Index(0).Interface()
}
开发者ID:ASAPPinc,项目名称:fun-go,代码行数:11,代码来源:Slices+Fun.go


示例7: Last

func Last(slice interface{}) (elem interface{}) {
	chk := ty.Check(
		new(func([]ty.A) ty.A),
		slice)
	vSlice, tElem := chk.Args[0], chk.Returns[0]

	if vSlice.IsNil() || vSlice.Len() == 0 {
		return reflect.Zero(tElem).Interface()
	}
	return vSlice.Index(vSlice.Len() - 1).Interface()
}
开发者ID:ASAPPinc,项目名称:fun-go,代码行数:11,代码来源:Slices+Fun.go


示例8: Values

// Values has a parametric type:
//
//	func Values(m map[A]B) []B
//
// Values returns a list of the values of `m` in an unspecified order.
func Values(m interface{}) interface{} {
	chk := ty.Check(
		new(func(map[ty.A]ty.B) []ty.B),
		m)
	vm, tvals := chk.Args[0], chk.Returns[0]

	vvals := reflect.MakeSlice(tvals, vm.Len(), vm.Len())
	for i, vkey := range vm.MapKeys() {
		vvals.Index(i).Set(vm.MapIndex(vkey))
	}
	return vvals.Interface()
}
开发者ID:johnvilsack,项目名称:golang-stuff,代码行数:17,代码来源:map.go


示例9: Reverse

// Reverse has a parametric type:
//
//	func Reverse(xs []A) []A
//
// Reverse returns a new slice that is the reverse of `xs`.
func Reverse(xs interface{}) interface{} {
	chk := ty.Check(
		new(func([]ty.A) []ty.A),
		xs)
	vxs, tys := chk.Args[0], chk.Returns[0]

	xsLen := vxs.Len()
	vys := reflect.MakeSlice(tys, xsLen, xsLen)
	for i := 0; i < xsLen; i++ {
		vys.Index(i).Set(vxs.Index(xsLen - 1 - i))
	}
	return vys.Interface()
}
开发者ID:johnvilsack,项目名称:golang-stuff,代码行数:18,代码来源:list.go


示例10: CycleEach

// CycleEach has a parametric type
//
//  func CycleEach(f func(A), xs []A, n int)
//
// CycleEach calls each element of xs with f in order n times
func CycleEach(f, xs interface{}, n int) {
	chk := ty.Check(
		new(func(func(ty.A), []ty.A, int)),
		f, xs, n)
	vp, vxs := chk.Args[0], chk.Args[1]

	xsLen := vxs.Len()
	for t := 0; t < n; t++ {
		for i := 0; i < xsLen; i++ {
			call(vp, vxs.Index(i))
		}
	}
}
开发者ID:acsellers,项目名称:ty,代码行数:18,代码来源:cycle.go


示例11: Concat

// Concat has a parametric type:
//
//	func Concat(xs [][]A) []A
//
// Concat returns a new flattened list by appending all elements of `xs`.
func Concat(xs interface{}) interface{} {
	chk := ty.Check(
		new(func([][]ty.A) []ty.A),
		xs)
	vxs, tflat := chk.Args[0], chk.Returns[0]

	xsLen := vxs.Len()
	vflat := reflect.MakeSlice(tflat, 0, xsLen*3)
	for i := 0; i < xsLen; i++ {
		vflat = reflect.AppendSlice(vflat, vxs.Index(i))
	}
	return vflat.Interface()
}
开发者ID:johnvilsack,项目名称:golang-stuff,代码行数:18,代码来源:list.go


示例12: Count

// Count has a parametric type:
//
//  func Count(f func(A) bool, xs []A) int
//
// Count returns the number of elements of xs for which f
// returns true
func Count(f, xs interface{}) (matches int) {
	chk := ty.Check(
		new(func(func(ty.A) bool, []ty.A)),
		f, xs)
	vp, vxs := chk.Args[0], chk.Args[1]

	xsLen := vxs.Len()
	for i := 0; i < xsLen; i++ {
		if call1(vp, vxs.Index(i)).Bool() {
			matches++
		}
	}
	return
}
开发者ID:acsellers,项目名称:ty,代码行数:20,代码来源:list_checkers.go


示例13: ShuffleGen

// ShuffleGen has a parametric type:
//
//	func ShuffleGen(xs []A, rng *rand.Rand)
//
// ShuffleGen shuffles `xs` in place using the given random number
// generator `rng`.
func ShuffleGen(xs interface{}, rng *rand.Rand) {
	chk := ty.Check(
		new(func([]ty.A, *rand.Rand)),
		xs, rng)
	vxs := chk.Args[0]

	// Implements the Fisher-Yates shuffle: http://goo.gl/Hb9vg
	xsLen := vxs.Len()
	swapper := swapperOf(vxs.Type().Elem())
	for i := xsLen - 1; i >= 1; i-- {
		j := rng.Intn(i + 1)
		swapper.swap(vxs.Index(i), vxs.Index(j))
	}
}
开发者ID:ngaut,项目名称:ty,代码行数:20,代码来源:rand.go


示例14: In

// In has a parametric type:
//
//	func In(needle A, haystack []A) bool
//
// In returns `true` if and only if `v` can be found in `xs`. The equality test
// used is Go's standard `==` equality and NOT deep equality.
//
// Note that this requires that `A` be a type that can be meaningfully compared.
func In(needle, haystack interface{}) bool {
	chk := ty.Check(
		new(func(ty.A, []ty.A) bool),
		needle, haystack)
	vhaystack := chk.Args[1]

	length := vhaystack.Len()
	for i := 0; i < length; i++ {
		if vhaystack.Index(i).Interface() == needle {
			return true
		}
	}
	return false
}
开发者ID:BurntSushi,项目名称:ty,代码行数:22,代码来源:list.go


示例15: Exists

// Exists has a parametric type:
//
//	func Exists(p func(A) bool, xs []A) bool
//
// Exists returns `true` if and only if an element in `xs` satisfies `p`.
func Exists(f, xs interface{}) bool {
	chk := ty.Check(
		new(func(func(ty.A) bool, []ty.A) bool),
		f, xs)
	vf, vxs := chk.Args[0], chk.Args[1]

	xsLen := vxs.Len()
	for i := 0; i < xsLen; i++ {
		if call1(vf, vxs.Index(i)).Interface().(bool) {
			return true
		}
	}
	return false
}
开发者ID:BurntSushi,项目名称:ty,代码行数:19,代码来源:list.go


示例16: None

// None has a parametric type
//
//  func None(f func(A) bool, xs []A) bool
//
// None returns true if none of the elements in xs caused f to return
// true, false otherwise
func None(f, xs interface{}) bool {
	chk := ty.Check(
		new(func(func(ty.A) bool, []ty.A)),
		f, xs)
	vp, vxs := chk.Args[0], chk.Args[1]

	xsLen := vxs.Len()
	for i := 0; i < xsLen; i++ {
		if call1(vp, vxs.Index(i)).Bool() {
			return false
		}
	}
	return true
}
开发者ID:acsellers,项目名称:ty,代码行数:20,代码来源:list_checkers.go


示例17: Set

// Set has a parametric type:
//
//	func Set(xs []A) map[A]bool
//
// Set creates a set from a list.
func Set(xs interface{}) interface{} {
	chk := ty.Check(
		new(func([]ty.A) map[ty.A]bool),
		xs)
	vxs, tset := chk.Args[0], chk.Returns[0]

	vtrue := reflect.ValueOf(true)
	vset := reflect.MakeMap(tset)
	xsLen := vxs.Len()
	for i := 0; i < xsLen; i++ {
		vset.SetMapIndex(vxs.Index(i), vtrue)
	}
	return vset.Interface()
}
开发者ID:johnvilsack,项目名称:golang-stuff,代码行数:19,代码来源:set.go


示例18: Map

// Map has a parametric type:
//
//	func Map(f func(A) B, xs []A) []B
//
// Map returns the list corresponding to the return value of applying
// `f` to each element in `xs`.
func Map(f, xs interface{}) interface{} {
	chk := ty.Check(
		new(func(func(ty.A) ty.B, []ty.A) []ty.B),
		f, xs)
	vf, vxs, tys := chk.Args[0], chk.Args[1], chk.Returns[0]

	xsLen := vxs.Len()
	vys := reflect.MakeSlice(tys, xsLen, xsLen)
	for i := 0; i < xsLen; i++ {
		vy := call1(vf, vxs.Index(i))
		vys.Index(i).Set(vy)
	}
	return vys.Interface()
}
开发者ID:johnvilsack,项目名称:golang-stuff,代码行数:20,代码来源:list.go


示例19: Detect

// Detect has a parametric type:
//
//  func Detect(f func(A) bool, xs []A) A
//
// Detect returns the first element for which f returns
// true, if none are returned it returns nil
func Detect(f, xs interface{}) interface{} {
	chk := ty.Check(
		new(func(func(ty.A) bool, []ty.A)),
		f, xs)
	vp, vxs := chk.Args[0], chk.Args[1]

	xsLen := vxs.Len()
	for i := 0; i < xsLen; i++ {
		if call1(vp, vxs.Index(i)).Bool() {
			return vxs.Index(i).Interface()
		}
	}
	return nil
}
开发者ID:acsellers,项目名称:ty,代码行数:20,代码来源:list_checkers.go


示例20: SumFloat

// SumFloat has a parametric type:
//
//  func SumFloat(f func(A) float64, xs []A) float64
//
// SumFloat returns the sum of the values returned from f
func SumFloat(f, xs interface{}) float64 {
	chk := ty.Check(
		new(func(func(ty.A) float64, []ty.A)),
		f, xs)
	vp, vxs := chk.Args[0], chk.Args[1]

	xsLen := vxs.Len()
	var sum float64
	for i := 0; i < xsLen; i++ {
		vx := vxs.Index(i)
		sum += call1(vp, vx).Float()
	}

	return sum
}
开发者ID:acsellers,项目名称:ty,代码行数:20,代码来源:min_max_sum.go



注:本文中的github.com/BurntSushi/ty.Check函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang fun.Filter函数代码示例发布时间:2022-05-24
下一篇:
Golang toml.MetaData类代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap