-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from IBM/cleue-add-some-more-itertools
fix: more iterator functions
- Loading branch information
Showing
15 changed files
with
383 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]), | ||
)), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.