nskins / goby

Command-line role-playing game framework
MIT License
122 stars 56 forks source link

Hash of world (and battle?) commands #111

Closed nskins closed 6 years ago

nskins commented 7 years ago

Does Ruby have first-order functions? It would be great if we could split input into a "key" and a "list of arguments" to the value in a hash map. Each function in the "values" has type :: "Array of Strings" -> nil. This means it takes an "Array of Strings" and returns nil. Again, the argument would be the ordered set containing the elements which follow the key. This would greatly simplify the implementation of running world commands, but I'm not sure about functional aspects of Ruby.

pablofullana commented 7 years ago

Not sure if I'm getting this right, but it sounds that you are looking for lambdas (perhaps an example could help me understand what you are trying to achieve here). If this is the case, you may find this useful https://stackoverflow.com/questions/13940515/using-higher-order-functions-in-ruby

nskins commented 7 years ago

Here's an example: the user types "use giant banana". We hash the key "use" to find the function #use_item (or something like that). Then we call the returned function as use_item(["giant", "banana"]).

Another example would be the command "help". We hash the key "help" to find #help. We then call the function help([]) (note that the command did not have any "arguments").

Higher-order functions are functions that take other functions as arguments, so it's a little different, I think? I'm looking for a way to use functions as types (like in Haskell).