Ph0enixKM / Amber

💎 Amber the programming language compiled to bash
https://amber-lang.com
GNU General Public License v3.0
3.51k stars 67 forks source link

Add base array function to std #216

Open CymDeveloppement opened 1 week ago

CymDeveloppement commented 1 week ago

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 commented 1 week ago

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 commented 1 week ago

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 commented 1 week ago

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

CymDeveloppement commented 1 week ago

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 commented 1 week ago

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 commented 1 week ago

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 commented 1 week ago

@CymDeveloppement any progress on this?

CymDeveloppement commented 1 week ago

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

b1ek commented 1 day ago

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 commented 1 day ago

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