Issue with Clojure 'contains'

前端 未结 3 1090
心在旅途
心在旅途 2021-01-11 19:21

I am going through some Clojure tutorials using Closure Box, and entered the following code:

user> (def stooges (vector \"Moe\" \"Larry\" \"Curly\"))
#\'u         


        
3条回答
  •  执念已碎
    2021-01-11 20:23

    A vector is similar to an array. contains? returns true if the key exists in the collection. You should be looking for the "key/index" 0, 1 or 2

    user=> (def stooges (vector "Moe" "Larry" "Curly"))
    #'user/stooges
    user=> (contains? stooges 1)
    true
    user=> (contains? stooges 5)    
    false
    

    If you were using a hash...

    user=> (def stooges {:moe "Moe" :larry "Larry" :curly "Curly"})
    #'user/stooges
    user=> (contains? stooges :moe)
    true
    user=> (contains? stooges :foo)
    false
    

    As mikera suggests, you probably want something like clojure.core/some

提交回复
热议问题