mirror of
https://github.com/IBM/fp-go.git
synced 2025-07-17 01:32:23 +02:00
fix: more iterator functions
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
27
iterator/stateless/compress.go
Normal file
27
iterator/stateless/compress.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
// 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"
|
||||||
|
T "github.com/IBM/fp-go/tuple"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Compress returns an [Iterator] that filters elements from a data [Iterator] returning only those that have a corresponding element in selector [Iterator] that evaluates to `true`.
|
||||||
|
// Stops when either the data or selectors iterator has been exhausted.
|
||||||
|
func Compress[U any](sel Iterator[bool]) func(Iterator[U]) Iterator[U] {
|
||||||
|
return G.Compress[Iterator[U], Iterator[bool], Iterator[T.Tuple2[U, bool]]](sel)
|
||||||
|
}
|
35
iterator/stateless/compress_test.go
Normal file
35
iterator/stateless/compress_test.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
// 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"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCompress(t *testing.T) {
|
||||||
|
// sequence of 5 items
|
||||||
|
data := From(0, 1, 2, 3)
|
||||||
|
// select some of these items
|
||||||
|
selector := From(true, false, false, true)
|
||||||
|
// compressed result
|
||||||
|
compressed := Compress[int](selector)(data)
|
||||||
|
|
||||||
|
assert.Equal(t, A.From(0, 3), ToArray(compressed))
|
||||||
|
|
||||||
|
}
|
26
iterator/stateless/cycle.go
Normal file
26
iterator/stateless/cycle.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"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DropWhile creates an [Iterator] that drops elements from the [Iterator] as long as the predicate is true; afterwards, returns every element.
|
||||||
|
// Note, the [Iterator] does not produce any output until the predicate first becomes false
|
||||||
|
func Cycle[U any](ma Iterator[U]) Iterator[U] {
|
||||||
|
return G.Cycle[Iterator[U]](ma)
|
||||||
|
}
|
33
iterator/stateless/cycle_test.go
Normal file
33
iterator/stateless/cycle_test.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
// 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"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCycle(t *testing.T) {
|
||||||
|
// sequence of 5 items
|
||||||
|
items := Take[int](5)(Count(0))
|
||||||
|
// repeat
|
||||||
|
repeated := Take[int](17)(Cycle(items))
|
||||||
|
|
||||||
|
assert.Equal(t, A.From(0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1), ToArray(repeated))
|
||||||
|
|
||||||
|
}
|
26
iterator/stateless/dropwhile.go
Normal file
26
iterator/stateless/dropwhile.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"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DropWhile creates an [Iterator] that drops elements from the [Iterator] as long as the predicate is true; afterwards, returns every element.
|
||||||
|
// Note, the [Iterator] does not produce any output until the predicate first becomes false
|
||||||
|
func DropWhile[U any](pred func(U) bool) func(Iterator[U]) Iterator[U] {
|
||||||
|
return G.DropWhile[Iterator[U]](pred)
|
||||||
|
}
|
35
iterator/stateless/dropwhile_test.go
Normal file
35
iterator/stateless/dropwhile_test.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
// 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"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDropWhile(t *testing.T) {
|
||||||
|
// sequence of 5 items
|
||||||
|
data := Take[int](10)(Cycle(From(0, 1, 2, 3)))
|
||||||
|
|
||||||
|
total := DropWhile(func(data int) bool {
|
||||||
|
return data <= 2
|
||||||
|
})(data)
|
||||||
|
|
||||||
|
assert.Equal(t, A.From(3, 0, 1, 2, 3, 0, 1), ToArray(total))
|
||||||
|
|
||||||
|
}
|
34
iterator/stateless/generic/compress.go
Normal file
34
iterator/stateless/generic/compress.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
// 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"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Compress returns an [Iterator] that filters elements from a data [Iterator] returning only those that have a corresponding element in selector [Iterator] that evaluates to `true`.
|
||||||
|
// Stops when either the data or selectors iterator has been exhausted.
|
||||||
|
func Compress[GU ~func() O.Option[T.Tuple2[GU, U]], GB ~func() O.Option[T.Tuple2[GB, bool]], CS ~func() O.Option[T.Tuple2[CS, T.Tuple2[U, bool]]], U any](sel GB) func(GU) GU {
|
||||||
|
return F.Flow2(
|
||||||
|
Zip[GU, GB, CS](sel),
|
||||||
|
FilterMap[GU, CS](F.Flow2(
|
||||||
|
O.FromPredicate(T.Second[U, bool]),
|
||||||
|
O.Map(T.First[U, bool]),
|
||||||
|
)),
|
||||||
|
)
|
||||||
|
}
|
43
iterator/stateless/generic/cycle.go
Normal file
43
iterator/stateless/generic/cycle.go
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
// 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"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Cycle[GU ~func() O.Option[T.Tuple2[GU, U]], U any](ma GU) GU {
|
||||||
|
// avoid cyclic references
|
||||||
|
var m func(O.Option[T.Tuple2[GU, U]]) O.Option[T.Tuple2[GU, U]]
|
||||||
|
|
||||||
|
recurse := func(mu GU) GU {
|
||||||
|
return F.Nullary2(
|
||||||
|
mu,
|
||||||
|
m,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
m = O.Fold(func() O.Option[T.Tuple2[GU, U]] {
|
||||||
|
return recurse(ma)()
|
||||||
|
}, F.Flow2(
|
||||||
|
T.Map2(recurse, F.Identity[U]),
|
||||||
|
O.Of[T.Tuple2[GU, U]],
|
||||||
|
))
|
||||||
|
|
||||||
|
return recurse(ma)
|
||||||
|
}
|
49
iterator/stateless/generic/dropwhile.go
Normal file
49
iterator/stateless/generic/dropwhile.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
// 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"
|
||||||
|
P "github.com/IBM/fp-go/predicate"
|
||||||
|
T "github.com/IBM/fp-go/tuple"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DropWhile creates an [Iterator] that drops elements from the [Iterator] as long as the predicate is true; afterwards, returns every element.
|
||||||
|
// Note, the [Iterator] does not produce any output until the predicate first becomes false
|
||||||
|
func DropWhile[GU ~func() O.Option[T.Tuple2[GU, U]], U any](pred func(U) bool) func(GU) GU {
|
||||||
|
// avoid cyclic references
|
||||||
|
var m func(O.Option[T.Tuple2[GU, U]]) O.Option[T.Tuple2[GU, U]]
|
||||||
|
|
||||||
|
fromPred := O.FromPredicate(P.Not(P.ContraMap(T.Second[GU, U])(pred)))
|
||||||
|
|
||||||
|
recurse := func(mu GU) GU {
|
||||||
|
return F.Nullary2(
|
||||||
|
mu,
|
||||||
|
m,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
m = O.Chain(func(t T.Tuple2[GU, U]) O.Option[T.Tuple2[GU, U]] {
|
||||||
|
return F.Pipe2(
|
||||||
|
t,
|
||||||
|
fromPred,
|
||||||
|
O.Fold(recurse(t.F1), O.Of[T.Tuple2[GU, U]]),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
return recurse
|
||||||
|
}
|
@ -20,7 +20,7 @@ import (
|
|||||||
F "github.com/IBM/fp-go/function"
|
F "github.com/IBM/fp-go/function"
|
||||||
"github.com/IBM/fp-go/internal/utils"
|
"github.com/IBM/fp-go/internal/utils"
|
||||||
IO "github.com/IBM/fp-go/iooption/generic"
|
IO "github.com/IBM/fp-go/iooption/generic"
|
||||||
N "github.com/IBM/fp-go/number/integer"
|
N "github.com/IBM/fp-go/number"
|
||||||
O "github.com/IBM/fp-go/option"
|
O "github.com/IBM/fp-go/option"
|
||||||
T "github.com/IBM/fp-go/tuple"
|
T "github.com/IBM/fp-go/tuple"
|
||||||
)
|
)
|
||||||
@ -135,38 +135,49 @@ func Flatten[GV ~func() O.Option[T.Tuple2[GV, GU]], GU ~func() O.Option[T.Tuple2
|
|||||||
return MonadChain(ma, F.Identity[GU])
|
return MonadChain(ma, F.Identity[GU])
|
||||||
}
|
}
|
||||||
|
|
||||||
// MakeBy returns an [Iterator] with `n` elements initialized with `f(i)`
|
// MakeBy returns an [Iterator] with an infinite number of elements initialized with `f(i)`
|
||||||
func MakeBy[GU ~func() O.Option[T.Tuple2[GU, U]], FCT ~func(int) U, U any](n int, f FCT) GU {
|
func MakeBy[GU ~func() O.Option[T.Tuple2[GU, U]], FCT ~func(int) U, U any](f FCT) GU {
|
||||||
|
|
||||||
var m func(int) O.Option[T.Tuple2[GU, U]]
|
var m func(int) O.Option[T.Tuple2[GU, U]]
|
||||||
|
|
||||||
recurse := func(i int) GU {
|
recurse := func(i int) GU {
|
||||||
return func() O.Option[T.Tuple2[GU, U]] {
|
return F.Nullary2(
|
||||||
return F.Pipe1(
|
F.Constant(i),
|
||||||
i,
|
m,
|
||||||
m,
|
)
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m = F.Flow2(
|
m = F.Flow3(
|
||||||
O.FromPredicate(N.Between(0, n)),
|
T.Replicate2[int],
|
||||||
O.Map(F.Flow2(
|
T.Map2(F.Flow2(
|
||||||
T.Replicate2[int],
|
utils.Inc,
|
||||||
T.Map2(F.Flow2(
|
recurse),
|
||||||
utils.Inc,
|
f),
|
||||||
recurse),
|
O.Of[T.Tuple2[GU, U]],
|
||||||
f),
|
|
||||||
)),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// bootstrap
|
// bootstrap
|
||||||
return recurse(0)
|
return recurse(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replicate creates an [Iterator] containing a value repeated the specified number of times.
|
// Replicate creates an infinite [Iterator] containing a value.
|
||||||
func Replicate[GU ~func() O.Option[T.Tuple2[GU, U]], U any](n int, a U) GU {
|
func Replicate[GU ~func() O.Option[T.Tuple2[GU, U]], U any](a U) GU {
|
||||||
return MakeBy[GU](n, F.Constant1[int](a))
|
return MakeBy[GU](F.Constant1[int](a))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Repeat creates an [Iterator] containing a value repeated the specified number of times.
|
||||||
|
// Alias of [Replicate] combined with [Take]
|
||||||
|
func Repeat[GU ~func() O.Option[T.Tuple2[GU, U]], U any](n int, a U) GU {
|
||||||
|
return F.Pipe2(
|
||||||
|
a,
|
||||||
|
Replicate[GU],
|
||||||
|
Take[GU](n),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Count creates an [Iterator] containing a consecutive sequence of integers starting with the provided start value
|
||||||
|
func Count[GU ~func() O.Option[T.Tuple2[GU, int]]](start int) GU {
|
||||||
|
return MakeBy[GU](N.Add(start))
|
||||||
}
|
}
|
||||||
|
|
||||||
func FilterMap[GV ~func() O.Option[T.Tuple2[GV, V]], GU ~func() O.Option[T.Tuple2[GU, U]], FCT ~func(U) O.Option[V], U, V any](f FCT) func(ma GU) GV {
|
func FilterMap[GV ~func() O.Option[T.Tuple2[GV, V]], GU ~func() O.Option[T.Tuple2[GU, U]], FCT ~func(U) O.Option[V], U, V any](f FCT) func(ma GU) GV {
|
||||||
|
@ -21,19 +21,25 @@ import (
|
|||||||
T "github.com/IBM/fp-go/tuple"
|
T "github.com/IBM/fp-go/tuple"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func apTuple[A, B any](t T.Tuple2[func(A) B, A]) T.Tuple2[B, A] {
|
||||||
|
return T.MakeTuple2(t.F1(t.F2), t.F2)
|
||||||
|
}
|
||||||
|
|
||||||
func Scan[GV ~func() O.Option[T.Tuple2[GV, V]], GU ~func() O.Option[T.Tuple2[GU, U]], FCT ~func(V, U) V, U, V any](f FCT, initial V) func(ma GU) GV {
|
func Scan[GV ~func() O.Option[T.Tuple2[GV, V]], GU ~func() O.Option[T.Tuple2[GU, U]], FCT ~func(V, U) V, U, V any](f FCT, initial V) func(ma GU) GV {
|
||||||
// pre-declare to avoid cyclic reference
|
// pre-declare to avoid cyclic reference
|
||||||
var recurse func(ma GU, v V) GV
|
var m func(GU) func(V) GV
|
||||||
|
|
||||||
recurse = func(ma GU, current V) GV {
|
recurse := func(ma GU, current V) GV {
|
||||||
return F.Nullary2(
|
return F.Nullary2(
|
||||||
ma,
|
ma,
|
||||||
O.Map(func(t T.Tuple2[GU, U]) T.Tuple2[GV, V] {
|
O.Map(F.Flow2(
|
||||||
v := f(current, t.F2)
|
T.Map2(m, F.Bind1st(f, current)),
|
||||||
return T.MakeTuple2(recurse(t.F1, v), v)
|
apTuple[V, GV],
|
||||||
}),
|
)),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m = F.Curry2(recurse)
|
||||||
|
|
||||||
return F.Bind2nd(recurse, initial)
|
return F.Bind2nd(recurse, initial)
|
||||||
}
|
}
|
||||||
|
@ -29,18 +29,14 @@ func Take[GU ~func() O.Option[T.Tuple2[GU, U]], U any](n int) func(ma GU) GU {
|
|||||||
fromPred := O.FromPredicate(N.Between(0, n))
|
fromPred := O.FromPredicate(N.Between(0, n))
|
||||||
|
|
||||||
recurse = func(ma GU, idx int) GU {
|
recurse = func(ma GU, idx int) GU {
|
||||||
return func() O.Option[T.Tuple2[GU, U]] {
|
return F.Nullary3(
|
||||||
return F.Pipe2(
|
F.Constant(idx),
|
||||||
idx,
|
fromPred,
|
||||||
fromPred,
|
O.Chain(F.Ignore1of1[int](F.Nullary2(
|
||||||
O.Chain(F.Ignore1of1[int](F.Nullary2(
|
ma,
|
||||||
ma,
|
O.Map(T.Map2(F.Bind2nd(recurse, idx+1), F.Identity[U])),
|
||||||
O.Map(func(t T.Tuple2[GU, U]) T.Tuple2[GU, U] {
|
))),
|
||||||
return T.MakeTuple2(recurse(t.F1, idx+1), t.F2)
|
)
|
||||||
}),
|
|
||||||
))),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return F.Bind2nd(recurse, 0)
|
return F.Bind2nd(recurse, 0)
|
||||||
|
@ -78,14 +78,14 @@ func From[U any](data ...U) Iterator[U] {
|
|||||||
return G.From[Iterator[U]](data...)
|
return G.From[Iterator[U]](data...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MakeBy returns an [Iterator] with `n` elements initialized with `f(i)`
|
// MakeBy returns an [Iterator] with an infinite number of elements initialized with `f(i)`
|
||||||
func MakeBy[FCT ~func(int) U, U any](n int, f FCT) Iterator[U] {
|
func MakeBy[FCT ~func(int) U, U any](f FCT) Iterator[U] {
|
||||||
return G.MakeBy[Iterator[U]](n, f)
|
return G.MakeBy[Iterator[U]](f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replicate creates an [Iterator] containing a value repeated the specified number of times.
|
// Replicate creates an [Iterator] containing a value repeated an infinite number of times.
|
||||||
func Replicate[U any](n int, a U) Iterator[U] {
|
func Replicate[U any](a U) Iterator[U] {
|
||||||
return G.Replicate[Iterator[U]](n, a)
|
return G.Replicate[Iterator[U]](a)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FilterMap filters and transforms the content of an iterator
|
// FilterMap filters and transforms the content of an iterator
|
||||||
@ -107,3 +107,14 @@ func Ap[V, U any](ma Iterator[U]) func(Iterator[func(U) V]) Iterator[V] {
|
|||||||
func MonadAp[V, U any](fab Iterator[func(U) V], ma Iterator[U]) Iterator[V] {
|
func MonadAp[V, U any](fab Iterator[func(U) V], ma Iterator[U]) Iterator[V] {
|
||||||
return G.MonadAp[Iterator[func(U) V], Iterator[V]](fab, ma)
|
return G.MonadAp[Iterator[func(U) V], Iterator[V]](fab, ma)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Repeat creates an [Iterator] containing a value repeated the specified number of times.
|
||||||
|
// Alias of [Replicate]
|
||||||
|
func Repeat[U any](n int, a U) Iterator[U] {
|
||||||
|
return G.Repeat[Iterator[U]](n, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Count creates an [Iterator] containing a consecutive sequence of integers starting with the provided start value
|
||||||
|
func Count(start int) Iterator[int] {
|
||||||
|
return G.Count[Iterator[int]](start)
|
||||||
|
}
|
||||||
|
@ -76,8 +76,9 @@ func isPrimeNumber(num int) bool {
|
|||||||
|
|
||||||
func TestFilterMap(t *testing.T) {
|
func TestFilterMap(t *testing.T) {
|
||||||
|
|
||||||
it := F.Pipe2(
|
it := F.Pipe3(
|
||||||
MakeBy(100, utils.Inc),
|
MakeBy(utils.Inc),
|
||||||
|
Take[int](100),
|
||||||
FilterMap(O.FromPredicate(isPrimeNumber)),
|
FilterMap(O.FromPredicate(isPrimeNumber)),
|
||||||
ToArray[int],
|
ToArray[int],
|
||||||
)
|
)
|
||||||
|
@ -18,19 +18,20 @@ package stateless
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
A "github.com/IBM/fp-go/array"
|
||||||
F "github.com/IBM/fp-go/function"
|
F "github.com/IBM/fp-go/function"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTake(t *testing.T) {
|
func TestTake(t *testing.T) {
|
||||||
|
|
||||||
total := MakeBy(100, F.Identity[int])
|
total := MakeBy(F.Identity[int])
|
||||||
|
|
||||||
trimmed := F.Pipe1(
|
trimmed := F.Pipe1(
|
||||||
total,
|
total,
|
||||||
Take[int](10),
|
Take[int](10),
|
||||||
)
|
)
|
||||||
|
|
||||||
assert.Equal(t, ToArray(MakeBy(10, F.Identity[int])), ToArray(trimmed))
|
assert.Equal(t, A.MakeBy(10, F.Identity[int]), ToArray(trimmed))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user