From 4aef9d6d22ff1e5edb5532cc7c13c1d97c6ea0a9 Mon Sep 17 00:00:00 2001 From: donutloop Date: Wed, 5 Jan 2022 12:38:14 +0100 Subject: [PATCH] Slice: Add none func (#13) Returns true whether no elements of this slice match the provided predicate func. Negated form of Every func --- README.md | 1 + README_zh-CN.md | 1 + slice/slice.go | 22 ++++++++++++++++++++++ slice/slice_test.go | 12 ++++++++++++ 4 files changed, 36 insertions(+) diff --git a/README.md b/README.md index 0cc6971..8016225 100644 --- a/README.md +++ b/README.md @@ -394,6 +394,7 @@ func Difference(slice1, slice2 interface{}) interface{} //creates an slice of wh func DeleteByIndex(slice interface{}, start int, end ...int) (interface{}, error) //delete the element of slice from start index to end index - 1 func Drop(slice interface{}, n int) interface{} //creates a slice with `n` elements dropped from the beginning when n > 0, or `n` elements dropped from the ending when n < 0 func Every(slice, function interface{}) bool //return true if all of the values in the slice pass the predicate function, function signature should be func(index int, value interface{}) bool +func None(slice, function interface{}) bool // return true if all the values in the slice mismatch the criteria func Filter(slice, function interface{}) interface{} //filter slice, function signature should be func(index int, value interface{}) bool func Find(slice, function interface{}) (interface{}, bool) //iterates over elements of slice, returning the first one that passes a truth test on function.function signature should be func(index int, value interface{}) bool . func FlattenDeep(slice interface{}) interface{} //flattens slice recursive diff --git a/README_zh-CN.md b/README_zh-CN.md index 6f7daea..fa5cf0f 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -395,6 +395,7 @@ func Difference(slice1, slice2 interface{}) interface{} //返回 func DeleteByIndex(slice interface{}, start int, end ...int) (interface{}, error) //删除切片中start到end位置的值 func Drop(slice interface{}, n int) interface{} //创建一个新切片,当n大于0时删除原切片前n个元素,当n小于0时删除原切片后n个元素 func Every(slice, function interface{}) bool //slice中所有元素都符合函数条件时返回true, 否则返回false. 函数签名:func(index int, value interface{}) bool +func None(slice, function interface{}) bool func Find(slice, function interface{}) (interface{}, bool)//查找slice中第一个符合条件的元素,函数签名:func(index int, value interface{}) bool func Filter(slice, function interface{}) interface{} //过滤slice, 函数签名:func(index int, value interface{}) bool func FlattenDeep(slice interface{}) interface{} //将slice递归为一维切片。 diff --git a/slice/slice.go b/slice/slice.go index 94fdb69..138481c 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -121,6 +121,28 @@ func Every(slice, function interface{}) bool { return currentLength == sv.Len() } +// None return true if all the values in the slice mismatch the criteria +// The function signature should be func(index int, value interface{}) bool . +func None(slice, function interface{}) bool { + sv := sliceValue(slice) + fn := functionValue(function) + + elemType := sv.Type().Elem() + if checkSliceCallbackFuncSignature(fn, elemType, reflect.ValueOf(true).Type()) { + panic("function param should be of type func(int, " + elemType.String() + ")" + reflect.ValueOf(true).Type().String()) + } + + var currentLength int + for i := 0; i < sv.Len(); i++ { + flag := fn.Call([]reflect.Value{reflect.ValueOf(i), sv.Index(i)})[0] + if !flag.Bool() { + currentLength++ + } + } + + return currentLength == sv.Len() +} + // Some return true if any of the values in the list pass the predicate function. // The function signature should be func(index int, value interface{}) bool . func Some(slice, function interface{}) bool { diff --git a/slice/slice_test.go b/slice/slice_test.go index 6dc9dbe..e802882 100644 --- a/slice/slice_test.go +++ b/slice/slice_test.go @@ -108,6 +108,18 @@ func TestEvery(t *testing.T) { } } +func TestNone(t *testing.T) { + nums := []int{1, 2, 3, 5} + check := func(i, num int) bool { + return num%2 == 1 + } + res := None(nums, check) + if res != false { + internal.LogFailedTestInfo(t, "Every", nums, false, res) + t.FailNow() + } +} + func TestSome(t *testing.T) { nums := []int{1, 2, 3, 5} isEven := func(i, num int) bool {