evalexpr icon indicating copy to clipboard operation
evalexpr copied to clipboard

Support get value from tuple

Open ogata-k opened this issue 3 years ago • 6 comments

I want to get value at the index from a tuple.

e.g. eval("(1,2,3,4).4") // 4

ogata-k avatar Mar 08 '22 23:03 ogata-k

That's a nice idea. I am unsure though if I want to implement this as a new syntax or simply as a builtin function.

What do you think about e.g. eval("get((1,2,3,4), 4)") // 4?

ISibboI avatar Mar 12 '22 06:03 ISibboI

If I think of it as an array rather than a tuple, I think that such a function notation is better. But, I have never seen such a function notation in a tuple.

However, since I just want to get the value from the tuple, it doesn't matter which one I use.

ogata-k avatar Mar 12 '22 10:03 ogata-k

@ISibboI

Hey, this would be also useful in the project I'm working on. I'm currently thinking about implementing it. Did you already start, if not I will be happy to make another pullrequest for it soon.

tsmt09 avatar Jul 21 '22 20:07 tsmt09

I'm sorry, I'm thinking about how to implement it, but I can't think of a good one.

ogata-k avatar Jul 21 '22 21:07 ogata-k

for us, it would be also be great to have arrays(vecs) and objects(maps) to be compatible with serde_json some time in future. but I understand that woudl be a loooong way to go and I'm currently happy just having tuples. so I don't dare asking for it, but maybe I will come up with an array and object implementation draft in the future.

That being said, the usual ways of writing it

  • (1,2,3,4)[2] - i would not like because I would save it for future array implementations
  • (1,2,3,4).2 - seems reasonable
  • get((1,2,3,4),4) - also a good way. maybe it would make sense to call it tuple::get the same way theres math:: and str:: functions. then, tuple::set would also make sense of course.

tsmt09 avatar Jul 21 '22 21:07 tsmt09

Thank you for your interest in implementing this! I actually thought about the dot notation ((1, 2).0) before, but it was not trivial to implement because the dot is also a decimal separator. So it would mean to refactor also float parsing a little. At the moment I am quite busy, so I would not want to make that non-trivial changes myself. However, I am happy to review and accept pull requests.

ISibboI avatar Jul 22 '22 08:07 ISibboI