Arc Forumnew | comments | leaders | submitlogin
5 points by absz 5383 days ago | link | parent

Almost line-for-line:

  (def brackify (openb closeb)
    [+ openb _ closeb])
  
  (= rbrackify (brackify "(" ")"))
  (= cbrackify (brackify "{" "}"))
  (= sbrackify (brackify "[" "]"))
  
  ...
  
  arc> (rbrackify "adm")
  "(adm")
  arc> (cbrackify "adm")
  "{adm}"
I suppose you could also write a macro, but I wouldn't bother:

  (def brackify (openb closeb)
    [+ openb _ closeb])
  
  (mac defbrackify (name open close)
    `(= ,name (brackify ,open ,close)))
  
  (defbrackify rbrackify "(" ")")
  (defbrackify cbrackify "{" "}")
  (defbrackify sbrackify "[" "]")
  
  ...
  
  arc> (rbrackify "adm")
  "(adm")
  arc> (cbrackify "adm")
  "{adm}"


6 points by rntz 5383 days ago | link

The multiple '=s are unnecessary:

    (= rbrackify (brackify "(" ")")
       cbrackify (brackify "{" "}")
       sbrackify (brackify "[" "]"))

-----

2 points by twilightsentry 5376 days ago | link

What about a macro something like:

  (mac =map (f . args)
    `(= ,@(mappend (fn ((v . xs))
                     `(,v (,f ,@xs)))
                   args)))

  (=map brackify
    (rbrackify "(" ")")
    (cbrackify "{" "}")
    (sbrackify "[" "]"))

-----

1 point by thaddeus 5382 days ago | link

can someone explain to me how the variable adm is being passed into the function when it's not accounted for in the functions input args ?

Also - anyone notice there's now vote down buttons on the forum?

-----

4 points by absz 5382 days ago | link

The brackify function returns the closure [+ openb _ closeb]. The [... _ ...] notation means "construct an anonymous function of one argument, _"; it's the same as (fn (_) (... _ ...)). Thus, (brackify "(" ")") returns a function of one argument which, when called, surrounds its text with "()"; the text given is "adm".

The downvote buttons have been there for quite a while for me---you seem to have just broken 100 karma, so maybe it's because of that?

-----

1 point by thaddeus 5382 days ago | link

I see - thanks. T.

-----

1 point by adm 5383 days ago | link

thanks.

-----