[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Lua ➜ Comparing a function to another

Comparing a function to another

It is now over 60 days since the last post. This thread is closed.     Refresh page


Pages: 1  2 

Posted by Ksajal   (17 posts)  Bio
Date Reply #15 on Mon 26 Apr 2010 05:29 PM (UTC)

Amended on Mon 26 Apr 2010 05:39 PM (UTC) by Ksajal

Message
David Haley said:

My suggestion works with more than just names, but it does seem like your functions are complex enough to not be just data. But then the question is why exactly can't you give these functions proper names or at least values that you can directly compare?

Because for each module that may queue a function I have a method that does that automatically, without me directly specifying the function. I'm not a programmer, I do it just for fun, I find it difficult to find the right words (I'm not even a native English speaker, just take a look at the typo in the thread title), and my code may seem really weird at times, but that's how I imagined it working, so here is an example.

Most of the time, I queue the function affs:add (name, val) in my prompt. So instead of typing each time prompt:queue (function () affs:add ("affliction")end, "affs_add_affliction", true) I use this:

function affs:add_queue (name, val)

prompt:queue (function () affs:add (name, val)end, "affs_add_"..name, true)

end--function

affs:add_queue ("affliction")


It's just easier to type, and modify. Instead of modifying each different function, I just modify the affs:add_queue (name, val). The same reason I use affs:add (name, val) instead of affs ["current"] [aff] = val
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #16 on Mon 26 Apr 2010 06:24 PM (UTC)

Amended on Mon 26 Apr 2010 07:42 PM (UTC) by David Haley

Message
I don't really understand what forces you to do it this way or even why it's more convenient. Maybe there's something I'm not seeing based on what you have or haven't said about your problem description, but this setup just seems weird and convoluted to me.

If I'm understanding you correctly, your problem is that you want a generic way of adding functions to your prompt queue, and don't want to list out 100 functions that are all the same.

That's ok: you can still give them names or at least handles, if you create a map from name to function:


prompt_funcs = {}
names = {'affliction', 'bla', 'foo'}

for _, name in ipairs(names) do
  -- val is nil here and in your code, by the way
  local val = nil
  prompt_funcs[name] = function () affs:add (name, val) end
end


Now, for a given name, you have a unique function that adds that name.

In other words, to give functions unique handles, you don't need to spell every one out explicitly; you can generate the functions.

I suppose it's kind of ok to create an identifier as you ad things into the queue, but that's unsafe and kind of weird.

(EDIT: fixed implicitly --> explicitly)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #17 on Mon 26 Apr 2010 07:35 PM (UTC)
Message
David Haley said:


prompt_funcs = {}
names = {'affliction', 'bla', 'foo'}

for _, name in ipairs(names) do
  -- val is nil here and in your code, by the way
  local val = nil
  prompt_funcs[name] = function () affs:add (name, val) end
end


Now, for a given name, you have a unique function that adds that name.

In other words, to give functions unique handles, you don't need to spell every one out implicitly; you can generate the functions.


That's also the approach ACS2 (which I pointed out before) takes for generating its affliction-handler functions. :)

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


47,785 views.

This is page 2, subject is 2 pages long:  [Previous page]  1  2 

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]