Arc Forumnew | comments | leaders | submitlogin
Proposing a few extended functions for arc (remove & genstring)...
2 points by thaddeus 5855 days ago | 6 comments
Just and idea - how about including an extended version of remove in arc ?

I am sure you guys can work it much better, but I am finding my version really handy:

  (def remove args
  	  (with (spec (last args)
	        (val . rest)(rem (last args) args))
  	    (case (type spec) 
	          cons   (if (no rest)(rem val spec)(eval `(remove ,@rest ',(rem val spec))))
                  string (if (no rest)(subst "" val spec)(eval `(remove ,@rest ',(subst "" val spec)))))))
(on a side note: it really bugs me that 'case' doesn't handle types the way other arc functions do - ie shouldn't the type be quoted ? (case (type something) 'cons ... instead of cons ?)

anyway back to remove - which allows me to rem multiple items from a list without having to -> (rem this (rem that (from list)))

so instead I can:

  arc> (remove "e" "u" "s" '( "T" "h" "a" "d" "d" "e" "u" "s")) 
       ("T" "h" "a" "d" "d")
and also I use genstring a lot which allows me to coerce lists into strings:

   (def genstring (options (o endit ""))
     (string (trim (if (no options)
        nil
        (let (val . rest) options
           (string val " " (genstring rest)))) 'both) endit))
and so....

  arc> (genstring '("T" "h" "a" "d" "d"))
       "T h a d d"
   
then I can go back and use 'remove' on strings as well:

  arc> (remove " " "T h a d d")
       "Thadd"
so to wrap it up you could just do:

   arc> (remove " " (genstring (remove "e" "u" "s" '( "T" "h" "a" "d" "d" "e" "u" "s"))))  
        "Thadd"
This one simple example could probably be coded other ways, but I'm finding I use these two functions alot!

P.S. Thaddeus is actually my dogs name - lol :)

T.

[EDIT] - one could just mod genstring to strip out the spaces too, but I tried to keep the genstring function generic enough to reuse in other situation where the space might be required.



3 points by rntz 5854 days ago | link

If you use the latest arc3.tar (unfortunately I haven't gotten around to merging these changes into the arc3.master branch on anarki), 'string will concatenate lists of strings (or indeed anything coercable to string) into one string, with no spaces, recursively:

    arc> (string '("foo" bar (2 3) "baz")
    "foobar23baz"
Also, 'rem is capable of removing characters from strings.

    arc> (rem #\  "foo bar")
    "foobar"

-----

2 points by fallintothis 5855 days ago | link

In case you didn't know, 'rem can also take a function predicate to test items against. For example,

  arc> (rem even '(8 6 7 5 3 0 9))
  (7 5 3 9)
So your example becomes:

  (apply string (rem [in _ "e" "u" "s"] '("T" "h" "a" "d" "d" "e" "u" "s")))

-----

2 points by thaddeus 5855 days ago | link

lol... that would have been nice to know :) thnx

-----

2 points by pg 5854 days ago | link

I don't think you need genstring. You can duplicate your example with +:

    > (apply + '("T" "h" "a" "d" "d")) 
    "Thadd"
or with string if you don't know what the first arg type will be:

    > (apply string '(t "h" "a" "d" "d")) 
    "thadd"

-----

1 point by thaddeus 5854 days ago | link

There are sooo many uses for these generic functions, I just need to get up to speed with all the possibilities...

thnx T.

-----

1 point by shader 5855 days ago | link

I think the reason that case expects just cons instead of 'cons is that it automatically quotes the case arguments. It's been somewhat annoying for me too, because you can't use expressions for cases - unless you're matching against a list that contains code. ^^

-----