Stormeus

  • VC:MP Developer
  • oh god how did i get here im not good w computer
  • Posts: 1,128
Re: Scripting challenges!
« Reply #15, on June 10th, 2015, 06:16 PM »Last edited on June 10th, 2015, 06:24 PM
One-line delegate abuse (two lines if counting the array).
(click to show/hide)
Code: [Select]
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; });

One-line delegate abuse featuring lambdas:
(click to show/hide)
Code: [Select]
// 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));

.

  • VC:MP Beta Tester
  • .
  • Posts: 1,808
Re: Scripting challenges!
« Reply #16, on June 10th, 2015, 06:20 PM »
Quote from stormeus on June 10th, 2015, 06:16 PM
One-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:
(click to show/hide)
Code: [Select]
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);

I will do a review of each after I finish my movie :D
Re: Scripting challenges!
« Reply #17, on June 10th, 2015, 08:47 PM »Last edited on June 11th, 2015, 01:20 PM by stormeus
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:
Code: [Select]
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

  • VC:MP Developer
  • oh god how did i get here im not good w computer
  • Posts: 1,128
Re: Scripting challenges!
« Reply #18, on June 11th, 2015, 01:21 PM »
Posts nuked from orbit. Any vitriolic responses have been thoroughly cleansed.

EK.IceFlake

  • Hero Member
  • "We are the champions my fellahs" - Sufyan/VK.SuFy
  • Posts: 1,757

.

  • VC:MP Beta Tester
  • .
  • Posts: 1,808
Re: Scripting challenges!
« Reply #20, on June 13th, 2015, 09:34 AM »Last edited on June 13th, 2015, 09:38 AM
Quote from NE.CrystalBlue on June 13th, 2015, 04:53 AM
When will be the next challenge I wonder...
Here's a special challenge just for you: Be patient.
.

Thijn

  • VC:MP Beta Tester
  • Posts: 1,686

.

  • VC:MP Beta Tester
  • .
  • Posts: 1,808
Re: Scripting challenges!
« Reply #22, on June 14th, 2015, 08:04 AM »
New challenge for any scripter that wants to be challenged. (check the first post) Also read the new rules and updates.
.

EK.IceFlake

  • Hero Member
  • "We are the champions my fellahs" - Sufyan/VK.SuFy
  • Posts: 1,757
Re: Scripting challenges!
« Reply #23, on June 14th, 2015, 01:31 PM »
This requires CreateSprite which is not yet compiled...

.

  • VC:MP Beta Tester
  • .
  • Posts: 1,808
Re: Scripting challenges!
« Reply #24, on June 14th, 2015, 03:48 PM »
Quote from NE.CrystalBlue on June 14th, 2015, 01:31 PM
This requires CreateSprite which is not yet compiled...
What do you mean it's not yet compiled? Have you created a sprite before?

Code: [Select]
.Func(_SC("CreateSprite"), CreateSprite, 8, _SC("tsiiiini"))
.

EK.IceFlake

  • Hero Member
  • "We are the champions my fellahs" - Sufyan/VK.SuFy
  • Posts: 1,757
Re: Scripting challenges!
« Reply #25, on June 14th, 2015, 03:58 PM »
Quote from S.L.C on June 14th, 2015, 03:48 PM
Quote from NE.CrystalBlue on June 14th, 2015, 01:31 PM
This requires CreateSprite which is not yet compiled...
What do you mean it's not yet compiled? Have you created a sprite before?

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

.

  • VC:MP Beta Tester
  • .
  • Posts: 1,808
.

EK.IceFlake

  • Hero Member
  • "We are the champions my fellahs" - Sufyan/VK.SuFy
  • Posts: 1,757

Sebastian

  • VC:MP Beta Tester
  • Something like this: hga hga hga
  • Posts: 1,193
Re: Scripting challenges!
« Reply #28, on June 14th, 2015, 05:22 PM »Last edited on June 15th, 2015, 10:23 AM
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

  • VC:MP Developer
  • oh god how did i get here im not good w computer
  • Posts: 1,128
Re: Scripting challenges!
« Reply #29, on June 14th, 2015, 05:45 PM »
Quote from NE.CrystalBlue on June 14th, 2015, 03:58 PM
Quote from S.L.C on June 14th, 2015, 03:48 PM
Quote from NE.CrystalBlue on June 14th, 2015, 01:31 PM
This requires CreateSprite which is not yet compiled...
What do you mean it's not yet compiled? Have you created a sprite before?

Code: [Select]
.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.