This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
- # aleph (1)
- # announcements (9)
- # babashka (3)
- # beginners (200)
- # calva (22)
- # cider (74)
- # clojure (97)
- # clojure-dev (43)
- # clojure-europe (15)
- # clojure-italy (4)
- # clojure-nl (4)
- # clojure-sanfrancisco (2)
- # clojure-uk (103)
- # clojuredesign-podcast (2)
- # clojurescript (45)
- # core-async (5)
- # crux (18)
- # cursive (13)
- # datomic (42)
- # emacs (20)
- # fulcro (6)
- # graalvm (37)
- # jackdaw (10)
- # leiningen (7)
- # mid-cities-meetup (3)
- # off-topic (2)
- # pathom (1)
- # pedestal (3)
- # re-frame (6)
- # reagent (38)
- # reitit (5)
- # shadow-cljs (117)
- # spacemacs (1)
- # sql (1)
- # tools-deps (17)
- # vim (14)
After nearly a year off of Clojure I’d forgotten just how lovely it is to write :-)
@mattford certainly is. I think I've actually noticed messing around with paredit has the same kind of soothing tactile effect of a fidget spinner, aside from letting me write and edit from a higher level of abstraction.
Say you have a number of vars that you want to then use in a function, but only if each var is not null, like this:
(i.e., guarding the execution of the function if by some misconfiguration, the var is nil)
Yes, true, but those functions call into 3rd party libaries (with the var passed in) so I don't know how they will behave 🙂
Yes, as a rule, when I write my own functions, they tend to handle nil puning well 🙂
I actually found a neat trick from SICP where they talk about how to use
unless and lazy eval to deal with nils when doing something like dividing by your argument in a test form. Iirc, with lazy eval specifically,
unless basically isolates your nil case before it gets sent to the test.
Seems to me this is a bit like working with Maybe or Option types in typed FP, and AFAIK
some-> is the closest clojure analogue to
bind (or whatever it’s called), which you would use in those cases?
when and the
not-any? nil? redundant there, aside from wrapping everything in a single form for the when? If the
when form gets a test from
[foo bar baz] that evaluates to nil, it's going to not call the function that doesn't play well with nil.
My intention was that the body of the
when would contain the three functions in a context where they could all safely be used.
I assumed the when's were nested.
You'd basically give it something to do if it gets a nil for the argument of a function that would break if it tries to do something with the nil.
(fnil divide-5-by-arg "Can't divide five by nil") takes the fn in the first position, and any time you give it a nil, it will return the second arg.
So you'd just have a defn form with fnil holding the library function you're using, with the "exception" you want it to return if a nil worms its way in.
fnil still calls the fn doesn't it @edward.hughes1911 - so you get to supply default values, rather than swerve the application
Yeah it still calls it, but with whatever you supply as x. I was thinking of doing string stuff so my mind jumped to returning an error message or something.
how about this @dharrigan
(defmacro when-apply [f & args] `(let [v# [ [email protected] ]] (when (every? some? v#) (apply ~f v#))))
obvs it's also fine as a plain-old-fn
(defn when-apply [f & args] (when (every? some? args) (apply f args)))
Could you elaborate a bit more on the double evaluation? Everybody is always telling me if I can use a function I shouldn't use a macro, and I strongly believe we are missing out by not using macros more.. Your double evaluation of args argument is one no body has put on the table.. but now that you've mentioned I think I remember SICP referring to it.. (I'll need to brush it up).. Still I'd like to hear more on what you meant if you don't mind 🙂
E_INSUFFICIENT_COFFEE - it was just me being dim...
args isn't double-evaluated in the fn form...
hmm ok, no worries.. I thought I could have found a reason to push a little more for macros.. In any case, thanks 😄
Coming from Java, learning that code can be data and vice versa, and that that allows for marcos; and then everyone is hammering down that macros are filth.. One feels not encouraged to use them, not even to learn about them.. Luckily I hate all dogmatism and I like to challenge the status quo, so I don't let the hammering down stop me to try to find out what is this conspiracy against macros.. Specially when it feels like it is one of LISP's secret weapons.. Who in their sane mind would argue against a secret weapon? 🙂
a quick ag across our monorepo:
defmacro:73 ... we use them, but not that often - but one or two of those 73 macros i think will be the most common non-core constructs we use
Interestingly I am working on something a little similar @dharrigan. The new builder objects in v2 of the AWS API for Java are a little inconsistently weird. Mostly you can chain them with
(..) but it seems that some of them behave differently between setting a value = nil and not setting it at all. I sort of need a cond version of the
.. macro, but it doesn't exist, so I have been dusting off my macro-fu 🙂. Probably I could just drop the
.. and use whens but it's an exercise 🙂
You may, by the way, want
when-some unless you want the function not to be called when foo = false (as well as nil).
not saying it is any better than using
when but you can use
(and foo (fn-a foo)) exploiting the fact that
and returns the value of the last expression if all previous are truthy
i quite like using
and like that - but i also keep forgetting it's a thing and have rediscovered it several times
Hello everyone - how are you all doing? Sorry I've not been around; busy, busy, busy!
Just a reminder that ClojureBridge London content has some really small issues that could use your help to improve our content. Its a good way to practice contributing to an open source project (and good for your CV if you are job hunting) https://github.com/ClojureBridgeLondon/workshop-content-gitbook/issues Thank you
one obvious caveat with
and that if the param is boolean this is just not a good idea. applies to vanilla
when too tho
If you do multiple things in your when then and is not a perfect replacement either.
Anybody have a particularly elegant way of doing
distinct-with? As in, remove all values that return the same value when applied to a given function. Right now I am thinking
first on each val, but I am sure a greater magic probably exists.
(doc medley.core/dedupe-by) ------------------------- medley.core/dedupe-by ([f] [f coll]) Returns a lazy sequence of the elements of coll, removing any **consecutive** elements that return duplicate values when passed to a function f. => nil
(defn distinct-with [f coll] (vals (zipmap (map f coll) coll))) => #'user/distinct-with (distinct-with odd? (range 10)) => (8 9)
not particularly an improvement over
(defn distinct-with2 [f coll] (map first (vals (group-by f coll))))
@mattford It's still on my "to read list", but Ben Evans (aka Kittylyst) is my goto dude on all things JVM magic. His book, "Java: The legend" has a section on the JVM and its design including all the JIT and GC magic. It's probably worth a read. Some years back he was doing classroom courses on JVM internals which was pretty fascinating but that was in the jClarity days and I doubt he's still running them unfortunately.
@mattford There was a chap called Kirk Pepperdine he used to write on Java performance tuning, knew the JVM very very well. That long since I’ve looked for any of his stuff as that last stuff I did for JDJ was 2002/2003.
Sooo, your local supermarkets, can you still find food, tinned or otherwise, on the shelves?
Went and did a 150 pound shop at Morrison's the other day. Shelves are brimming as ever.
My survival plan is based primarily on chocolate eggs. Always plenty of those left.