Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add base array function to std #216

Merged
merged 9 commits into from
Jul 2, 2024

Conversation

CymDeveloppement
Copy link
Member

array_value(array, index)
return the value at index

array_index(array, value): Num
search the first occurrence of value and return index

array_search(array, value): [Num]
return an array of Num with corresponding indexes

@b1ek
Copy link
Member

b1ek commented Jun 19, 2024

array_value(array, index)

that's essentially a wrapper for array[index], what's the use for it? i'd imagine it throwing an error if the value doesn't exist (idk if amber doesnt already do that)

also every stdlib function should be covered by a test (see #127)

@CymDeveloppement
Copy link
Member Author

array_value(array, index) is related to #211
i think is more readable :
let str = array_value(split("foo bar test tutu", " "), 0)

compared to :
let result = split("foo bar test tutu", " ") let str = result[0]

and is permit more complex code like :

array_value(split("foo bar test tutu", " "), get_index_function_example("test"))

it's just palliative

@CymDeveloppement
Copy link
Member Author

and sorry for the test, I did not see.
I will write them

@CymDeveloppement
Copy link
Member Author

I am not a Rust specialist,
Do you think it's good ?

#[test]
fn array_value() {
    let code = "
        import * from \"std\"
        main {
            echo array_value([1, 2, 3, 4], 1)
        }
    ";
    test_amber!(code, "2")
}

#[test]
fn array_index() {
    let code = "
        import * from \"std\"
        main {
            echo array_index([1, 2, 3, 4], 3)
        }
    ";
    test_amber!(code, "2")
}

#[test]
fn array_search() {
    let code = "
        import * from \"std\"
        main {
            let result = array_search([1, 2, 3, 4, 3], 3)
            echo result[0]+result[1]
        }
    ";
    test_amber!(code, "6")
}

@Ph0enixKM
Copy link
Member

Ph0enixKM commented Jun 19, 2024

array_value(array, index)

that's essentially a wrapper for array[index], what's the use for it? i'd imagine it throwing an error if the value doesn't exist (idk if amber doesnt already do that)

@b1ek The problem is that Amber has implemented subscript syntax just for variables only. The reason is that back then array literals were literally evaluated as literals ("item1", "item2") but now they are evaluated as variables (line before:ARRAY=("item1", "item2")) $ARRAY

@b1ek
Copy link
Member

b1ek commented Jun 20, 2024

Do you think it's good ?

just push the commit and i will approve it if its good, and request changes if its not

@b1ek
Copy link
Member

b1ek commented Jun 22, 2024

@CymDeveloppement any progress on this?

@CymDeveloppement
Copy link
Member Author

CymDeveloppement commented Jun 22, 2024

@b1ek pull request is updated with the commit,
you can review

src/std/main.ab Show resolved Hide resolved
@b1ek
Copy link
Member

b1ek commented Jun 28, 2024

tbh i agree with @Mte90, function names are important since they will stay as is.

what about renaming array_value to array_get? that seems more straightforward to me as to what this function does

@CymDeveloppement
Copy link
Member Author

Maybe :
array_value ==> array_get
array_index ==> array_first_index

@Ph0enixKM
Copy link
Member

Ph0enixKM commented Jul 1, 2024

the array_get shouldn't be an stdlib function but a compiler feature. @CymDeveloppement please create an issue for this instead. I'll create this in my free time.

src/std/main.ab Outdated Show resolved Hide resolved
@Mte90 Mte90 self-requested a review July 2, 2024 14:30
@Ph0enixKM Ph0enixKM merged commit 3d117f6 into amber-lang:master Jul 2, 2024
1 check passed
@CymDeveloppement CymDeveloppement deleted the std_array_function branch July 3, 2024 14:39
Mte90 pushed a commit to Mte90/Amber that referenced this pull request Jul 3, 2024
* Add array_value function

* Add array_index function

* Add array_search function

* test for new array function

* add return value in array_index function

return -1 if value not present in array

* in_array test function

* array_value rename to array_get

* arry_index rename to array_first_index

* remove array_get function
@Mte90
Copy link
Member

Mte90 commented Jul 5, 2024

An user reported that is missing a function to get the length of the array

@Ph0enixKM
Copy link
Member

@Mte90 there is such function: len(array)

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

Successfully merging this pull request may close these issues.

None yet

4 participants