r/Clojure • u/dustingetz • Apr 03 '18
What little clojure tricks did you not realize for years?
58
u/alexdmiller Apr 03 '18
#_ is a reader form to skip the next read expression. And they "stack" so you can do:
(let [#_#_ a 1
b 2] ...)to knock out two expressions. Lots of other options in that simple example but when it's a multi-line expression, that helps a lot.
5
16
8
5
u/chayatoure Apr 03 '18
I'm a bit confused when you would use this? During dev to prevent execution of code you want to skip?
7
u/will_i_be_pretty Apr 04 '18
Yes. It's basically just like commenting out a bit of code.
The crucial bit where it's uniquely useful versus a semi-colon though, is that
#_eats the entire next expression, and that includes lists. So you can comment out an entire block in one go by putting it ahead of the opening parens, and you can stick it anywhere in your code without having to worry about the semicolon causing dangling or missing parens,6
2
u/aberrant Apr 04 '18
Yes. Notably, (comment ...) produces nil, while #_ skips the form (producing nothing). The former may produce unwanted NPEs when, for example, debugging compojure routes.
1
1
4
3
u/TheLastSock Apr 03 '18
Can you explain why this would be useful? Everyone's mind is blown and i'm trying to come up with a usecase.
3
1
u/Someuser77 Apr 05 '18
Stacking is highly counterintuitive to me. I would have expected instead ## to be a no-op, as the first symbol would tell the reader to ignore the next symbol. But, neat to know!
11
Apr 06 '18
(def separate (juxt filter remove))
(separate pos? [1 2 3 -1 0 4 -2])
=> [(1 2 3 4) (-1 0 -2)]Combined with destructuring, I find I use it often with ex-data for dealing with sequences / channels that may contain ex-info / errors.
(let [[errors results] (separate ex-data [...])]
(...))*I should add I came across this on the mailing list via Alex Miller years ago.
3
1
u/wowomgspin Apr 11 '18
Nice one! In my works I did it like
(def separate (comp vals group-by))1
Apr 11 '18
That's an interesting approach, only thing I'd say is you might have unpredictable result order because maps are not ordered in Clojure (so the call to vals will return results with no guarantee of which is first).
5
u/dustingetz Apr 03 '18 edited Apr 03 '18
Load a resource at compile time from cljc (Note line 2 makes portable)
(ns contrib.template
#?(:cljs (:require-macros [contrib.template :refer [load-resource]])
:clj (:require [clojure.java.io :as io])))
#?(:clj
(defmacro load-resource [filename]
(-> (io/resource filename) slurp)))1
u/joinr Apr 04 '18
Is there a cookbook of cljc portability examples / concerns? Are there any tools that can help analyze and identify non-portable code? I'm thinking of migrating a large codebase to cljc (targeting jvm/js), and I know there are likely dozens of little minefields like this...
1
u/dustingetz Apr 04 '18 edited Apr 04 '18
Not very many people have done serious portability work on large projects to my knowledge.
Inconsistent impls of
hashwas a good one that comes to mindreader extensions, edn extensions, deftype
The obvious stuff like java.lang.Math, js/Number, java.lang.Character, java.net.URI etc
If you run into trouble you should retain my partner, Hyperfiddle runs fully in node, jvm and browser, my partner got that all to work, it was a pretty serious undertaking iirc https://github.com/hyperfiddle/hyperfiddle
21
Apr 03 '18
doto is not just for interop!
(doto 'my.test-ns require in-ns clojure.test/run-tests)
8
1
2
u/joinr Apr 03 '18 edited Apr 03 '18
Using vector + map-indexed for dumb indexed sequence creation/manipulation (this crops up quite a bit for some reason during data transformation):
(for [[idx v] (map-indexed vector [:some :elements :of :seq])
{:idx idx :v v})or dumping into a convenient indexed map (feel free to reverse the keys/vals ala #(vector %2 %1)):
(->> [:some :elements :of :seq]
(map-indexed vector)
(into {}))2
u/dustingetz Apr 03 '18
Vectors are already associative by index, is there another reason you'd want to do this?
2
u/joinr Apr 04 '18 edited Apr 04 '18
I've run into a few cases where having a flexible, sparse, unordered container is useful. In fact, I typically don't use vectors as associative containers (via their assocN implementation) unless the problem calls for a dense representation (for me, that's atypical).
Aside from the trivial example, adding an index field / computing a field for sequences of maps is somewhat common. Replace the vector of keywords with a sequence of maps; i.e. you have more than one key. Downstream, we may want to facilitate sorting using a compound key, where the original positional index is a criteria (perhaps a tie-breaker). Or we may merely want to annotate the positional index as part of data when serializing to output like tsv. Or we may have additional transforms in the pipeline (like computing additional fields) that are easily expressed if an :idx value is assumed to exist.
For the second case I offered, it's more typical to compute a map of v->idx (or have a bidirectional mapping of v->idx, idx->v) for certain lookup scenarios, like:
(->> (for [[idx v] (map-indexed vector [:some :elements :of :seq])] [v idx]) (into {}))Assuming the vs are unique to begin with, you get a positional lookup table from v->idx; assuming idx has meaning (like depth, hierarchy, or some other notion of weight), this can convey useful information. With vectors, you'd need to search instead of a single hash-based get. Depending on use case and size of data, map-based lookup table may be superior.
Also, if we are performing something where we'd like to "modify" the lookup to table, to represent some progress in a computation, we can dissoc keys randomly from a sparse hashmap (happens in search / graph algos). Vectors don't support dissoc; you can implement it by building new vectors, but that requires additional considerations (namely efficiency). I typically leverage the sparse map representation (and ability to dissoc) more than a dense vector representation. It certainly depends on the problem though. If it makes sense to use a fairly static, dense int-map, and perhaps provide ordered traversal, then vectors would be better (or maybe Tellman's intmap). Luckily, coercing between representations is fairly straightforward :)
16
u/jeremyrsellars Apr 03 '18
In REPL-based development, it can be useful to def deep inside a fn or other form to capture the last value. (defn awesome [x] (def debug-last-input x) ; your decomplected goodness here (throw (Exception. "some crazy bug that is hard to reproduce"))
Then, when some other code evaluates your awesome function, the part of interest is captured as an object, not just dumped to the screen as in prn.
I first learned this from Alex Miller at the KCDC.
11
u/gshayban Apr 03 '18
This will get substantially better with tap> in 1.10. Stay tuned
4
1
0
2
u/davedrowsy Apr 03 '18
Very sensible -- I'll give that a try the next time I'm debugging. I litter my code with
prns far too often!2
u/Borkdude Apr 05 '18
I called this technique 'inline def' here: https://blog.michielborkent.nl/blog/2017/05/25/inline-def-debugging/
3
u/kickopotomus Apr 03 '18
(defn awesome [x] (def debug-last-input x) ; your decomplected goodness here (throw (Exception. "some crazy bug that is hard to reproduce"))5
u/joinr Apr 04 '18
Your code reminds me of another one, I totally missed apparently years back.
I still write variants of (throw (Exception. ... ) ) to this day, but apparently 'ex-info' has been in clojure since 1.4 !
(throw (ex-info "some crazy bug that is hard to reproduce" {}))ex-info is portable across cljs apparently. I'm still rewiring my brain to use it...
16
u/joinr Apr 03 '18
Using some-> for interop (kind of like doto or ..) is really useful since many operations return null, and if you want to do some deeply-nested effectful interop stuff, you can short-circuit without writing a bunch of conditions.
I picked up on this from the NightCode source, and have adopted it...
(some-> pane .getParent .getChildren (.remove pane)))1
Apr 04 '18
Cool! Didn't realize this is just a safe navigation operator. Just updated the cljs api doc to reflect this and added this example: http://cljs.github.io/api/cljs.core/some-GT
1
u/joinr Apr 04 '18
Wow, that goes to show my ignorance of OOP langs. I had no idea that operator existed; makes sense.
I always thought of it more like the Maybe (or Option) monad since failure -> short-circuit.
Thanks for updating docs.
0
Apr 03 '18
[deleted]
3
u/joinr Apr 03 '18
some-> showed up in core at some point, as well as some->>, although some say the author is unknown, and may play banjo....
16
u/jwhitlark Apr 03 '18
Having a user.clj at the root of source, set up for debugging and only included in dev code, to speed up testing/debugging/exploring/experimenting.
That way, just opening a REPL has all the tools and aliases already set up.
2
u/billrobertson42 Apr 08 '18
That's neat. How do you only include it in dev code?
3
u/jwhitlark Apr 08 '18
Put it in a dev or debug dir, instead of src. The use a profile to only include it when needed. Like test. I'm on mobile right now, but I'll try to put the details in later.
1
14
u/joinr Apr 03 '18 edited Apr 03 '18
Work "around" Java using interop. I found this invaluable when messing with JFreeChart and Piccolo2D, since the designers decided I shouldn't be able to read (let alone write) certain innocent fields (because according to OOP Best Practices, we should put our data in the equivalent of empty mason jars, buried in the backyard, with locations marked only on One-Eyed-Willy's treasure map).
led me to the simple extended idea of unpacking getters and setters as functions in a [convenient namespace via macro and reflection] (https://github.com/joinr/spork/blob/master/src/spork/util/interop.clj) so that I could peer inside objects (or access...although I prefer not to) without having to done the ceremonial robes of a Java Priest. This is all type-hinted, so no per-call reflection costs (if that matters for use-case). So....using Clojure to make Java more data-friendly/open is a neat trick to me. Although it's a trick for consenting adults though if you decide to write to said fields...(reading is still innocent and pure!)
(defmacro expose-private-accessors
"Given a java class, klass, and a seq of symbols defining assumably private
fields in said class, returns a map of keyworded getters and setters, that
associate with functions that operate on said object to access the fields.
Yields public function definitions of the form set-fieldname, get-fieldname
for each field."
[klass & fields]
(let [m (with-meta (gensym "m" ) {:tag 'java.lang.reflect.Field})
o (with-meta (gensym "o") {:tag klass})
fld-get-sets (vec (for [f fields]
[(str f)
(symbol (str "get-" f))
(symbol (str "set-" f))]))]
`(do ~@
(for [[fld getter setter] fld-get-sets]
`(let [~m (.getDeclaredField ~klass ~fld)
~'_ (.setAccessible ~m true)]
(defn ~getter [~o] (.get ~m ~o))
(defn ~setter [~o v#] (do (.set ~m ~o v#) ~o)))))))3
9
u/charlesHD Apr 03 '18
Using when with some. Sounds obvious now, but I always found some kinda lame before, because it returns the predicate value.
Now something like that seems like plain english :
(some #(when (zero? (rem % 7)) %) (range 50 100))Generally speaking there is many place where you can just use when in clojure and everything goes well. I find that's a neat trick.
4
u/yogthos Apr 03 '18
on a related note,
keepis also very nice for filtering and massaging data in one step:(keep #(when (zero? (rem % 7)) (str "value: " %)) (range 50 100))0
Apr 03 '18
[deleted]
1
u/Daegs Apr 03 '18
find-firstinmedley.coreor just first-filter is preference.using some and when will trip a lot of developers up
4
Apr 07 '18
Using a set as a function, particularly with filter.
(filter (comp #{"one" "three"} :id)
[{:id "one"}
{:id "two"}
{:id "three"}
{:id "four"}])
=> ({:id "one"} {:id "three"})3
u/joinr Apr 03 '18
Normalize strings, symbols, keywords (and anything else clojure.lang.Named) using 'name', named-> keywords:
=>(map (comp keyword name) [:hello 'world "!"])
(:hello :world :!)3
u/malcolmsparks Dec 22 '22
I don't use condp much, but I do love the way you can combine it with regular expressions:
(let [input "Alexa, set a timer for 10 seconds"]
(condp re-matches input
#"Alexa, turn on (.*)" :>> (fn [[_ thing]] [:turn-on thing])
#"Alexa, turn off (.*)" :>> (fn [[_ thing]] [:turn-off thing])
#"Alexa, set a timer for (.*) (minutes|seconds)"
:>> (fn [[_ amount units]] [:start-timer amount units])
#"Alexa, what's the weather like (.*)"
:>> (fn [[_ when]]
(case when
"today" [:weather-today]
"tomorrow" [:weather-tomorrow]))))2
Apr 04 '18
Abusing the reader. say you're read a list of values from a file. if they are delimited by commas or white space you can turn it into a list or vector like so
(read-string (str "(" (slurp "file.dat") ")"))
30
u/dustingetz Apr 03 '18 edited Apr 03 '18
Embed as-> inside ->
(edit: better example)