Shorter form to check if some words are contained in a sentence?

57 views
Skip to first unread message

Thad Guidry

unread,
Jun 22, 2019, 5:32:41 PM6/22/19
to Clojure
Clojure is supported as an expression language in OpenRefine.  Where any cells value in OpenRefine's datagrid is just accessed by the name value.
Here's one of my cells value in OpenRefine...

"001","878","245","$c","CBS Barmarick Publications,","$c","Emerald"

and I am trying to see if that cells value contains all of these words...

(and (.contains value "CBS") (.contains value "Bar") (.contains value "cat"))

It works, but I am looking to Clojure experts to find out how to shorten this expression?

-Thad

Andy Fingerhut

unread,
Jun 22, 2019, 5:37:51 PM6/22/19
to clo...@googlegroups.com
I haven't counted characters, but this would certainly become relatively shorter the more substrings you check for.  It is shown in the context of a Clojure REPL.  I do not know whether OpenRefine might already do the require for you, or perhaps even (use 'clojure.string).

user=> (def value "Bar the cat from CBS")
#'user/value
user=> (require '[clojure.string :as str])
nil
user=> (every? #(str/includes? value %) ["CBS" "Bar" "cat"])
true

Andy


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@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+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/clojure/c37eb1b0-1557-486b-b95a-a5bc53b67b24%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andy Fingerhut

unread,
Jun 22, 2019, 5:38:56 PM6/22/19
to clo...@googlegroups.com
And, of course you can continue to use .contains:

(every? #(.contains value %) ["CBS" "Bar" "cat"])


Thad Guidry

unread,
Jun 22, 2019, 6:41:27 PM6/22/19
to clo...@googlegroups.com
Beautiful Andy!  Can you confirm that last syntax for me?
I see it as treating the anonymous function of #(.contains value %) as the predicate to check on every member of the vector?
But anonymous function expansion is what throws me off... I need layman's terms (grandma speak) to break down the logic there.  Specifically, there must be some wiring of the vector members to the % placeholder?
Ahhh! I guess that what does that actual wiring is the every? function ... "for every x in coll"
So this is just a simple every? invocation I guess.
And I assume that some would be like an or/coalesce, kinda in my usecase ?
(some #(.contains value %) ["CBS" "Bar" "cat"])

It worked by the way. See screenshot attached.

Still learning a few ropes on Clojure  (and I still can't get the stupid nrepl to initialize in my Visual Studio Code with leiningen  so that doesn't help my learning when I don't have a real dev environment that I can learn Clojure against.  Would love someone to help me with that)

Finally, how can I send you a tip or payment for this help?


Annotation 2019-06-22 130917.png

Andy Fingerhut

unread,
Jun 22, 2019, 7:06:40 PM6/22/19
to clo...@googlegroups.com
No tip or payment needed for me.  If at some point you desperately want to toss some money to someone for Clojure, one possibility is to make periodic small payments to Cognitect Labs, which also gives you access to a tool called REBL that you may find useful at some point if you continue using Clojure for development: https://www.patreon.com/cognitect/posts

The syntax #(.contains value %) creates an anonymous function that takes one argument, and the % represents where the argument is used in the function body.

If it makes more sense, you can replace it with (fn [x] (.contains value x)), which will do the same thing.

Yes, every? and some are similar to each other, with every? doing the AND of all individual function calls on the elements in the sequence, and some doing the OR, and not only doing the OR, but also returning the value of the first call that returns a logical true value (anything except nil or false).

There are Clojurians Slack and ZulipChat chat channels called #beginners that are also useful for getting started problems.

Andy


Reply all
Reply to author
Forward
0 new messages