Arc Forumnew | comments | leaders | submitlogin
How do I pass an arbitrary function name into a function for re-use?
1 point by thaddeus 5848 days ago | 3 comments
I was trying to create a function that would display generic stuff plus whatever function I wanted to output for the day, but I think I'm going about it wrong.

I created a simplified scenario:

Trying to create a function called "todays-event" that...

If called without args would:

* print: "Sorry! nothing new for today" from inside the 'todays-event' function

    ; (todays-event)
    ; hoped for result: 
    "Sorry! nothing new for today"
If called with args would:

* display "Generic introduction to todays: " ....the generic stuff inside the function 'todays-event'

* then display the results of the function passed in....

    ;(todays-event "song" (sunday-song))
    ; hoped for result:
    Generic introduction to todays: Song
    I love this song!
    Psuedo Echo: Funky Town
    Line 1: Won't you take me to....funky-town...yeah yeah
    Line 2: Won't you take me to....funky-town...yeah yeah yeah
    Line 3: Won't you take me to....funky-town...yeah yeah

    ;(todays-event "word" (sunday-word))
    ; hoped for result:
      Generic introduction to todays: Word
      LISP
      Lots of Infuratingly Strange Parenthesis

    ;; functions created

    (def todays-event ((o type) (o todays-thing))
        (if (is type nil)
            (pr "Sorry! nothing new for today")
            ((pr "Generic introduction to todays:     ")(pr type)
         (list todays-thing)))) 

    (def sunday-song ()
           (prbold "I love this song!")
           (link "Psuedo Echo: Funky Town"   "http://www.youtube.com/watch?v=jIOurOl3Y1A")
           (prn "Line 1: Won't you take me to....funky-town...yeah yeah")
           (prn "Line 2: Won't you take me to....funky-town...yeah yeah yeah")
           (prn "Line 3: Won't you take me to....funky-town...yeah yeah"))

    (def sunday-word ()
           (prbold "LISP")
           (prn "Lots of Infuratingly Strange Parenthesis"))
The idea is that I can create any given function for the day and pass it into an existing framework. The only problem is that (todays-thing) is being evaluated right off the start and not being passed into the part (list todays-thing)....

Any suggestions on how to do this kind of thing ?

Thanks. T.



3 points by CatDancer 5848 days ago | link

If I understand what you want...

  (todays-event "song" sunday-song)

  (def todays-event ((o type) (o todays-thing))
     [...]
     (todays-thing))
Because "todays-thing" can be nil since it is an optional argument, you'd probably also only want to call it if it isn't nil, like this:

  (def todays-event ((o type) (o todays-thing))
     [...]
     (if todays-thing (todays-thing)))
which in turn can be make shorter with aif:

  (def todays-event ((o type) (o todays-thing))
     [...]
     (aif todays-thing (it)))
I hope this helps!

-----

1 point by rincewind 5847 days ago | link

What about using default paramters for optional args?

  (def todays-event ((o type) (o todays-thing nilfn))
     ...

-----

1 point by thaddeus 5848 days ago | link

That's perfect - Thank you! T.

-----