This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
- # beginners (41)
- # boot (38)
- # cider (17)
- # cljs-dev (52)
- # cljsjs (3)
- # clojure (200)
- # clojure-italy (8)
- # clojure-russia (50)
- # clojure-spec (28)
- # clojure-uk (45)
- # clojurescript (28)
- # core-async (9)
- # core-matrix (2)
- # cursive (16)
- # datascript (15)
- # datomic (50)
- # dirac (5)
- # emacs (20)
- # figwheel (8)
- # flambo (2)
- # hoplon (10)
- # incanter (1)
- # jobs (1)
- # leiningen (2)
- # lumo (26)
- # mount (171)
- # off-topic (22)
- # om (54)
- # onyx (2)
- # pedestal (27)
- # re-frame (10)
- # reagent (12)
- # ring (27)
- # ring-swagger (3)
- # rum (2)
- # slack-help (1)
- # spacemacs (134)
- # specter (6)
- # sql (15)
- # testing (20)
- # uncomplicate (5)
- # unrepl (49)
- # untangled (9)
- # yada (29)
Hey guys, what would be an idiomatic clojure approach to implement a
prev function where given a vector and an element, it returns the next/prev element if it exists.
(next [:a :b :c :d] :a) ;; => :b (next [:a :b :c :d] :b) ;; => :c (next [:a :b :c :d] :d) ;; => nil (next [:a :b :c :d] :foo) ;; => nil (prev [:a :b :c :d] :c) ;; => :b (prev [:a :b :c :d] :b) ;; => :a (prev [:a :b :c :d] :a) ;; => nil
(defn next [s k] (k (zipmap s (rest s)))) and
(defn prev [s k] (k (zipmap (rest s) s)))
boot.user=> (defn prev [s k] (k (zipmap (rest s) s))) #'boot.user/prev boot.user=> (defn next [s k] (k (zipmap s (rest s)))) WARNING: next already refers to: #'clojure.core/next in namespace: boot.user, being replaced by: #'boot.user/next #'boot.user/next boot.user=> (next [:a :b :c :d] :a) ;; => :b :b boot.user=> (next [:a :b :c :d] :b) ;; => :c :c boot.user=> (next [:a :b :c :d] :d) ;; => nil nil boot.user=> (next [:a :b :c :d] :foo) ;; => nil nil boot.user=> (prev [:a :b :c :d] :c) ;; => :b :b boot.user=> (prev [:a :b :c :d] :b) ;; => :a :a boot.user=> (prev [:a :b :c :d] :a) ;; => nil nil boot.user=>
I have a question regarding recursion. Say I have something that looks something like
(fn [arg1 arg2 col] (loop [element (dec (count col)), col col] (if (>= element 0) (print arg1 arg2) (recur (dec element) (pop col))))) my question is, because loop has 2 arguments, recur expects 2 arguments, but if I'm say printing or otherwise interacting with arg1 and arg2 which are defined in the original function call but not directly in the loop, is my recursive function still using tail-recursion? Or will this overflow my stack if my initial collection is large enough?
The same is true of
defn/`recur` — the Clojure compiler simply won’t accept
recur in a position where it can’t compile it as a loop, i.e., in a non-tail-recursion position.
oh okay, so I don't necessarily need loop/recur in my function. I didn't realize fn/defn worked with recur
Of course, for your function as written, there are many better ways to write that but I’m sure you mean it as just an example, not a real function? 🙂
No my function doesn't look like that, but it was the best way I could "summarize" my question I guess
(for code that is evaluated purely for side-effects — such as printing —
dotimes or even
run! are more likely what you want)
Also, when you have a collection (or sequence), the idiomatic solution is rarely going to be
loop/`recur` (sure, it will be sometimes, especially if you need to “loop” over several unrelated things at once or the “recursion” is conditional and does different things will each of those… but even there
reduce might be better).
(is (= expected-long-string (generate-long-string))) in a
The string contains many lines separated by newlines.
If the test fails, clojure.test displays them as a single long line comparison failed in the report, the newlines are shown as
Is there a function that will show me some kind of diff of the two strings?
Someone pointed me to
pjstadig.humane-test-output, but unfortunately it is not compatible with cider > 0.10, and I'm using version 0.14.
It would be much more easy to spot the difference in the actual result compared to the generated string that way.
I know you got an answer already for this, but another simple option is
lein difftest which diffs the expected and found data and shows that
@vinai using Expectations test library could help, but I'm not sure if that's an option for you.
The integration story with Cider is not completely smooth, but it should work if you use
@jumar Thanks for the recommendation, I'm investigating one other option, but if that doesn't turn out I'll try Expectations.
somebody successfully implement this in hiccup? because in my code.. isn’t works.. can’t manage the
@sb try last links https://github.com/weavejester/hiccup/wiki/Converting-html-to-hiccup
Syntactically correct what I wrote.. just hiccup can’t manage the version conflicts. I will check it in different way.
Whats the simplist way to build a clojure program that runs tell i tell it to exit and can take arguments at run time?
It seems like this should be obvious to me, but its been a while sense i have had to worry about this instead of having something take care of it for me :0