clojure

New to Clojure? Try the #beginners channel. Official docs: https://clojure.org/ Searchable message archives: https://clojurians-log.clojureverse.org/
yuhan 2021-04-26T07:53:57.054500Z

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-himik 2021-04-26T07:59:41.054600Z

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

p-himik 2021-04-26T08:00:17.054800Z

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

yuhan 2021-04-26T08:13:26.055100Z

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

prnc 2021-04-26T08:49:48.055600Z

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-himik 2021-04-26T08:51:52.055800Z

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-himik 2021-04-26T08:52:22.056Z

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

yuhan 2021-04-26T09:05:10.057Z

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

πŸ‘ 1
Jim Newton 2021-04-26T09:05:22.057500Z

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.

Jim Newton 2021-04-26T09:05:56.058100Z

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.

yuhan 2021-04-26T09:10:50.058300Z

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

yuhan 2021-04-26T09:13:08.058600Z

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

2021-04-26T10:08:35.059100Z

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

NoahTheDuke 2021-04-26T12:20:21.060400Z

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

Adam 2021-04-26T19:32:14.063700Z

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

Adam 2021-04-26T19:35:09.064700Z

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

Adam 2021-04-26T19:36:26.065700Z

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

bhaim123 2021-04-26T19:38:12.066400Z

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

ghadi 2021-04-26T19:41:24.067700Z

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

Derek Passen 2021-04-26T19:41:34.068100Z

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

ghadi 2021-04-26T19:41:50.068400Z

^ that too

Adam 2021-04-26T19:47:02.070100Z

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

πŸ‘ 1