[v1] Searching for a value that exists in a provided list of strings

I’m having trouble with searching for a value that exists in a provided list.

{
:find [?id ?found-employee-id]
:where [
[?e :xt/id ?id]
[?e :val ?val]
[(get-in ?val [:id]) ?found-employee-id]
[(contains? [“11111”] ?found-employee-id)]
]
}

I’ve tried

  • [(contains? [“11111”] ?found-employee-id)]
  • [(.contains [“11111”] ?found-employee-id)]
  • [(java.lang.String/contains [“11111”] ?found-employee-id)]
  • [(set [“11111”]) ?found-employee-id]

Can someone point me in the correct direction?

Hey @tempire unfortunately a vector isn’t supported as an argument like this because it would conflict with the wider use of vectors as the representation for tuples (IIRC), but a set literal should work:

[(contains? #{“11111”} ?found-employee-id)]

Or you can create a hash-set in a separate clause and reference that using another logic var:

[(hash-set “11111”) ?my-set]
[(contains? ?my-set ?found-employee-id)]

(but again using the vector constructor here wouldn’t work - the engine simply doesn’t allow you to pass top-level vectors around)

Hope that helps!

Jeremy

That helps, thank you. If I pass in an array of strings via in-json-args, I’m able to use:

[(set ?employee-id) ?set]
[(contains? ?set ?found-employee-id)]

Followup questions:

  1. I’m a little confused as to the point of :in [[?employee-id ...]]
    assuming in-json-args is passing in an array. Shouldn’t contains? be able to handle that?

  2. If I use:

[(set ?employee-id) ?set]
[(contains? ?set ?found-employee-id)]

It’s fine, unless ?employee-id is an empty array, in which case, the query stops executing at that point, presumably because set resolves to falsy. How can I assign a falsy result without stopping the query?

–EDIT–

I misunderstood the problem. Solution:

    [(set ?employee-id) ?set]
    (or
      [(empty? ?set)]
      [(contains? ?set ?found-employee-id)]
    )
1 Like

Bear in mind that in Clojure:

user=> (contains? [10 11 12 13] 2)
true

(because vectors are associative on their indices and that is what contains? checks)

1 Like

Aha, of course, I forgot about that! Thanks for chiming in. There are more examples on that here