Functions as parameters
Have you ever wondered if you could pass functions as parameters, and how that could help you sometimes? Well, I tried I guess.
What could you use functions as parameters for? You could pass on execution logic for said function when a certain thing happens in the logic above it. (functions like map,filter,find,each from other languages, or sorting algorithms) Lets see.. first, lets make an example: You have an array of numbers from 1 to 9. Your task is to create a function which doubles all array values. Preferably passing the doubling rule as a function to the array iterator. PHP Code:
PHP Code:
temp.test = this.filter(temp.array, this.f = function(x) { return x*2; }); temp.test = this.filter(temp.array, this.f = this.double); Wait, no, this is actually just a mayor workaround PHP Code:
PHP Code:
All it does is actually setting this.f to the function in the running script, making it "kinda global" in that script. This in returns allows use of this.f and/or f inside the functions as well, as they all inherit this, and they point to the variable this.f which was set during calling of the function. All an illusion kind of. I wish Graal had support for proper anonymous functions. (PS: I used filter deliberately) |
It's possible. You just need to cast the function to a string then call it. (@temp.f)(). Check out this: http://forums.graalonline.com/forums...hp?t=134261142
And, if you'd like to see something far more evil (getting access to the environment the function was defined in within the function), check out this: http://forums.graalonline.com/forums...hp?t=134258232 (Would not recommend using this one in production because it doesn't GC at all and will leak memory like a sieve.) |
All times are GMT +2. The time now is 03:34 AM. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.