Skip to content

Commit

Permalink
Filter_with_index, changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Jan 22, 2022
1 parent 7994705 commit 2a74cf6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
2 changes: 2 additions & 0 deletions app/gui/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

- [Implemented `Vector.distinct` allowing to remove duplicate elements from a
Vector][3224]
- [Improved performance of `Vector.filter` and `Vector.each`; implemented `Vector.filter_with_index`. Made `Vector.at` accept negative indices and ensured it fails with a dataflow error on out of bounds access instead of an internal Java exception.][3232]

[3153]: https://github.com/enso-org/enso/pull/3153
[3166]: https://github.com/enso-org/enso/pull/3166
Expand All @@ -25,6 +26,7 @@
[3193]: https://github.com/enso-org/enso/pull/3193
[3208]: https://github.com/enso-org/enso/pull/3208
[3224]: https://github.com/enso-org/enso/pull/3224
[3232]: https://github.com/enso-org/enso/pull/3232

# Enso 2.0.0-alpha.18 (2021-10-12)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,30 @@ type Vector
case acc of
_ -> builder.to_vector

## Selects all elements of this vector which satisfy a predicate.

Arguments:
- predicate: A function that takes an index and a list element and
returns a boolean value that says whether that value should be included
in the result.

> Example
Selecting all elements which are equal to their position in the vector.

[0, 10, 2, 2].filter (==) == [0, 2]
filter_with_index : (Integer -> Any -> Boolean) -> Vector Any
filter_with_index predicate =
builder = here.new_builder
acc = this.fold 0 ix-> elem->
## The accumulator is threaded through to preserve dataflow
dependencies which are cut by using the stateful builder. This to
ensure correct dataflow error propagation.
acc = if predicate ix elem then builder.append elem
case acc of
_ -> ix+1
case acc of
_ -> builder.to_vector

## Applies a function to each element of the vector, returning the vector of
results.

Expand Down
13 changes: 9 additions & 4 deletions test/Tests/src/Data/Vector_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,15 @@ spec = Test.group "Vectors" <|

Test.specify "should filter elements" <|
vec = [1, 2, 3, 4, 5]
vec.filter (ix -> ix > 3) . should_equal [4, 5]
vec.filter (ix -> ix == 1) . should_equal [1]
vec.filter (ix -> ix < 0) . should_equal []
vec.filter (ix -> if ix == 2 then Error.throw <| My_Error "foo" else True) . should_fail_with My_Error
vec.filter (x -> x > 3) . should_equal [4, 5]
vec.filter (x -> x == 1) . should_equal [1]
vec.filter (x -> x < 0) . should_equal []
vec.filter (x -> if x == 2 then Error.throw <| My_Error "foo" else True) . should_fail_with My_Error

Test.specify "should filter elements with indices" <|
[0, 10, 2, 2].filter_with_index (==) . should_equal [0, 2]
([1, 2, 3, 4].filter_with_index ix-> _-> ix < 2) . should_equal [1, 2]
([1, 2, 3, 4].filter_with_index ix-> _-> if ix == 1 then Error.throw <| My_Error "foo" else True) . should_fail_with My_Error

Test.specify "should allow mapping an operation, returning a new vector" <|
vec = [1, 2, 3, 4]
Expand Down

0 comments on commit 2a74cf6

Please sign in to comment.