Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expand filterview() docs, add to README #43

Merged
merged 1 commit into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ for the `group` family of functions.
# API reference

The package currently implements and exports `only`, `splitdims`, `splitdimsview`,
`combinedims`, `combinedimsview`, `mapmany`, `flatten`, `group`, `groupinds`, `groupview`,
`combinedims`, `combinedimsview`, `mapview`, `filterview`, `mapmany`, `flatten`, `group`, `groupinds`, `groupview`,
`groupreduce`, `innerjoin` and `leftgroupjoin`, as well as the `@_` macro. Expect this list
to grow.

Expand Down Expand Up @@ -242,6 +242,34 @@ julia> b
false
```

### `filterview(f, arr)`

Like `filter`, but presents a view of the data contained in `arr`. Data in `arr` is not copied, and modifications to the resulting view are propagated to the original array.

#### Example:

```julia
julia> a = [1, 2, 3];

julia> b = filterview(isodd, a)
2-element view(::Vector{Int64}, [1, 3]) with eltype Int64:
1
3

julia> b[2] = 10;

julia> a
3-element Vector{Int64}:
1
2
10

julia> b
2-element view(::Vector{Int64}, [1, 3]) with eltype Int64:
1
10
```

### `mapmany(f, iters...)`

Like `map`, but `f(x...)` for each `x ∈ zip(iters...)` may return an arbitrary number of
Expand Down
25 changes: 25 additions & 0 deletions src/map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,30 @@ mapview(::typeof(identity), d::AbstractDictionary) = d
filterview(f, a)

Return a view of an array `a` without elements for which `f` is `false`. Similar to `filter(f, a)`, except returns a view instead of a copy.
Filtered indices are computed once, and are not updated when the array gets modified.

# Example

```
julia> a = [1, 2, 3];

julia> b = filterview(isodd, a)
2-element view(::Vector{Int64}, [1, 3]) with eltype Int64:
1
3

julia> b[2] = 10;

julia> a
3-element Vector{Int64}:
1
2
10

julia> b
2-element view(::Vector{Int64}, [1, 3]) with eltype Int64:
1
10
```
"""
filterview(f, a::AbstractArray) = @view a[findall(f, a)]