Fork me on GitHub
#clojure
<
2021-04-26
>
yuhan07:04:57

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 control

p-himik07:04:41

You can remove one line by using nested when-first.

p-himik08:04:17

And if you write that particular code many times, you can just write a macro yourself for it.

yuhan08:04:26

I was trying to avoid writing more macros than necessary, heh - but thanks for the tip about when-first !

prnc08:04:48

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)))

p-himik08:04:52

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.

p-himik08:04:22

Also, your code still needs to check if the very first iteration is needed.

yuhan09:04:10

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

πŸ‘ 2
yuhan09:04:50

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 :/ 

yuhan09:04:08

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))

Noah Bogart12:04:21

Wait, destructuring in loops works? This is gonna change a lot of my code!

Jim Newton09:04:22

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.

delaguardo10:04:35

feel free to post your question in #off-topic channel

Jim Newton09:04:56

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.

Adam19:04:14

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 test

Adam19:04:09

May it be related to some namespace collision or something, and how to debug it?

bhaim12319:04:12

it seems your test NS has the same name as another one in src, am I right?

Adam19:04:26

I was NOT able to run test file from a repl as well

ghadi19:04:24

@adam.starlight test/modern-tile-gen needs to be test/modern_tile_gen

Derek19:04:34

Also your namespace declaration modern-title-gen.domain.line doesn’t match the path modern-title-gen/domain/test_line.clj

ghadi19:04:50

^ that too

Adam19:04:02

Thank you guys! I was missing both :test-paths, and filenames/namespaces. Now it is working.

πŸ‘ 2