jar1
April 5, 2023, 8:54pm
1
Is there a function somewhere like this?
nonempty_prefixes(xs) = [@view xs[firstindex(xs):i] for i in eachindex(xs)]
julia> xs = String('a':'z')
"abcdefghijklmnopqrstuvwxyz"
julia> nonempty_prefixes(xs)
26-element Vector{SubString{String}}:
"a"
"ab"
"abc"
"abcd"
"abcde"
"abcdef"
"abcdefg"
⋮
"abcdefghijklmnopqrstu"
"abcdefghijklmnopqrstuv"
"abcdefghijklmnopqrstuvw"
"abcdefghijklmnopqrstuvwx"
"abcdefghijklmnopqrstuvwxy"
"abcdefghijklmnopqrstuvwxyz"
I am probably not understanding the question but maybe first
?
julia> xs = String('a':'z')
"abcdefghijklmnopqrstuvwxyz"
julia> first(xs,8)
"abcdefgh"
jar1
April 5, 2023, 9:10pm
3
I’m just looking for exactly the function I wrote, but if it’s already in some package or Base.
@jar1 , there is SubString
, but if broadcasted as below, it is not more efficient than your code:
SubString.(xs, firstindex(xs), eachindex(xs))
If written as a comprehension it is equivalent:
[SubString(xs, firstindex(xs), i) for i in eachindex(xs)]
2 Likes
view.(xs,(:).(1,1:length(xs))
jar1
April 5, 2023, 10:10pm
7
view.(fill(xs), range.(firstindex(xs), eachindex(xs)))
works on Strings and Vectors
jar1:
fill(xs)
Why fill()
and not Ref()
?
1 Like
Probably not, but why would you need one? Your 1-line function is pretty compact and clear and about as efficient as such a thing can get.
(Attempting to use broadcasting here will probably result in additional allocations, because broadcasting will call collect
on eachindex
under the hood.)
5 Likes
sijo
April 6, 2023, 7:12am
11
Also you might want to return a generator instead of an array:
prefixes(xs) = ((@view xs[begin:i]) for i in eachindex(xs))
(You can always collect
the result when you actually need an array.)
Side question: I was surprised I had to add inner parentheses for the view in the above code. Consider this:
# This works:
julia> [@view a[1:2] for a in [[1,2]]]
# This doesn't:
julia> (@view a[1:2] for a in [[1,2]])
ERROR: syntax: unexpected ")"
Is there a reason for that or is it a bug?
2 Likes
DNF
April 6, 2023, 9:37am
12
That seems a bit inconsistent to me, too. Note also that you can use parens for macro calls. Imo, @view(a[1:2])
is nicer than (@view a[1:2])
.
1 Like
In this specific case, I prefer:
view(a,1:2)
1 Like
DNF
April 6, 2023, 1:13pm
14
But in combination with begin
or end
, the macro form is needed.
1 Like
sijo
April 11, 2023, 8:21am
16
1 Like