mirror of
https://github.com/IBM/fp-go.git
synced 2025-12-15 23:33:46 +02:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
05395aebb1 |
26
iterator/stateless/any.go
Normal file
26
iterator/stateless/any.go
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
// Copyright (c) 2023 IBM Corp.
|
||||||
|
// All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package stateless
|
||||||
|
|
||||||
|
import (
|
||||||
|
G "github.com/IBM/fp-go/iterator/stateless/generic"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Any returns `true` if any element of the iterable is `true`. If the iterable is empty, return `false`
|
||||||
|
// Similar to the [https://docs.python.org/3/library/functions.html#any] function
|
||||||
|
func Any[U any](pred func(U) bool) func(ma Iterator[U]) bool {
|
||||||
|
return G.Any[Iterator[U]](pred)
|
||||||
|
}
|
||||||
38
iterator/stateless/any_test.go
Normal file
38
iterator/stateless/any_test.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
// Copyright (c) 2023 IBM Corp.
|
||||||
|
// All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package stateless
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
A "github.com/IBM/fp-go/array"
|
||||||
|
F "github.com/IBM/fp-go/function"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAny(t *testing.T) {
|
||||||
|
|
||||||
|
anyBool := Any(F.Identity[bool])
|
||||||
|
|
||||||
|
i1 := FromArray(A.From(false, true, false))
|
||||||
|
assert.True(t, anyBool(i1))
|
||||||
|
|
||||||
|
i2 := FromArray(A.From(false, false, false))
|
||||||
|
assert.False(t, anyBool(i2))
|
||||||
|
|
||||||
|
i3 := Empty[bool]()
|
||||||
|
assert.False(t, anyBool(i3))
|
||||||
|
}
|
||||||
55
iterator/stateless/example_test.go
Normal file
55
iterator/stateless/example_test.go
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
// Copyright (c) 2023 IBM Corp.
|
||||||
|
// All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package stateless
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
A "github.com/IBM/fp-go/array"
|
||||||
|
F "github.com/IBM/fp-go/function"
|
||||||
|
O "github.com/IBM/fp-go/option"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Example_any() {
|
||||||
|
// `Any` function that simply returns the boolean identity
|
||||||
|
anyBool := Any(F.Identity[bool])
|
||||||
|
|
||||||
|
fmt.Println(anyBool(FromArray(A.From(true, false, false))))
|
||||||
|
fmt.Println(anyBool(FromArray(A.From(false, false, false))))
|
||||||
|
fmt.Println(anyBool(Empty[bool]()))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
|
||||||
|
func Example_next() {
|
||||||
|
|
||||||
|
seq := MakeBy(F.Identity[int])
|
||||||
|
|
||||||
|
first := seq()
|
||||||
|
|
||||||
|
value := F.Pipe1(
|
||||||
|
first,
|
||||||
|
O.Map(Current[int]),
|
||||||
|
)
|
||||||
|
|
||||||
|
fmt.Println(value)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// Some[int](0)
|
||||||
|
}
|
||||||
31
iterator/stateless/generic/any.go
Normal file
31
iterator/stateless/generic/any.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
// Copyright (c) 2023 IBM Corp.
|
||||||
|
// All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package generic
|
||||||
|
|
||||||
|
import (
|
||||||
|
F "github.com/IBM/fp-go/function"
|
||||||
|
O "github.com/IBM/fp-go/option"
|
||||||
|
T "github.com/IBM/fp-go/tuple"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Any returns `true` if any element of the iterable is `true`. If the iterable is empty, return `false`
|
||||||
|
func Any[GU ~func() O.Option[T.Tuple2[GU, U]], FCT ~func(U) bool, U any](pred FCT) func(ma GU) bool {
|
||||||
|
return F.Flow3(
|
||||||
|
Filter[GU](pred),
|
||||||
|
First[GU],
|
||||||
|
O.Fold(F.ConstFalse, F.Constant1[U](true)),
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -41,7 +41,7 @@ func DropWhile[GU ~func() O.Option[T.Tuple2[GU, U]], U any](pred func(U) bool) f
|
|||||||
return F.Pipe2(
|
return F.Pipe2(
|
||||||
t,
|
t,
|
||||||
fromPred,
|
fromPred,
|
||||||
O.Fold(recurse(t.F1), O.Of[T.Tuple2[GU, U]]),
|
O.Fold(recurse(Next(t)), O.Of[T.Tuple2[GU, U]]),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,16 @@ import (
|
|||||||
T "github.com/IBM/fp-go/tuple"
|
T "github.com/IBM/fp-go/tuple"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Next returns the iterator for the next element in an iterator `T.Tuple2`
|
||||||
|
func Next[GU ~func() O.Option[T.Tuple2[GU, U]], U any](m T.Tuple2[GU, U]) GU {
|
||||||
|
return T.First(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Current returns the current element in an iterator `T.Tuple2`
|
||||||
|
func Current[GU ~func() O.Option[T.Tuple2[GU, U]], U any](m T.Tuple2[GU, U]) U {
|
||||||
|
return T.Second(m)
|
||||||
|
}
|
||||||
|
|
||||||
// From constructs an array from a set of variadic arguments
|
// From constructs an array from a set of variadic arguments
|
||||||
func From[GU ~func() O.Option[T.Tuple2[GU, U]], U any](data ...U) GU {
|
func From[GU ~func() O.Option[T.Tuple2[GU, U]], U any](data ...U) GU {
|
||||||
return FromArray[GU](data)
|
return FromArray[GU](data)
|
||||||
@@ -56,8 +66,8 @@ func reduce[GU ~func() O.Option[T.Tuple2[GU, U]], U, V any](as GU, f func(V, U)
|
|||||||
current := initial
|
current := initial
|
||||||
for ok {
|
for ok {
|
||||||
// next (with bad side effect)
|
// next (with bad side effect)
|
||||||
current = f(current, next.F2)
|
current = f(current, Current(next))
|
||||||
next, ok = O.Unwrap(next.F1())
|
next, ok = O.Unwrap(Next(next)())
|
||||||
}
|
}
|
||||||
return current
|
return current
|
||||||
}
|
}
|
||||||
@@ -199,8 +209,8 @@ func FilterMap[GV ~func() O.Option[T.Tuple2[GV, V]], GU ~func() O.Option[T.Tuple
|
|||||||
m = O.Fold(
|
m = O.Fold(
|
||||||
Empty[GV](),
|
Empty[GV](),
|
||||||
func(t T.Tuple2[GU, U]) O.Option[T.Tuple2[GV, V]] {
|
func(t T.Tuple2[GU, U]) O.Option[T.Tuple2[GV, V]] {
|
||||||
r := recurse(t.F1)
|
r := recurse(Next(t))
|
||||||
return O.MonadFold(f(t.F2), r, F.Flow2(
|
return O.MonadFold(f(Current(t)), r, F.Flow2(
|
||||||
F.Bind1st(T.MakeTuple2[GV, V], r),
|
F.Bind1st(T.MakeTuple2[GV, V], r),
|
||||||
O.Some[T.Tuple2[GV, V]],
|
O.Some[T.Tuple2[GV, V]],
|
||||||
))
|
))
|
||||||
|
|||||||
@@ -26,6 +26,16 @@ import (
|
|||||||
// Iterator represents a stateless, pure way to iterate over a sequence
|
// Iterator represents a stateless, pure way to iterate over a sequence
|
||||||
type Iterator[U any] L.Lazy[O.Option[T.Tuple2[Iterator[U], U]]]
|
type Iterator[U any] L.Lazy[O.Option[T.Tuple2[Iterator[U], U]]]
|
||||||
|
|
||||||
|
// Next returns the [Iterator] for the next element in an iterator `T.Tuple2`
|
||||||
|
func Next[U any](m T.Tuple2[Iterator[U], U]) Iterator[U] {
|
||||||
|
return G.Next(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Current returns the current element in an [Iterator] `T.Tuple2`
|
||||||
|
func Current[U any](m T.Tuple2[Iterator[U], U]) U {
|
||||||
|
return G.Current(m)
|
||||||
|
}
|
||||||
|
|
||||||
// Empty returns the empty iterator
|
// Empty returns the empty iterator
|
||||||
func Empty[U any]() Iterator[U] {
|
func Empty[U any]() Iterator[U] {
|
||||||
return G.Empty[Iterator[U]]()
|
return G.Empty[Iterator[U]]()
|
||||||
|
|||||||
2
test.bat
2
test.bat
@@ -1,3 +1,3 @@
|
|||||||
@echo off
|
@echo off
|
||||||
mkdir build 2> NUL
|
mkdir build 2> NUL
|
||||||
gotip test .\... && go1.20.5 test .\... && go test -v -coverprofile build/cover.out -coverpkg=./... -covermode=atomic .\...
|
gotip test .\... && go1.20.11 test .\... && go test -v -coverprofile build/cover.out -coverpkg=./... -covermode=atomic .\...
|
||||||
Reference in New Issue
Block a user