Scripting challenges!

Started by ., Jun 10, 2015, 10:13 AM

Previous topic - Next topic

Stormeus

#15
One-line delegate abuse (two lines if counting the array).
[spoiler]
local arrayTest = ["test1","test2","test3","test4"];

// .apply replaces each value in an array with the returned value.
//
// This is a cheaty way of printing stuff without loops and we need to
// return the original value to avoid filling the array with nulls.
arrayTest.apply(function(v) { print(v); return v; });
[/spoiler]

One-line delegate abuse featuring lambdas:
[spoiler]
// Squirrel internally clones the array and uses the return value from map to
// fill the cloned array.
//
// If you were to look at the returned array from .map, you'd see a bunch
// of nulls, as we never return anything.
arrayTest.map(@(v) print(v));
[/spoiler]

.

Quote from: stormeus on Jun 10, 2015, 05:16 PMOne-line delegate abuse (two lines if counting the array).

Finally someone found the last approach. See people! That's what happens when you RTFM. I thought I was going to close this without anyone comming with the proper solution ;D

Either way. Here's all of them:
[spoiler]
local arr = [2, 354, "foo", 43, 5.234, 675, "bar", true, {a = 23, b = 34.546}];

// Nethod 1 --------------------------------------------------------

// Recursive functions
function RecursiveApproach(arr, idx = 0)
{
    if (idx < arr.len())
    {
        ::print(arr[idx]);
        // Go down the rabbit hole
        RecursiveApproach(arr, ++idx);
    }
}

// Initial call
RecursiveApproach(arr, 0);

print("---- ---- ---- ---- ---- ----");

// Nethod 2 --------------------------------------------------------

// Default delegates + lambda
function DelegatesApproach1(arr)
{
    // NOTE: arr becomes filled with nulls now
    arr.apply(@(v) ::print(v));
}

// We make a shallow copy of the array
//  Otherwise we fill the original with nulls
DelegatesApproach1(clone arr);

print("---- ---- ---- ---- ---- ----");

// Default dlegates + function
function DelegatesApproach2(arr)
{
    // NOTE: arr doesn't become filled with nulls now
    arr.apply(function(v) { ::print(v); return v; });
}

DelegatesApproach2(arr);

print("---- ---- ---- ---- ---- ----");

// Nethod 3 --------------------------------------------------------

function VarargsApproach(arr)
{
    // Insert the format string in the argument pack
    arr.insert(0, "");

    // Generate the format string
    local fmt = function(arr, idx = 0)
    {
        if (idx < arr.len())
        {
            switch (typeof arr[idx])
            {
                case "integer": arr[0] += "%d\n"; break;
                case "float": arr[0] += "%f\n"; break;
                case "string": arr[0] += "%s\n"; break;
                default:
                    arr[0] += "%s\n";
                    try {
                        arr[idx] = arr[idx].tostring();
                    } catch (e) {
                        arr[idx] = "(null : 0x00000000)";
                    }
                break;
            }
            // Go down the rabbit hole
            callee().call(this, arr, ++idx);
        }
    }

    // Skip the format string
    fmt(arr, 1);

    // Insert the 'this' environment
    arr.insert(0, this);

    // Print the f*er
    ::print(::format.acall(arr));
}

// We make a shallow copy of the array
//  Otherwise we edit the original
VarargsApproach(clone arr);
[/spoiler]

I will do a review of each after I finish my movie :D
.

.

#17
Ok, review time :D



The first one comes from @NE.CrystalBlue and I must say it's very disappointing. Not only that you do a sh!t load of if statements but you also have to add more code every time the array gets bigger. Not a viable solution.



Second one comes from @Doom_Killer.



Third one comes from @Kratos_ . And shows the use of recursive functions. One more thing I need to suggest to almost everyone is to stop using stuff on onScriptLoad() event because Squirrel is executed the same way PHP is. Top down. Like the following example:
function say(a)
{
    print(a);
}

say("Wazzaaaa!");

As soon as the compiler reaches the 'say("Wazzaaaa!");' line that function will get called. I'm just saying this because many scripters think Squirrel works the same way as C/C++. NO! Code is executed from top to bottom. No need to use events.

Another disappointment was the fact that the array is declared inside the recurred function which adds more load and also that 'if' condition could be combined with the line bellow it to get rid of the return.

Anyway, this was the only functional approach up until this point.



Fourth one comes from @vcmptr and improves upon the previous one. Although I would also like to suggest using pre-increment operators instead of assignment operators. Either way, good approach.



Again, the fifth one comes from @NE.CrystalBlue this time trying to fix his mistake in the first one. The approach works and even though it's not the best, it's unique and does what was required. I was curious to see if anyone would use timers ;D



And yet again, @NE.CrystalBlue tries to improves his approach. The nice thing is that you can have custom separators. Even though the implementation could be improved, I give it points for the custom delimiter feature.



Finally we reach the seventh approach and the one I was expecting. The one from @stormeus that uses the built in default delegates of the array type. That's what happens when you read the manual people. You learn what optios you have. By far the most elegant approach. A single line to do what you need.



I will now order your approaches based on how good it was.




I will be posting the next challenge soon. Probably it won't be printing tables without loops because that's going to ask for some really awkward approaches ;D Thanks to all who participated.
.

Stormeus

Posts nuked from orbit. Any vitriolic responses have been thoroughly cleansed.

EK.IceFlake

When will be the next challenge I wonder...

.

#20
Quote from: NE.CrystalBlue on Jun 13, 2015, 03:53 AMWhen will be the next challenge I wonder...

Here's a special challenge just for you: Be patient.
.

Thijn

Quote from: S.L.C on Jun 13, 2015, 08:34 AM
Quote from: NE.CrystalBlue on Jun 13, 2015, 03:53 AMWhen will be the next challenge I wonder...

Here's a special challenge just for you: Be patient.

.

New challenge for any scripter that wants to be challenged. (check the first post) Also read the new rules and updates.
.

EK.IceFlake

This requires CreateSprite which is not yet compiled...

.

Quote from: NE.CrystalBlue on Jun 14, 2015, 12:31 PMThis requires CreateSprite which is not yet compiled...

What do you mean it's not yet compiled? Have you created a sprite before?

.Func(_SC("CreateSprite"), CreateSprite, 8, _SC("tsiiiini"))
.

EK.IceFlake

Quote from: S.L.C on Jun 14, 2015, 02:48 PM
Quote from: NE.CrystalBlue on Jun 14, 2015, 12:31 PMThis requires CreateSprite which is not yet compiled...

What do you mean it's not yet compiled? Have you created a sprite before?

.Func(_SC("CreateSprite"), CreateSprite, 8, _SC("tsiiiini"))
Who said we load the whole module? We just load squirrel... Pure and native support only

.

Quote from: NE.CrystalBlue on Jun 14, 2015, 02:58 PMWho said we load the whole module? We just load squirrel... Pure and native support only

Are you trolling me?
.

EK.IceFlake

Quote from: S.L.C on Jun 14, 2015, 03:17 PM
Quote from: NE.CrystalBlue on Jun 14, 2015, 02:58 PMWho said we load the whole module? We just load squirrel... Pure and native support only

Are you trolling me?
You're excellently smart (take their first letters)

Sebastian

#28
Damn, this challenge is so... for me.
I hope I will find some free-time today to make it happen, because I already have some ideas. :)

EDIT: Unfortunately, I won't do that... Don't have the needed free-time. In a couple of weeks, the big exams starts, and I really need to study since them are influencing my future a lot. Good luck guys !

Stormeus

Quote from: NE.CrystalBlue on Jun 14, 2015, 02:58 PM
Quote from: S.L.C on Jun 14, 2015, 02:48 PM
Quote from: NE.CrystalBlue on Jun 14, 2015, 12:31 PMThis requires CreateSprite which is not yet compiled...

What do you mean it's not yet compiled? Have you created a sprite before?

.Func(_SC("CreateSprite"), CreateSprite, 8, _SC("tsiiiini"))
Who said we load the whole module? We just load squirrel... Pure and native support only

... sprites are pure and native to Squirrel. End of discussion.