# Nonempty prefixes function

**URL:** <https://discourse.julialang.org/t/nonempty-prefixes-function/97136>\
**Category:** General Usage\
**Tags:** strings\
**Created:** [April 5, 2023, 8:54pm UTC](https://discourse.julialang.org/t/nonempty-prefixes-function/97136 "2023-04-05T20:54:25Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [April 5, 2023, 8:54pm UTC](https://discourse.julialang.org/t/nonempty-prefixes-function/97136/1 "2023-04-05T20:54:25Z")

</div>

Is there a function somewhere like this?

```julia
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"

```

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [April 5, 2023, 9:08pm UTC](https://discourse.julialang.org/t/nonempty-prefixes-function/97136/2 "2023-04-05T21:08:55Z")

</div>

I am probably not understanding the question but maybe `first`?

```julia
julia> xs = String('a':'z')
"abcdefghijklmnopqrstuvwxyz"

julia> first(xs,8)
"abcdefgh"

```

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [April 5, 2023, 9:10pm UTC](https://discourse.julialang.org/t/nonempty-prefixes-function/97136/3 "2023-04-05T21:10:47Z")

</div>

I’m just looking for exactly the function I wrote, but if it’s already in some package or Base.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [April 5, 2023, 9:27pm UTC](https://discourse.julialang.org/t/nonempty-prefixes-function/97136/4 "2023-04-05T21:27:55Z")

</div>

```julia
accumulate(*,xs)

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 5, 2023, 9:33pm UTC](https://discourse.julialang.org/t/nonempty-prefixes-function/97136/5 "2023-04-05T21:33:52Z")

</div>

@jar1, there is `SubString`, but if broadcasted as below, it is not more efficient than your code:

```julia
SubString.(xs, firstindex(xs), eachindex(xs))

```

If written as a comprehension it is equivalent:

```julia
[SubString(xs, firstindex(xs), i) for i in eachindex(xs)]

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [April 5, 2023, 10:02pm UTC](https://discourse.julialang.org/t/nonempty-prefixes-function/97136/6 "2023-04-05T22:02:55Z")

</div>

```julia
view.(xs,(:).(1,1:length(xs))

```

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [April 5, 2023, 10:10pm UTC](https://discourse.julialang.org/t/nonempty-prefixes-function/97136/7 "2023-04-05T22:10:50Z")

</div>

```julia
view.(fill(xs), range.(firstindex(xs), eachindex(xs)))

```

works on Strings and Vectors

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 5, 2023, 10:17pm UTC](https://discourse.julialang.org/t/nonempty-prefixes-function/97136/8 "2023-04-05T22:17:59Z")

</div>

> [@jar1](#):
>
> `fill(xs)`

Why `fill()` and not `Ref()`?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 5, 2023, 11:09pm UTC](https://discourse.julialang.org/t/nonempty-prefixes-function/97136/9 "2023-04-05T23:09:31Z")

</div>

> [@jar1](#):
>
> Is there a function somewhere like this?

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.)

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 6, 2023, 6:25am UTC](https://discourse.julialang.org/t/nonempty-prefixes-function/97136/10 "2023-04-06T06:25:59Z")

</div>

> [@jar1](#):
>
> `xs[firstindex(xs):i]`

`firstindex(xs)` =\> `begin`.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [April 6, 2023, 7:12am UTC](https://discourse.julialang.org/t/nonempty-prefixes-function/97136/11 "2023-04-06T07:12:23Z")

</div>

Also you might want to return a generator instead of an array:

```julia
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:

```julia
# 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?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 6, 2023, 9:37am UTC](https://discourse.julialang.org/t/nonempty-prefixes-function/97136/12 "2023-04-06T09:37:57Z")

</div>

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])`.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 6, 2023, 1:10pm UTC](https://discourse.julialang.org/t/nonempty-prefixes-function/97136/13 "2023-04-06T13:10:19Z")

</div>

In this specific case, I prefer:

```julia
view(a,1:2) 

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 6, 2023, 1:13pm UTC](https://discourse.julialang.org/t/nonempty-prefixes-function/97136/14 "2023-04-06T13:13:12Z")

</div>

But in combination with `begin` or `end`, the macro form is needed.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [April 11, 2023, 8:21am UTC](https://discourse.julialang.org/t/nonempty-prefixes-function/97136/16 "2023-04-11T08:21:39Z")

</div>

It turns out this is a design trade-off, due to an ambiguity between generator “for loop” and `(a; b; c)` block syntax. See [parser bug: macro calls w/o parens in generator expressions · Issue #18650 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/18650) and [RFC: fix #18650, parsing generator expressions containing macro calls by JeffBezanson · Pull Request #22943 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/22943).
