FlexiMaps.filtermap
— Functionfiltermap(f, X)
Map and filter a collection in one go. Most useful when the mapped function shares some computations with the filter predicate.
Returns same as map(f, X)
, dropping elements where f(x)
is nothing
. Return Some(nothing)
from f
to keep nothing
in the result.
FlexiMaps.flatmap
— Functionflatmap(f, X)
Apply f
to all elements of X
and flatten the result by concatenating all f(x)
collections. Similar to mapreduce(f, vcat, X)
, more efficient and generic.
flatmap(fₒᵤₜ, fᵢₙ, X)
Apply fₒᵤₜ
to all elements of X
, and apply fᵢₙ
to the results. Basically, [fᵢₙ(x, y) for x in X for y in fₒᵤₜ(x)]
.
FlexiMaps.flatmap_parent
— Methodflatmap_parent(f, A)
Returns a collection consisting of all elements of each f(a)
, similar to flatmap(f, A)
.
Each element of A
should be a view
of the same parent array, and these views together should cover all elements of the parent. Elements of the result are in the same order as the corresponding elements in the parent array of a
s.
Examples
julia> a = [10, 20, 30, 40, 50]
julia> avs = [view(a, [2, 5]), view(a, [3, 1, 4])]
2-element Vector{SubArray{Int64, 1, Vector{Int64}, Tuple{Vector{Int64}}, false}}:
[20, 50]
[30, 10, 40]
# multiply by 2 and reassemble in the original order
julia> flatmap_parent(av -> 2 .* av, avs)
5-element Vector{Int64}:
20
40
60
80
100
FlexiMaps.flatten
— Methodflatten(X)
Flatten a collection of collections by concatenating all elements.
Similar to reduce(vcat, X)
, but more generic and type-stable.
FlexiMaps.mapinsert
— Methodmapinsert(X; prop=f, ...)
Insert x.prop
with the value f(x)
into all x
elements of X
. Common usage is for adding table columns.
Equivalent to map(x -> @insert(x.prop = f(x)), X)
, but supports multiple properties.
When possible (eg X isa StructArray
): uses an optimized approach, keeping all other component untouched.
FlexiMaps.mapinsertview
— Methodmapinsertview(X; prop=f, ...)
Like mapinsert
, but returns a view instead of a copy.
When possible (eg X isa StructArray
): uses an optimized approach, keeping all other component untouched.
FlexiMaps.mapinsert⁻
— Method mapinsert⁻(X; prop=f, ...)
Insert x.prop
with the value f(x)
into all x
elements of X
, and remove the topmost part of x
that is accessed by f(x)
(f
should be an Accessors
optic). Common usage is for modifying table columns.
Similar to mapinsert(X; prop=f, ...)
, but also removes a part of x
.
FlexiMaps.maprange
— Functionmaprange(f, start; stop, length)
maprange(f; start, stop, length)
maprange(f, start, stop; length)
length
values between start
and stop
, so that f(x)
is incremented in uniform steps. Uses mapview
in order not to materialize the array.
maprange(identity, ...)
is equivalent to range(...)
. Most common application - log-spaced ranges:
maprange(log, 10, 1000, length=5) ≈ [10, 31.6227766, 100, 316.227766, 1000]
Other transformations can also be useful:
maprange(sqrt, 16, 1024, length=5) == [16, 121, 324, 625, 1024]
FlexiMaps.mapset
— Methodmapset(X; prop=f, ...)
Set values of x.prop
to f(x)
for all x
elements of X
. Common usage is for modifying table columns.
Equivalent to map(x -> @set(x.prop = f(x)), X)
, but supports multiple properties.
When possible (eg X isa StructArray
): uses an optimized approach, keeping all other component untouched.
FlexiMaps.mapsetview
— Methodmapsetview(X; prop=f, ...)
Like mapset
, but returns a view instead of a copy.
When possible (eg X isa StructArray
): uses an optimized approach, keeping all other component untouched.
FlexiMaps.mapview
— Methodmapview(f, X)
Like map(f, X)
but doesn't materialize the result and returns a view.
Works on arrays, dicts, and arbitrary iterables. Passes length
, keys
and others directly to the parent. Does its best to determine the resulting eltype
without evaluating f
. Supports both getting and setting values (through Accessors.jl
).