Arc Forumnew | comments | leaders | submitlogin
nifty :
3 points by CatDancer 6166 days ago | 4 comments
I was reading through the arc code and noticed there were several places where system was being called with strings concatenated together, the output captured, and then the trailing newline removed. So a function could be extracted that does this:

  (rmeol/ssystem "echo " "hi")  ==> "hi"
(not sure about the names), but then I thought, "rmeol" is its own useful little piece of functionality, so it could be it's own function and "rmeol/ssystem" would combine them... and then I realized that's what : would do!

  (rmeol:ssystem "echo " "hi")  ==> "hi"
I thought it was cool that ":" let me write the construct in the way I was thinking of it, without having to write a third function to combine the two. (Yes, I know Haskell's . does this too, but somehow my brain finds this more readable).

  (ssystem "echo " "hi")  ==> "hi\n"

  (def ssystem args
    (tostring (system (apply string args))))

  (rmeol "hi\n")  ==> "hi"

  (def rmeol (s)
    (subseq s 0 (- (len s) 1)))
... and yes, of course rmeol could usefully check if the string does end in an eol character, like Perl's chomp.


2 points by cpfr 6165 days ago | link

I think this is exactly what pg was thinking with : . I had a similar moment with rev:range

-----

1 point by mascarenhas 6165 days ago | link

I don't have an arc prompt to try this right now, but wouldn't (rmeol:system:string "echo" "hi") do the same? If this is too verbose you can do (def ssystem system:string), I think.

-----

2 points by CatDancer 6164 days ago | link

Not quite: you need tostring to capture the output of system (which is sent to stdout, not returned as a string). tostring is macro, so you can't compose it using :.

If I thought of a shorter name than "rmeol/ssystem" that I would still understand to mean "concatenate strings together, pass to system, capture the output, and remove the trailing newline", I would use it instead of "rmeol:ssystem". If "rmeol/ssystem" is the best I can come up with, then hey, I can save myself a function def and use "rmeol:ssystem". :-)

-----

0 points by fail 6165 days ago | link

: in a symbol name denotes composition.

WOW!!! FUNCTION COMPOSITION!!!!!

-----