Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

Arrays indexers #561

Closed
otawakkil opened this issue Mar 3, 2019 · 5 comments
Closed

Arrays indexers #561

otawakkil opened this issue Mar 3, 2019 · 5 comments

Comments

@otawakkil
Copy link

So far I wasn't able to find indexers function in arrays, and I cannot extract one item from an array.
Even with the foreach with the condition, an array is returned.

It would be interesting to have the usual:

let lst = [1,2,3]
let item = getItem(lst, 1); // to retrieve the item under the index 1
let item = lst[1]; // more natural
let last = getLast(lst); // returns the last
let first = getFirst(lst); // returns the first

if predicates are supported something like:
let goodItem = find(itemList, x.id == "someId");

@jeromesimeon
Copy link
Member

I will add getSingleton to this list... maybe that's a good name for its to align with the other accessors.

@jphillips-bp3
Copy link

Yup, I am hitting this problem now also. I like the predicate idea a lot.

let goodItem = find(itemList, x.id == "someId");

@dselman
Copy link
Contributor

dselman commented Mar 15, 2019

The Singleton function was added here: https://github.com/accordproject/ergo/releases/tag/v0.6.3

@jeromesimeon
Copy link
Member

Support for array index access is ready in the markdown branch https://github.com/accordproject/ergo/tree/js-experiment-markdown

Proposed syntax:

e1[e2]

where e1 and e2 are expressions, with e1 of type array and e2 of type Long.

The result of that function is an option.

Examples:

Welcome to ERGOTOP version 0.8.2
ergo$ return ["foo","bar"][0]
Response. some("foo") : String?
ergo$ return [1,2][2]
Response. none : Integer?
ergo$ return { a : [1,2,3] }.a[1]
Response. some(2) : Integer?
ergo$ return [1,2][-1]
Response. none : Integer?
ergo$ 

@jeromesimeon
Copy link
Member

The work is completed in #623 with syntax and semantics as shown in the previous message.

Please re-open if any concern/questions.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants