This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-02-29
Channels
- # announcements (1)
- # babashka (5)
- # beginners (64)
- # cider (9)
- # circleci (11)
- # clojure (80)
- # clojure-uk (25)
- # clojured (6)
- # clojurescript (35)
- # css (1)
- # cursive (3)
- # datomic (6)
- # fulcro (10)
- # graphql (3)
- # kaocha (2)
- # leiningen (3)
- # lumo (3)
- # malli (10)
- # meander (24)
- # off-topic (17)
- # re-frame (10)
- # reagent (2)
- # ring (1)
- # shadow-cljs (27)
- # spacemacs (3)
- # tree-sitter (3)
You can take any (fn ...)
form and surround it with (memoize (fn ...))
, and I believe that should memoize it.
If you prefer a different memoizing library other than the function Clojure.core/memoize
, e.g. core.memoize, they should have a function that takes a function to be memoized as an argument, and returns a memoized version of that function. That should work regardless of whether that function has a name or not.
The function is recursive and without a def
, clj-kondo is complaining. I took its word but now that I try it, it actually works!
No worries. You may want to create an issue for clj-kondo if you think it is complaining about something that it should not.
Actually, clj-kondo is right. The following does not work,
(let [f (memoize
(fn [x]
(if (pos? x)
(+ 1 (f (dec x)))
0)))]
(f 10))
;; => Unable to resolve symbol: f in this context
Previously, I was testing things with a running REPL, and it may have taken the symbol from the previous eval and worked. A freshly booted REPL would have thrown error.
(let [f (memoize
(fn f [x]
(prn "computing: " x)
(if (neg? x)
0
(+ (f (- x 1)) (f (- x 2))))))]
(f 3))
That did not work for me. This does memoization properly,
(def f
(memoize
(fn [x]
(prn "Called with" x)
(if (pos? x)
(+ 1 (f (dec x)))
0))))
This doesn't,
(def f
(memoize
(fn f [x] ; Notice the "f"
(prn "Called with" x)
(if (pos? x)
(+ 1 (f (dec x)))
0))))
my example was showing it does not memoize correctly. a hack is the following:
(let [g (memoize
(fn g [f x]
(prn "computing: " x)
(if (neg? x)
0
(+ x (f f (- x 1)) (f f (- x 2))))))]
(g g 3))
Kind of. It was a one-off function that I did not want to put in the namespace. But I'll settle for def
.
reworking 4clojure problems is a great way of being reminded how much clojure.core has grown since 1.4
@penryu what am as->
one googles deep... http://clojuredocs.org/clojure.core/as-%3E
I suspect penryu is commenting that 4clojure still uses Clojure 1.4 to evaluate code you type into it for solving problems, thus anything added to Clojure's core library after that is not available when solving those problems.
Yeah, I resumed 4clojure from several years ago. Was surprised when it a solution would work just fine locally, but fail with resolution error in web.
@penryu You can always run a Clojure 1.4 REPL locally for developing your solutions 🙂
@seancorfield Yeah, I almost did, too. But I think I like being reminded of the differences.
True, that can be a good learning process. Easy to run 1.4 via the CLI (if you have my dot-clojure file in place):
$ clj -A:1.4
Downloading: org/clojure/clojure/1.4.0/clojure-1.4.0.jar from central
Clojure 1.4.0
user=> (clojure-version)
"1.4.0"
user=>
I have incorporated several entries from your ~/.clojure/deps.edn
. Thanks for sharing it!
As a library author/maintainer, it's sometimes very frustrating when you have to support an older version of Clojure and can't use some new cool language feature! At least Clojure developers migrate to newer versions fairly quickly.
Almost 90% of developers are already using 1.10, only 25% are still using 1.9, 10% still using 1.8. Only 1.4% are using earlier versions.
Clojure 1.4 is one of the 1.4%, and still the one 4Clojure! (sorry, couldn't resist)
We got started at work on an alpha build of 1.3 and in nine years there have been almost no breaking changes which is a great accolade for Clojure's stability! The worst cases have been new core functions added that collided with some of our own functions -- which was always easy to fix.
Same! When I came back to Clojure after 4-5 years, everything I'd written worked without a change. Yes, at most an extra :as
in the require
.
That at least means libraries can drop support for older versions relatively quickly.
Would anyone be interested in starting a study/accountability group? I'm a javascript/react developer but looking to build up a Clojure portfolio. We would meet a minimum of once a week, either in person but most likely online.
sounds like a group reinforced study group, where members hold each other accountable for doing the homework?
I'm in, I'm a JavaScript/react developer too And I'm totally lost in clojure/script world Let's just do it
@UQRGVS3MG awesome! Lets do it! I'll dm you.
@seancorfield I also considered running the 4clojure app from git repo, after bumping the project.clj to 1.10. But I decided, if I solve it one way locally, using the 4clojure test cases as clojure.test cases, then have to solve it again when I find out update
isn't available, it's an extra layer of special restrictions to learn from.
When getting a value from a Clojure hash-map, Is it possible to use a function as the not-found value and only have that function called if the key in the hash-map is not found?
(get {:a 1 :b 2} :b (throw (Throwable.))
The throw
expression is evaluate and result returned even though there is a :b
key in the hash-map. I assume this is similar to passing an expression as an argument to a function definition, it is evaluated before the rest of the function definition. I could of course wrap the above in a let and put a condition on the result that would trigger the throw
expression. Any suggestions are appreciate.(or (:b {:a 1 :b :2}) (throw (Throwable.))
🙂 thats so obvious now I see it. Thank you
Which works as long as the value you get out of the map cannot be nil
nor false
. If it can be, you'll need something else, perhaps using find
or contains?
I was just thinking about this, and using contains?
. But it feels like a simpler/more idiomatic solution might be a non-falsy not-found value, as in:
(case ({:a 1 :b false :c nil} v :not-found)
:not-found (throw (Throwable.))
false ...
nil ...
(handle-inexplicit-but-present-value))
How can this be improved?Depends upon what you consider an improvement, e.g. number of characters to type, performance, whether it is reasonable to define a new macro for it, etc.
For conditional execution of code, there definitely needs to be a macro involved, either a built in one like if
case
cond
or
, or one you define yourself in terms of one of those.
Right. I guess if you just want to know if it's present in a map before dispatching the value off elsewhere, contains?
has the most clarity.
so a "simpler" (than my) solution might just be:
(if (contains? m v) (do-stuff (m v))
(throw (Throwable.))
Thanks, @U0CMVHBL2!
If you are trying to create something reusable that is optimized for performance here, I suspect using clojure.core/find
is worth experimenting with, as a single call to that function returns a distinguishable value if the key is not present, like contains?
does, but it also returns the key/value pair if the key is present.
Using contains?
followed by another call to get the corresponding value if the key is present, it takes both calls to get the value when the key is present.
Using find
you can check both of those things with a single lookup in the map
I have not measured performance of find
vs. the contains?
followed by regular map lookup to see which is faster myself, though.
In my case, if the map doesn't have a key, or the key has a value other than a number, then an error should be thrown. So or seems good. I want to have the number as the result, or throw an error.
As @U0CMVHBL2 mentioned, or
works as long as the values of the map are never nil
or false
.
If the values might be falsy, you can safely combine or
with find
(also as Andy mentioned above), since map entries (vector tuples) are never falsy.
with the down side that unless you wrap it in a macro to help do some of this stuff, using or
with find
will not return only the value corresponding with the key, but the key/value pair.