Vice City: Multiplayer

Server Development => Scripting and Server Management => Snippet Showroom => Topic started by: vito on Jul 04, 2016, 06:47 PM

Title: join / array to string
Post by: vito on Jul 04, 2016, 06:47 PM
I noticed no join method for arrays in squirrel so I made it:
function join_array(arr,sep){
local i = 0;local l = arr.len();local r = "";
if(l == 0){return "";} else if(l == 1){return arr[0].tostring();} else {r=arr[0].tostring();i=1;while(i < arr.len()){r = r + sep + arr[i].tostring();i++;}return r;};
}

Using
"hello, world, true" == join_array(["hello", "world", true], ", ")
Title: Re: join / array to string
Post by: . on Jul 04, 2016, 07:19 PM
Christ, what a horrible coding style you have. Why do you have to make everything in one line when it looks so ugly.

function join_array(arr, sep)
{
    if (typeof(arr) != "array" || arr.len() <= 0)
    {
        return "";
    }

    local str = "", sep = sep.tostring();

    foreach (val in arr)
    {
        str += val;
        str += sep;
    }

    return str.slice(0, -sep.len());
}
Title: Re: join / array to string
Post by: Thijn on Jul 04, 2016, 08:18 PM
Im wondering, is this:
str += val + sep;

faster / nicer then this:

str += val;
str += sep;
Title: Re: join / array to string
Post by: . on Jul 04, 2016, 08:32 PM
Not really since (val + sep) will be created as a separate string and deleted immediately after added to (str). Where as the other method simply adds 2 strings to (str) without making a temporary string. But it never hurts to benchmark that :D
Title: Re: join / array to string
Post by: KAKAN on Jul 05, 2016, 12:58 PM
this was released before, in the old forum, if I remember it :p
Title: Re: join / array to string
Post by: Stormeus on Jul 05, 2016, 05:44 PM
An improvement, if I may. This can be done in one line without error checking.

function join_array(arr, sep) {
    if (typeof(arr) != "array") {
        throw "join_array expected array input, got " + typeof(arr) + " (" + arr + ")";
    }
    else if (typeof(sep) != "string") {
        throw "join_array expected string separator, got " + typeof(sep) + " (" + sep + ")";
    }
    else if (arr.len() <= 0) {
        return "";
    }

    return arr.reduce(@(a, b) a + sep + b);
}

function test_join_array(arr, sep) {
    try {
        print("> " + join_array(arr, sep));
    }
    catch (e) {
        print("! Error: " + e);
    }
}

test_join_array(["test", "hello", "world"], ", ");
test_join_array(["single"], " | ");
test_join_array("string", ";");
test_join_array(["my", "array"], 7);
test_join_array([], "");

Quote from: console output[SCRIPT] > test, hello, world
[SCRIPT] > single
[SCRIPT] ! Error: join_array expected array input, got string (string)
[SCRIPT] ! Error: join_array expected string separator, got integer (7)
[SCRIPT] >
Title: Re: join / array to string
Post by: ysc3839 on Jul 05, 2016, 06:27 PM
Quote from: Stormeus on Jul 05, 2016, 05:44 PMAn improvement, if I may. This can be done in one line without error checking.

function join_array(arr, sep) {
    if (typeof(arr) != "array") {
        throw "join_array expected array input, got " + typeof(arr) + " (" + arr + ")";
    }
    else if (typeof(sep) != "string") {
        throw "join_array expected string separator, got " + typeof(sep) + " (" + sep + ")";
    }
    else if (arr.len() <= 0) {
        return "";
    }

    return arr.reduce(@(a, b) a + sep + b);
}

function test_join_array(arr, sep) {
    try {
        print("> " + join_array(arr, sep));
    }
    catch (e) {
        print("! Error: " + e);
    }
}

test_join_array(["test", "hello", "world"], ", ");
test_join_array(["single"], " | ");
test_join_array("string", ";");
test_join_array(["my", "array"], 7);
test_join_array([], "");

Quote from: console output[SCRIPT] > test, hello, world
[SCRIPT] > single
[SCRIPT] ! Error: join_array expected array input, got string (string)
[SCRIPT] ! Error: join_array expected string separator, got integer (7)
[SCRIPT] >
I know reduce in Python. I can't believe that Squirrel also have it.
Title: Re: join / array to string
Post by: Stormeus on Jul 05, 2016, 06:43 PM
@ysc3839 Squirrel has map, apply, reduce, filter and sort for arrays. Combining that with lambda expressions makes for some pretty powerful one-liners.

http://squirrel-lang.org/squirreldoc/reference/language/builtin_functions.html#id1
http://squirrel-lang.org/squirreldoc/reference/language/functions.html#lambda-expressions
Title: Re: join / array to string
Post by: vito on Feb 09, 2017, 08:26 PM
Quote from: KAKAN on Jul 05, 2016, 12:58 PMthis was released before, in the old forum, if I remember it :p

now it's here xD