Skip to content

Instantly share code, notes, and snippets.

@cgrand
Last active November 3, 2021 22:31
Show Gist options
  • Save cgrand/dc3a7d14575d8ad8c071f78c9ca5d6dd to your computer and use it in GitHub Desktop.
Save cgrand/dc3a7d14575d8ad8c071f78c9ca5d6dd to your computer and use it in GitHub Desktop.
A usage of `doto` that I overlooked for years
;; let's say you want to perform an action only when a state is true and returns true/false depending on the action success
;; typically I would write
(if @open ; check state
(do
(.put q bytes) ; action
true)
false)
;; or
(let [v @state]
(when v (.put q bytes))
v)
;; turns out one can just write:
(doto @state (when (.put q bytes)))
@piotr-yuxuan
Copy link

Should it be added as a rule to https://github.com/jonase/kibit? :-)

@jgomo3
Copy link

jgomo3 commented Nov 3, 2021

It doesn't seem right.

I mean, you can do it but, should you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment