(show&tell) Def-Con-Function, What's Your Function*
Posted: Sat May 13, 2006 2:45 pm
*to the tune of "Conjunction Junction", a popular children's educational commercial (did I really just say educational commercial?)
The Def-Con-Function I'm referring to in the title stands for default context function, and it's the means to the end for a little programming dream I've had for some time now. To spoil the ending for you (don't worry, I know what I'm doing), it ends badly. How so? I wouldn't dream of spoiling the ending for you ;-)
Calling a function function. That was it. That was my big dream. Actually, at the time, I wanted to send a message to a message. It began while experimenting with Ruby's natural way of chaining methods together, and I found I wanted to call a method on the method I was typing at the command line. Why? To ask it (remember we are in objectland here) for help on using it or to test it, for example. This is all happening on the command line, so the idea was to talk to the method right then and there while I had the object (method) in front of me. An example might be:
At this point, I think to myself, what is the symbol I pass in to sort by size? So what I wanted to do is backspace over the left paren and proceed:
There's my answer: :size, so I can continue:
That's were the idea began, and I have wondered how I could implement something to this effect in most every language I have studied since.
If you're still unclear on the difference between what I wanted and what would actually happen if I sent help to the table returned by (unpack), then realize that instead of wanting to send a message to the receiver, I wanted to send one to the message I was intending to send to the receiver. A message to the message.
The value of being able to do this is questionable, but preserving our flow while coding is a big deal and should be aided whenever possible. Bringing the answer as close to the question as we can through minimal effort on the user's part helps keep us on the plane (a sailing term).
Is this a language or an IDE issue? I just don't know. I do know that if a language had the power of an IDE built right into it, that might be one of the easiest languages to program in to date.
Really though, I realized the OOP languages I'd been using were not OOP enough, as I could not send messages to the messages. Are messages objects? I needed them to be in this case.
Here in newLISPland, we're blessed with not having to worry about all that OOP poop :-) In fact, the ability to do what I wanted is built right into newLISP! Default context functions let us call functions in a context (this is normal), but they also allow us to call functions that seem to be part of functions themselves (neat!). Function functions!
Now, here is the bad part I mentioned in the beginning. This technique is mostly useless. Yes, I finally get to experience calling a function function, but like most dreams, it has a way of looking pale and small, once realized.
I've found this lends itself mostly to contexts with one function called repeatedly or by default. I have this situation in my ob context where I normally call the . . . well, maybe we should leave that for another time ;-)
m i c h a e l
The Def-Con-Function I'm referring to in the title stands for default context function, and it's the means to the end for a little programming dream I've had for some time now. To spoil the ending for you (don't worry, I know what I'm doing), it ends badly. How so? I wouldn't dream of spoiling the ending for you ;-)
Code: Select all
-> (words Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")
> (words:help)
words
syntax: (words sym*) -> (str*)
Returns a list containing a string for every sym. Can be empty (no args).
example:
-> (words Mon Tue Wed Thu Fri Sat Sun)
("Mon" "Tue" "Wed" "Thu" "Fri" "Sat" "Sun")
> (words)
()
> (words:test)
"ok"
> (words:test -v)
The following should look identical:
("this" "is" "the" "way" "we" "wash" "our" "clothes")
("this" "is" "the" "way" "we" "wash" "our" "clothes")
> ; wheeeee!
Code: Select all
:ruby> carton.open.unpack(table).sort(_#<-- right here
At this point, I think to myself, what is the symbol I pass in to sort by size? So what I wanted to do is backspace over the left paren and proceed:
Code: Select all
:ruby> carton.open.unpack(table).sort.help
sort
syntax: sort : Array <- Array <- Symbol
Bla bla bla . . . the symbols are :size, :weight, :age, etc.
There's my answer: :size, so I can continue:
Code: Select all
:ruby> carton.open.unpack(table).sort(:size).show
That's were the idea began, and I have wondered how I could implement something to this effect in most every language I have studied since.
If you're still unclear on the difference between what I wanted and what would actually happen if I sent help to the table returned by (unpack), then realize that instead of wanting to send a message to the receiver, I wanted to send one to the message I was intending to send to the receiver. A message to the message.
The value of being able to do this is questionable, but preserving our flow while coding is a big deal and should be aided whenever possible. Bringing the answer as close to the question as we can through minimal effort on the user's part helps keep us on the plane (a sailing term).
Is this a language or an IDE issue? I just don't know. I do know that if a language had the power of an IDE built right into it, that might be one of the easiest languages to program in to date.
Really though, I realized the OOP languages I'd been using were not OOP enough, as I could not send messages to the messages. Are messages objects? I needed them to be in this case.
Here in newLISPland, we're blessed with not having to worry about all that OOP poop :-) In fact, the ability to do what I wanted is built right into newLISP! Default context functions let us call functions in a context (this is normal), but they also allow us to call functions that seem to be part of functions themselves (neat!). Function functions!
Now, here is the bad part I mentioned in the beginning. This technique is mostly useless. Yes, I finally get to experience calling a function function, but like most dreams, it has a way of looking pale and small, once realized.
I've found this lends itself mostly to contexts with one function called repeatedly or by default. I have this situation in my ob context where I normally call the . . . well, maybe we should leave that for another time ;-)
m i c h a e l