Does anyone know a less verbose way of writing the destructuring / termination check in the following kind of loop
:
(loop [xs foo
ys bar
...]
(if (and (seq xs) (seq ys))
(let [[x & xr] xs
[y & yr] ys]
(recur xr yr ...))
<return>))
Basically a loop which iterates through two sequences in parallel and exits if any of them run out, like (map f xs ys)
but with accumulators and flow controlYou can remove one line by using nested when-first
.
And if you write that particular code many times, you can just write a macro yourself for it.
I was trying to avoid writing more macros than necessary, heh - but thanks for the tip about when-first
!
maybe you could destructure in loop
? i.e.
(loop [[x & xs] [1 2]
[y & ys] [:a :b :c]]
;; do stuff
(when (and (seq ys) (seq xs))
(recur xs ys)))
That would work as well if foo
and bar
in the OP are something that can be used as an argument to first
while being empty. It's true for the built-in collection types, it might not be true for some custom types.
Also, your code still needs to check if the very first iteration is needed.
Yeah, destructuring in the loop only works well if the last iteration has to be treated specially (having a leftover x and y in the non-recur branch), otherwise you'd have to duplicate logic somewhere
I have a question not really about clojure but about open source sw development. Looking for someone to chat with who understands the git flow involving pull requests.
Iβd like to do something with git, but I suspect what Iβd like to do is not possible. Would love someone to contradict me and show me how it is possible.
Here's a simple example (yes it's just mapv inc
)
(loop [xs (range 10)
result []]
(if-some [[x & xr] xs]
(recur xr (conj result (inc x)))
result))
(loop [[x & xr] (range 10)
result []]
(if (seq xr)
(recur xr (conj result (inc x)))
(conj result (inc x)))) ;; repeated :/
In fact I used to do the following, until I realised it was a flawed idiom that breaks when the sequence contains nils:
(loop [[x & xr] (range 10)
result []]
(if (some? x)
(recur xr (conj result (inc x)))
result))
feel free to post your question in #off-topic channel
Wait, destructuring in loops works? This is gonna change a lot of my code!
Hello everyone! I'm struggling to get automated testing working. I have a small project (based on reagent) where I want to test some cljc code. Folder structure is the following:
.
βββ figwheel_server.log
βββ LICENSE
βββ Procfile
βββ project.clj
βββ README.md
βββ resources
βΒ Β βββ public
βΒ Β βββ css
βΒ Β βββ site.css
βββ src
βΒ Β βββ clj
βΒ Β βΒ Β βββ modern_tile_gen
βΒ Β βΒ Β βββ handler.clj
βΒ Β βΒ Β βββ server.clj
βΒ Β βββ cljc
βΒ Β βΒ Β βββ modern_tile_gen
βΒ Β βΒ Β βββ domain
βΒ Β βΒ Β βΒ Β βββ geometry.cljc
βΒ Β βΒ Β βΒ Β βββ line.cljc
βΒ Β βΒ Β βΒ Β βββ path.cljc
βΒ Β βΒ Β βΒ Β βββ shape.cljc
βΒ Β βΒ Β βββ util.cljc
βΒ Β βββ cljs
βΒ Β βΒ Β βββ modern_tile_gen
βΒ Β βΒ Β βββ core.cljs
βΒ Β βΒ Β βββ sidebar.cljs
βΒ Β βββ sass
βΒ Β βββ _layout.scss
βΒ Β βββ _profile.scss
βΒ Β βββ site.scss
βββ system.properties
βββ test
βββ clj
βββ cljc
βΒ Β βββ modern-tile-gen
βΒ Β βββ domain
βΒ Β βββ test_line.cljc
βββ cljs
Test looks like this
(ns modern-tile-gen.domain.line
(:require [clojure.test :as t :refer [deftest is testing]]))
(deftest some-simple-test
(testing "Some test"
(is (= "sdfsd" "bgf")))
However when I run lein test
it does not see nor run any testMay it be related to some namespace collision or something, and how to debug it?
I was NOT able to run test file from a repl as well
Are you setting :test-paths? https://github.com/technomancy/leiningen/blob/2a047ef4dcc68fcfa5792e0511fcb1cd7c1f589e/sample.project.clj#L311
it seems your test NS has the same name as another one in src
, am I right?
@adam.starlight test/modern-tile-gen needs to be test/modern_tile_gen
Also your namespace declaration modern-title-gen.domain.line
doesnβt match the path modern-title-gen/domain/test_line.clj
^ that too
Thank you guys! I was missing both :test-paths, and filenames/namespaces. Now it is working.