Skip to content

Commit

Permalink
Merge pull request #18 from IBM/cleue-add-some-more-itertools
Browse files Browse the repository at this point in the history
fix: more iterator functions
  • Loading branch information
CarstenLeue committed Aug 4, 2023
2 parents 2cd3587 + e53e2c5 commit 74fd0c9
Show file tree
Hide file tree
Showing 15 changed files with 383 additions and 49 deletions.
27 changes: 27 additions & 0 deletions iterator/stateless/compress.go
Original file line number Diff line number Diff line change
@@ -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 changes: 35 additions & 0 deletions iterator/stateless/compress_test.go
Original file line number Diff line number Diff line change
@@ -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 changes: 26 additions & 0 deletions iterator/stateless/cycle.go
Original file line number Diff line number Diff line change
@@ -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 changes: 33 additions & 0 deletions iterator/stateless/cycle_test.go
Original file line number Diff line number Diff line change
@@ -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 changes: 26 additions & 0 deletions iterator/stateless/dropwhile.go
Original file line number Diff line number Diff line change
@@ -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 changes: 35 additions & 0 deletions iterator/stateless/dropwhile_test.go
Original file line number Diff line number Diff line change
@@ -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 changes: 34 additions & 0 deletions iterator/stateless/generic/compress.go
Original file line number Diff line number Diff line change
@@ -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 changes: 43 additions & 0 deletions iterator/stateless/generic/cycle.go
Original file line number Diff line number Diff line change
@@ -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 changes: 49 additions & 0 deletions iterator/stateless/generic/dropwhile.go
Original file line number Diff line number Diff line change
@@ -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
}
53 changes: 32 additions & 21 deletions iterator/stateless/generic/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
F "github.com/IBM/fp-go/function"
"github.com/IBM/fp-go/internal/utils"
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"
T "github.com/IBM/fp-go/tuple"
)
Expand Down Expand Up @@ -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])
}

// MakeBy returns an [Iterator] with `n` 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 {
// 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](f FCT) GU {

var m func(int) O.Option[T.Tuple2[GU, U]]

recurse := func(i int) GU {
return func() O.Option[T.Tuple2[GU, U]] {
return F.Pipe1(
i,
m,
)
}
return F.Nullary2(
F.Constant(i),
m,
)
}

m = F.Flow2(
O.FromPredicate(N.Between(0, n)),
O.Map(F.Flow2(
T.Replicate2[int],
T.Map2(F.Flow2(
utils.Inc,
recurse),
f),
)),
m = F.Flow3(
T.Replicate2[int],
T.Map2(F.Flow2(
utils.Inc,
recurse),
f),
O.Of[T.Tuple2[GU, U]],
)

// bootstrap
return recurse(0)
}

// Replicate creates an [Iterator] containing a value repeated the specified number of times.
func Replicate[GU ~func() O.Option[T.Tuple2[GU, U]], U any](n int, a U) GU {
return MakeBy[GU](n, F.Constant1[int](a))
// Replicate creates an infinite [Iterator] containing a value.
func Replicate[GU ~func() O.Option[T.Tuple2[GU, U]], U any](a U) GU {
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 {
Expand Down
Loading

0 comments on commit 74fd0c9

Please sign in to comment.