On Mon, Nov 9, 2009 at 5:50 PM, Alex Osborne <a...@meshy.org> wrote: > Mark Engelberg wrote: > > 2009/11/9 Tiago Antão <tiagoan...@gmail.com>: > >> What is the rationale for even? and contains? having different > >> behaviors for the exact same error (ie, one throws the other works > >> fine and just returns false on a type error)? > > > I imagine the rationale is efficiency. > > Here's the function from clojure/lang/RT.java: > > static public Object contains(Object coll, Object key){ > if(coll == null) > return F; > else if(coll instanceof Associative) > return ((Associative) coll).containsKey(key) ? T : F; > else if(coll instanceof IPersistentSet) > return ((IPersistentSet) coll).contains(key) ? T : F; > else if(coll instanceof Map) { > Map m = (Map) coll; > return m.containsKey(key) ? T : F; > } > else if(key instanceof Number && (coll instanceof String || > coll.getClass().isArray())) { > int n = ((Number) key).intValue(); > return n >= 0 && n < count(coll); > } > return F; > } >
Why not: static public Object contains(Object coll, Object key){ if(coll == null) return F; else if(coll instanceof Map) return ((Map) coll).containsKey(key) ? T : F; else if(coll instanceof IPersistentSet) return ((IPersistentSet) coll).contains(key) ? T : F; else if(key instanceof Number && (coll instanceof String || coll.getClass().isArray())) { int n = ((Number) key).intValue(); return n >= 0 && n < count(coll); } return F; } instead? Oh, and who else finds it odd that the above implies that user=> (contains? [1 2 3] 0.71) true when in actuality user=> (contains? [1 2 3] 0.71) false despite user=> (.intValue 0.71) 0 user=> (contains? [1 2 3] 0) true ? (I suppose T and F are static fields holding java.lang.Booleans, and this code was written pre-autoboxing?) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---