Author Topic: $ Sign Prefixed Stuff  (Read 4413 times)

Offline parham

  • Acquaintance
  • *
  • Posts: 30
    • View Profile
$ Sign Prefixed Stuff
« on: September 08, 2008, 05:48:01 AM »
Hi again,
I was wondering if there is a list of things you can put after a $ sign (like $s, and etc), or what is their true technical name? I'm not even sure if it's an LPC feature, since I don't remember seeing it in the LPC manual.
And, would someone please tell me, how many times this function would run if the A array and the B array both have four elements, or would this even work?
string *C = (A, (: aFunction($1, $2) :), B);
If not, can someone please explain what gets assigned to $2, $3, and $4? I have asked someone, and I know that $1 refers (in functions that do something with arrays) to the array element. Another person has said that there are four $# thinggies, so that makes it $1 to $4. So what are the rest used for?
Thanks!

Offline silenus

  • BFF
  • ***
  • Posts: 196
    • View Profile
Re: $ Sign Prefixed Stuff
« Reply #1 on: September 08, 2008, 06:54:39 AM »
Hi Parham,

There are basically two variations of the $ usage in FluffOS. Both are related to functionals. The first usage i.e. $<num> is used to refer to arguments to be passed into the functional similar to the arguments list for a normal function i.e. suppose you have:

Code: [Select]
int n;
function f = (: $1 + $2 :); // create an anonymous function which adds its first argument to the second and returns it.
n = evaluate(f,4,7); // pass 4 and 7 to f.



The value of n would be 11. If you in future called evaluate(f,8,9) the value returned from the evaluate would be 17. The common usage of this is often in functions like filter and map which take a function/functional form as an argument iterate over it by passing an argument or arguments to the function and return something.

There is also a second usage of the form $(<expression>). But it seems you are interested in the first usage so I will leave things at that.

Offline daelaskai

  • BFF
  • ***
  • Posts: 174
    • View Profile
Re: $ Sign Prefixed Stuff
« Reply #2 on: September 08, 2008, 07:50:34 AM »
There is also a second usage of the form $(<expression>). But it seems you are interested in the first usage so I will leave things at that.

Oh, sure.  Dangle a treat in front of us and then take it away.  I think I'll cry. :(

Daelas :)

Offline silenus

  • BFF
  • ***
  • Posts: 196
    • View Profile
Re: $ Sign Prefixed Stuff
« Reply #3 on: September 08, 2008, 10:11:12 AM »
umm  ;). I just didnt want to confuse Parham by dumping too much information on him at once. The first usage is far more common than the second  :).

Offline parham

  • Acquaintance
  • *
  • Posts: 30
    • View Profile
Re: $ Sign Prefixed Stuff
« Reply #4 on: September 08, 2008, 10:20:47 AM »
Well, the example you gave was a $<num> inside a function. Mine was outside... so I'm still confused. And why not just use args[0..n] instead of $1..4?

Thanks for the previous answer!

Offline detah

  • BFF
  • ***
  • Posts: 190
  • Ruler of 2D
    • View Profile
Re: $ Sign Prefixed Stuff
« Reply #5 on: September 08, 2008, 10:29:02 AM »
Use the CODE tags to avoid having smilies appear everywhere. The Code Tags can be added by clicking the button with the pound sign on it.

Offline parham

  • Acquaintance
  • *
  • Posts: 30
    • View Profile
Re: $ Sign Prefixed Stuff
« Reply #6 on: September 08, 2008, 10:32:36 AM »
Yes, figured after sending.

Offline silenus

  • BFF
  • ***
  • Posts: 196
    • View Profile
Re: $ Sign Prefixed Stuff
« Reply #7 on: September 08, 2008, 10:44:51 AM »
Actually $<num> stuff can only be used inside of either a
Code: [Select]
(: :) or a function() { } and cannot be used on the outside. You can however also invoke another function in between in both cases per something like the example you gave-

Code: [Select]
map(anArray, (: aFunction($1) :) );
The code you posted wont work I think it's a bit difficult to answer how many times that will run. You might be able to test it yourself using the eval command. I am not sure what you mean by using args[0..n] instead since args isnt declared anywhere.

Offline Raudhrskal

  • BFF
  • ***
  • Posts: 214
  • The MUD community needs YOUR help!
    • View Profile
Re: $ Sign Prefixed Stuff
« Reply #8 on: September 08, 2008, 10:58:48 AM »
The $<num>s are just "positional parameters", like $1,$2,... in unix shell, or %1,%2,... in dos .bat files.
Code: [Select]
(: $1 + $2 :) is similar to a call to function like
mixed func(mixed arg1, mixed arg2) { return arg1 + arg2; }

or similarly
(: aFunc($1) :) may be related to
mixed func(mixed arg1) { return aFunc(arg1); }

Difference is in time of evaluation.

The $() syntax allows you to take a value of local variable IN the function creating a functional and write it into it as a constant.

void somefunc()
{
int x;
//blah
someCall((: $1 > x :)); //won't work, x is a var in somefunc not in functional
someCall((: $1 > $(x) :)); //take the value of x and write it in functional as a constant - so the "real" functional is (: $1 > 4 :) or whatever.
I think, therefore i may be wrong.
Please note that if you met a Raudhrskal in a place that's not related to muds, it wasn't me. *sigh*... back when I started there was zero hits on google for that name...

Offline parham

  • Acquaintance
  • *
  • Posts: 30
    • View Profile
Re: $ Sign Prefixed Stuff
« Reply #9 on: September 08, 2008, 11:07:43 AM »
Hmm. Well, I get the point, although what I've heard have made me more confused! I guess I can try EVAL and see what I get. Thanks!

Offline silenus

  • BFF
  • ***
  • Posts: 196
    • View Profile
Re: $ Sign Prefixed Stuff
« Reply #10 on: September 08, 2008, 12:12:38 PM »
Per discussion on intarmud 3. You could implement map as follows.

Code: [Select]

mixed array map(mixed array arr, function f)
{
    mixed arr2 = ({});
    for(int i = 0;i < sizeof(arr);++i)
        arr2 += ({ evaluate(f, arr[i] ) });
    return arr2;
}



Offline parham

  • Acquaintance
  • *
  • Posts: 30
    • View Profile
Re: $ Sign Prefixed Stuff
« Reply #11 on: September 08, 2008, 12:23:27 PM »
Thank you, everyone. It does make sense now.

Take care!