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, confirm your email, resolve issues, 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.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Useful calculator Alias

Useful calculator Alias

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


Pages: 1  2 3  

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #15 on Thu 19 Aug 2010 06:20 AM (UTC)

Amended on Thu 19 Aug 2010 06:22 AM (UTC) by Nick Gammon

Message
WillFa said:

Why would you want to math a string.find? This is a quick calculator, not a replacement for /print() after all.


I was hoping someone would drag us back to reality at some stage. :)

Having said that, it *is* handy to sometimes use Lua to find out simple things, like the code corresponding to a letter, eg.


=byte 'a'  --> byte 'a' = 97


In this case that is shorter than:


/print (string.byte 'a')  --> 97


And if you modify it to also look up the "world" table you can do nice stuff like this:


=GetWorldID ()  --> GetWorldID () = 3bcfeeb548c77aa15ae19061


So I suppose it depends if you are using it for straight maths, or a more general purpose "evaluate an expression and print the result" tool.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #16 on Thu 19 Aug 2010 06:23 AM (UTC)
Message
Modified version:


<aliases>
  <alias
   match="=*"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>
print ("%1 =", setfenv (assert (loadstring "return %1"), setmetatable ({}, 
    {__index = function (_, n) return math [n] or bit [n] or string [n] or utils [n] or world [n] end}) ) () )
</send>
  </alias>
</aliases>


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #17 on Thu 19 Aug 2010 06:52 AM (UTC)

Amended on Thu 19 Aug 2010 06:55 AM (UTC) by Twisol

Message
When you're using it as a general eval-alias, I prefer to leave out the shorthand environment because it's too un-obvious. I have to remember which tables are checked. If there are any shared keys, I have to remember which table is checked first. I can't print out a variable I just set with "/foo = 42" without adding _G to the list anyways. For that matter, I can't access global functions without adding _G to the list, either. And I have to manually add other namespaces later, like if I have a JSON API table.

For niche cases like a calculator, it makes loads of sense. But it has its limits.

Nick Gammon said:
And if you modify it to also look up the "world" table you can do nice stuff like this:

=GetWorldID ()  --> GetWorldID () = 3bcfeeb548c77aa15ae19061


You can do that with a perfectly normal, un-setfenv'd eval alias, too. :) World functions are accessible through _G.

'Soludra' on Achaea

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

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #18 on Thu 19 Aug 2010 07:14 AM (UTC)
Message
As usual, the question about whether the implementation is correct depends on what you are trying to achieve.

If Henry Tanner simply wants to evaluate mathematical expressions, then we have all over-egged the pudding.

His original alias is simple, easy to understand, and works fine for that purpose.

To be honest, all this sophisticated "loadstring" and "setfenv" stuff is clever, but isn't the sort of thing you rattle off without thinking and without having to look up a reference.

The original idea works perfectly for the situation where you just need to do simple arithmetic.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #19 on Thu 19 Aug 2010 07:30 AM (UTC)
Message
Twisol said:

(Off-topic: Happy birthday to me.... I'm now 18. ^_^)


Oh and Happy Birthday, Twisol!

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #20 on Thu 19 Aug 2010 07:44 AM (UTC)
Message
Nick Gammon said:
To be honest, all this sophisticated "loadstring" and "setfenv" stuff is clever, but isn't the sort of thing you rattle off without thinking and without having to look up a reference.


I absolutely agree... although assert(loadstring("return %1"))() is almost a necessity just because it fixes the double quotes issue.


Nick Gammon said:
Oh and Happy Birthday, Twisol!

Haha, thanks! ^_^

'Soludra' on Achaea

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

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #21 on Thu 19 Aug 2010 01:47 PM (UTC)
Message
Quote:
I absolutely agree... although assert(loadstring("return %1"))() is almost a necessity just because it fixes the double quotes issue.

But... why are we fixing a double quotes issue for a math calculator? I still am not sure why this is really useful. :-/

Besides, you could just use single quotes...

AFAICT the thing going for the loadstring approach is that it lets you use the metatable trick, which I think is extremely useful: having to type out math.log, math.sqrt, math.pow all the time would get pretty old. (I'm not convinced you need all the other stdlib tables, though.)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

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

Posted by WillFa   USA  (525 posts)  Bio
Date Reply #22 on Thu 19 Aug 2010 02:22 PM (UTC)
Message
Ya know, this reminds me of when I was younger than Twisol (Happy Bday) and in school. I got a calculator for Algebra II/Trig class, and another kid walks in with a TI-88 Graphing calculator. All the damn feature creep in that phone... by the end of the semester, he was playing Mortal Kombat on it...


It's.
A damn.
Calculator.
People.


:)


(Can your alias do Reverse Polish Notation?)
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #23 on Thu 19 Aug 2010 03:19 PM (UTC)
Message
David Haley said:

Quote:
I absolutely agree... although assert(loadstring("return %1"))() is almost a necessity just because it fixes the double quotes issue.

But... why are we fixing a double quotes issue for a math calculator? I still am not sure why this is really useful. :-/

Besides, you could just use single quotes...


Admittedly this is a little off from the original "calculator" alias, but a general evaluate-and-print alias is extremely useful. I use double quotes most by force of habit, so having to see an error every time I slip is a pain.

David Haley said:
AFAICT the thing going for the loadstring approach is that it lets you use the metatable trick, which I think is extremely useful: having to type out math.log, math.sqrt, math.pow all the time would get pretty old. (I'm not convinced you need all the other stdlib tables, though.)


You can do it without loadstring, as Nick did in his earlier examples.

WillFa: That's exactly why it's so enticing, IMHO. I've never tried, but you have to work within an interesting set of constraints, and you can do some cool stuff. Look at Tetris in Excel. ;)

'Soludra' on Achaea

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

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #24 on Thu 19 Aug 2010 03:58 PM (UTC)
Message
I should have said not loadstring but creating a function, it's what I was thinking in any case so I'm not sure why I said just loadstring. :P

And maybe it's just me, but I've not really ever (to my recollection) found it appealing to force myself into artificial constraints just to do something that would be easier elsewhere. So I guess I don't understand the appeal of things like Tetris in Excel. OK, yeah, it's an interesting hack on top of what Excel lets you do... but what's the point, in the end of the day? To get Tetris? Err :P

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

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

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #25 on Thu 19 Aug 2010 08:56 PM (UTC)
Message
WillFa said:

It's.
A damn.
Calculator.
People.


I've got some fancy graphing calculators and some simple ones. I usually reach for the simple one. The one that lets me add things together with minimal keystrokes, or convert from hex to decimal.

Not the one that, when I ask it:

"What is SIN(30)"

replies:

"SIN(30)"

(Yes, I know why it does it, but I don't feel that my question has been really answered).

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #26 on Thu 19 Aug 2010 09:02 PM (UTC)
Message
Quote:
Not the one that, when I ask it:

"What is SIN(30)"

replies:

"SIN(30)"

(Yes, I know why it does it, but I don't feel that my question has been really answered).

That sounds like somebody had a sense of humor when developing the calculator. :-)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

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

Posted by Henry Tanner   Finland  (23 posts)  Bio
Date Reply #27 on Sat 21 Aug 2010 10:23 AM (UTC)
Message
Originally I intended to share this thing with you of the sole purpose of you maybe finding an use for it.

For now I've done well with what I have, but if and when I feel that I need to upgrade it a bit I know which post to look up ;D

I got this!
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #28 on Wed 25 Aug 2010 05:00 AM (UTC)
Message
Just to make it easier to use, I have bundled the latest variation into a plugin. That way you just need to install the plugin, rather than fumbling around trying to find the alias (or one of them) mentioned in this thread.

Template:saveplugin=Calculator To save and install the Calculator plugin do this:
  1. Copy between the lines below (to the Clipboard)
  2. Open a text editor (such as Notepad) and paste the plugin into it
  3. Save to disk on your PC, preferably in your plugins directory, as Calculator.xml
  4. Go to the MUSHclient File menu -> Plugins
  5. Click "Add"
  6. Choose the file Calculator.xml (which you just saved in step 3) as a plugin
  7. Click "Close"



<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>
<plugin
   name="Calculator"
   author="Nick Gammon"
   id="ba1329d6355e728dd100fd41"
   language="Lua"
   purpose="Provides a simple calculator"
   date_written="2010-08-25 14:55:13"
   requires="4.50"
   version="1.0"
   >
<description trim="y">
<![CDATA[
To calculate something enter "=" followed by an expression which can be evaluated by Lua.

eg.

= 2 + 2     (prints 4)

= sqrt (64)  (prints 8)

The functions in the math, bit, string, utils and world libraries can be used without prefixing them with the library name.

eg.

= GetWorldID () 
= match ("fruit", "u")
= shl (4, 4)

]]>
</description>

</plugin>


<!--  Aliases  -->

<aliases>
  <alias
   match="=*"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>
print ("%1 =", setfenv (assert (loadstring "return %1"), setmetatable ({}, 
    {__index = function (_, n) return math [n] or bit [n] or string [n] or utils [n] or world [n] end}) ) () )
</send>
  </alias>
</aliases>

</muclient>


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #29 on Wed 25 Aug 2010 05:17 AM (UTC)
Message
At this point I think it might be better termed a mini-shell of sorts than a calculator. :P

Just wait until people want to assign results to temporary variables... ;)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
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.


116,616 views.

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

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

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