an epiphany finally!!!
while writing my response previously I realised that adding 2 numbers of
different type usuallly returns either a double or a long. That was the
source of confusion... I need to coerce whatever number my fn is
returning to the type the array is expecting (duh!)
I feel properly stupid now!
thanks all of you who bothered responding...
Jim
On 13/06/13 21:03, Jim - FooBar(); wrote:
I wish I could...if I do that I can't call amap on any other primitive
array but the first extended - I get the exception I posted earlier
and it has nothing to do with type hinting the array. I'm type-hinting
it in both cases:
IllegalArgumentException No matching method found: aset
clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:80)
ok it's starting t get annoying so let's work with a concrete example:
(defprotocol FOO
(bar [this a] [this a b]))
(extend-protocol FOO
(Class/forName "[D")
(bar
([this a]
(amap ^doubles this idx ret (a (aget ^doubles this idx) 10.0)))
([this a b]
(bar this #(apply a %1 (list %2 b))))) )
user=> (bar (double-array 1) +)
#<double[] [D@6239ae30>
user=> (seq *1)
(10.0)
Right, all is well! let's carry on...I'm going to copy-paste the
code-block above only changing the 'doubles'->floats
(extend-protocol FOO
(Class/forName "[F")
(bar
([this a]
(amap ^floats this idx ret (a (aget ^floats this idx) (float 10))))
([this a b]
(bar this #(apply a %1 (list %2 b))))) )
user=> (bar (float-array 1) +)
IllegalArgumentException No matching method found: aset
clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:80)
any other type I try from now onwards will fail with the same
exception. Could it be because Clojure changes the types internally to
longs?
I don't understand!forgive me but I'm a bit annoyed with this...
Jim
On 13/06/13 19:02, Aaron Cohen wrote:
On Thu, Jun 13, 2013 at 1:52 PM, Jim - FooBar();
<jimpil1...@gmail.com <mailto:jimpil1...@gmail.com>> wrote:
or can you perhaps show an example of successfully extending any
protocol to at least 2 primitive array types?
You can call extend-protocol several times:
user=> (defprotocol A (foo [a b]))
A
user=> (extend-protocol A (Class/forName "[D") (foo [a b] [a b]))
nil
user=> (extend-protocol A (Class/forName "[F") (foo [a b] [a b]))
nil
user=> (foo (double-array [1 2 3]) 1)
[#<double[] [D@5a12d46c> 1]
user=> (foo (float-array [1 2 3]) 1)
[#<float[] [F@5e310a07> 1]
--
--
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
---
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it,
send an email to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
--
--
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
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.