Thanks for your help :)
Bye
Alfredo

On Apr 8, 3:10 pm, Meikel Brandmeyer <m...@kotka.de> wrote:
> Hi,
>
> On 8 Apr., 15:02, Alfredo <alfredo.dinap...@gmail.com> wrote:
>
> > I know that it's a user's choice, but I'm wondering if it's possibile
> > to offer a "+" function that it's extended to other datatypes -with or
> > without protocols - and still able to be applied to Numbers :)
>
> Yes. It is. Just provide it in your namespace. You showed already how.
>
> (ns your.library
>   (:refer-clojure :rename {+ core-+})
>   (:import
>     clojure.lang.IPersistentVector
>     clojure.lang.ISeq))
>
> (defprotocol Addable
>  (+ [t1 t2]))
>
> (extend-protocol Addable
>   String
>   (+ [s1 s2] (str s1 s2))
>   IPersistentVector
>   (+ [v1 v2] (into v1 v2))
>   ISeq
>   (+ [s1 s2] (concat s1 s2))
>   Number
>   (+ [n1 n2] (core-+ n1 n2)))
>
> This gives:
> your.library=> (+ 1 2)
> 3
> your.library=> (+ "a" "b")
> "ab"
> your.library=> (+ [1 2] [3 4])
> [1 2 3 4]
> your.library=> (+ (list 1 2) (list 3 4))
> (1 2 3 4)
>
> I'm not sure it's advisable, though. I think adding numbers has
> nothing to with catenating strings. You should probably choose a name
> like append, concat or into.
>
> Sincerely
> Meikel

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to