# Ask user to provide points coordinates as two vectors or as a single vector of tuples?

**URL:** <https://discourse.julialang.org/t/ask-user-to-provide-points-coordinates-as-two-vectors-or-as-a-single-vector-of-tuples/117717>\
**Category:** Optimization (Mathematical)\
**Tags:** input-output, best-practices, convention, coordinates\
**Created:** [August 1, 2024, 6:41am UTC](https://discourse.julialang.org/t/ask-user-to-provide-points-coordinates-as-two-vectors-or-as-a-single-vector-of-tuples/117717 "2024-08-01T06:41:22Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![K5Julien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/k5julien/32/210626_2.png) [@K5Julien](https://discourse.julialang.org/u/K5Julien)\
**Post date:** [August 1, 2024, 6:41am UTC](https://discourse.julialang.org/t/ask-user-to-provide-points-coordinates-as-two-vectors-or-as-a-single-vector-of-tuples/117717/1 "2024-08-01T06:41:22Z")

</div>

When asking the user to provide coordinates of plan points for my package RingStarProblems.jl I initially thought the following was best:

```julia
xycoordinates = tuple.(1:10, rand(1:10, 10))
somefunction(xycoordinates)

```

However, looking at Concorde.jl, they do the following instead:

```julia
x = 1:10
y = rand(1:10, 10)
somefunction(x,y)

```

The major con I see in their choice is that the user could provide different sizes for `x` and `y` that’d lead to errors.

What is the best programming practice? Note that perhaps this is not entirely related to Julia but programming in general.

---

<div class="post-metadata">

**Author:** ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)\
**Post date:** [August 1, 2024, 6:49am UTC](https://discourse.julialang.org/t/ask-user-to-provide-points-coordinates-as-two-vectors-or-as-a-single-vector-of-tuples/117717/2 "2024-08-01T06:49:18Z")

</div>

I would suggest that you implement it in whichever form is best (performant, simple, whatever) for the package and then just provide a few different methods

```julia
somefunction(coords)
somefunction(xs, ys)

```

If performance is important perhaps document which is the most performant method.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [August 2, 2024, 12:38am UTC](https://discourse.julialang.org/t/ask-user-to-provide-points-coordinates-as-two-vectors-or-as-a-single-vector-of-tuples/117717/3 "2024-08-02T00:38:41Z")

</div>

> [@screw\_dog](#):
>
> then just provide a few different methods

I would give the opposite advice. Just pick one, and make that the only interface. People that want to use or integrate your package into a wider piece of software can handle the translation.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [August 2, 2024, 1:13am UTC](https://discourse.julialang.org/t/ask-user-to-provide-points-coordinates-as-two-vectors-or-as-a-single-vector-of-tuples/117717/4 "2024-08-02T01:13:51Z")

</div>

The most natural here is to take an `AbstractVector` of tuples (or svectors). Then, if your algorithm is natively working with a vector of tuples – then use it as-is. If it’s most efficient with two separate vectors – convert all abstractvectors to `StructArray`.

This way, both you and users only care about one input “type”, limiting potential confusion.

---

<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:** [August 2, 2024, 5:53am UTC](https://discourse.julialang.org/t/ask-user-to-provide-points-coordinates-as-two-vectors-or-as-a-single-vector-of-tuples/117717/5 "2024-08-02T05:53:59Z")

</div>

A vector of tuples defines a set of points, two separate vectors defines a 2D grid.

If a function requires two separate vector arguments, my intuition tells me I’m defining a grid.

---

<div class="post-metadata">

**Author:** ![K5Julien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/k5julien/32/210626_2.png) [@K5Julien](https://discourse.julialang.org/u/K5Julien)\
**Post date:** [August 2, 2024, 7:27am UTC](https://discourse.julialang.org/t/ask-user-to-provide-points-coordinates-as-two-vectors-or-as-a-single-vector-of-tuples/117717/6 "2024-08-02T07:27:56Z")

</div>

Thank you to all of you for your relevant remarks! Does that mean one could provide a PR to improve Concorde.jl on that point? I believe they did not mean to provide a 2D-Grid but points instead.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [August 2, 2024, 7:40am UTC](https://discourse.julialang.org/t/ask-user-to-provide-points-coordinates-as-two-vectors-or-as-a-single-vector-of-tuples/117717/7 "2024-08-02T07:40:07Z")

</div>

It wouldnt be an improvement. It would be a breaking change 😄

Just pick something and be consistent. There are arguments each way.

---

<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:** [August 2, 2024, 8:34am UTC](https://discourse.julialang.org/t/ask-user-to-provide-points-coordinates-as-two-vectors-or-as-a-single-vector-of-tuples/117717/8 "2024-08-02T08:34:48Z")

</div>

> [@odow](#):
>
> It would be a breaking change

Allowing vector-of-tuples might not be a breaking change. Disallowing multiple vectors would, definitely, though.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [August 2, 2024, 4:48pm UTC](https://discourse.julialang.org/t/ask-user-to-provide-points-coordinates-as-two-vectors-or-as-a-single-vector-of-tuples/117717/9 "2024-08-02T16:48:57Z")

</div>

I would go with two distinct vectors if it is reasonable to expect that the user will frequently have the data already in this format, and no unnecessary copy is needed. It is what is often done in mathematical programming.

---

<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:** [August 2, 2024, 5:44pm UTC](https://discourse.julialang.org/t/ask-user-to-provide-points-coordinates-as-two-vectors-or-as-a-single-vector-of-tuples/117717/10 "2024-08-02T17:44:02Z")

</div>

> [@Henrique\_Becker](#):
>
> It is what is often done in mathematical programming.

Isn’t it basically a Matlabism? Matlab doesn’t support vectors of tuples, so you have to provide something less natural.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [August 2, 2024, 9:21pm UTC](https://discourse.julialang.org/t/ask-user-to-provide-points-coordinates-as-two-vectors-or-as-a-single-vector-of-tuples/117717/11 "2024-08-02T21:21:42Z")

</div>

StructArrays is great for this usecase: no copy is needed, just do `StructArray(; x, y)` where `x` and `y` are your vectors. And you get all benefits, like ensuring consistent axes, convenient access to elements, and uniform familiar array interface.
