# Standard function name to skip intermediate step piping getindex call into vec

**URL:** <https://discourse.julialang.org/t/standard-function-name-to-skip-intermediate-step-piping-getindex-call-into-vec/135088>\
**Category:** General Usage\
**Created:** [January 15, 2026, 10:08pm UTC](https://discourse.julialang.org/t/standard-function-name-to-skip-intermediate-step-piping-getindex-call-into-vec/135088 "2026-01-15T22:08:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![croberts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/croberts/32/9465_2.png) [@croberts](https://discourse.julialang.org/u/croberts)\
**Post date:** [January 15, 2026, 10:08pm UTC](https://discourse.julialang.org/t/standard-function-name-to-skip-intermediate-step-piping-getindex-call-into-vec/135088/1 "2026-01-15T22:08:30Z")

</div>

I am wondering about Julia conventions - is there a function or kwarg for skipping an intermediate type when taking an array through getindex and then vec?

More concretely, suppose we are doing a getindex call on a SparseMatrixCSC and convert the result into a vector:

```julia-auto
using SparseArrays 
sa = sparse([1 0 1; 0 1 0]) 
x = vec(sa[1:2, 1:2]) 

```

What is returned by getindex is a SparseMatrixCSC:

```julia-auto
typeof(sa[1:2, 1:2]) <: SparseMatrixCSC 

```

I want to skip the intermediate allocation to a SparseMatrixCSC, but I want to do it in a way that accords with Julia conventions. Note that getindex(args…; kwargs…) does not allow using kwargs with the bracket syntactic sugar.

The reason why I am asking is because I have defined my own array type such that it would be computationally cheaper if I could skip the intermediate getindex type. I would like something like:

```julia-auto
typeof(my_arr) <: MyArrayType
typeof(getindex_as_vec(my_arr, 1:2, 1:2)) <: Vector 

```

But I don’t want to write the function name getindex\_as\_vec if there is some convention I should be adhering to.

---

<div class="post-metadata">

**Author:** ![croberts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/croberts/32/9465_2.png) [@croberts](https://discourse.julialang.org/u/croberts)\
**Post date:** [January 15, 2026, 10:09pm UTC](https://discourse.julialang.org/t/standard-function-name-to-skip-intermediate-step-piping-getindex-call-into-vec/135088/2 "2026-01-15T22:09:50Z")

</div>

I think I am an idiot. I think I merely need to use views. e.g. vec(view(myarra, 1:2, 1:2))

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/224680_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [January 16, 2026, 1:13am UTC](https://discourse.julialang.org/t/standard-function-name-to-skip-intermediate-step-piping-getindex-call-into-vec/135088/3 "2026-01-16T01:13:52Z")

</div>

You probably want to be careful with views of sparse arrays, they don’t have great performance. I’d compare both options, depending on the setting either one may be faster

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [January 16, 2026, 8:06pm UTC](https://discourse.julialang.org/t/standard-function-name-to-skip-intermediate-step-piping-getindex-call-into-vec/135088/4 "2026-01-16T20:06:03Z")

</div>

Here is a solution using `CartesianIndices` and broadcasting:

```julia-auto
using SparseArrays
A = sparse([1 0 1; 0 1 0 ; 0 1 1])
Is = CartesianIndices(A)
@show getindex.(Ref(A), Is[1:2, 1:2])

```

However looking up a lot of indices in a sparse matrix is not the fastest thing to do. Maybe if you describe your situation a bit more, we could see whether there is a faster approach?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [January 16, 2026, 8:23pm UTC](https://discourse.julialang.org/t/standard-function-name-to-skip-intermediate-step-piping-getindex-call-into-vec/135088/5 "2026-01-16T20:23:08Z")

</div>

With Sparse arrays, it can sometimes be advantageous to create a copy. You could try the `vec∘view`, or you could try explicitly collecting it into a new `SparseVector`. Depending upon your subsequent usage patterns, this may or may not be a win.
