Fork me on GitHub
#beginners
<
2022-05-14
>
Jon Olick03:05:18

is seq lazy or eager conversion to lists?

seancorfield03:05:22

seq on a LazySeq is a bit more complex as it has to realize enough to get something seqable: https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/LazySeq.java#L50

seancorfield03:05:13

RT.seq() delegates other cases to seqFrom() which is just below it and deals with the other types.

Alex Miller (Clojure team)03:05:54

A seq gives you a logical list view on a data structure, whether that's lazy depends on the object

zakkor07:05:31

I am using mount to manage my app state (more specifically, just the database connection). There are 3 namespaces which require and reference the piece of state defined through mount/defstate, and one of the namespaces has a main function. How can I make sure the state is always started: 1) before the main function is called, and also 2) for REPL driven development, whenever I load one of the other namespaces in the REPL? Currently I've acheived 1) by calling mount/start right in -main, but I don't know how to achieve 2). I mostly end up evaluating the mount/start myself, and it's annoying to have to do this every REPL restart

jmv22:05:50

for 2, you can add a dev time path with user.clj and require the namespaces there. you will often see this file at dev/user.clj . i might still keep the manual call to mount so your're in control, but if you want to have it happen automatically, you can also just add the (mount/start) call there

zakkor07:05:09

I just want the db state to behave as a global variable that only gets set once, whenever any namespace referencing it gets evaluated, so I am open to switching to another state management library if it sounds more fitted to my usecase

M3tti08:05:02

Hey guys i'm back again in clojure land after a while doing other language stuff. Just wanted to know if leiningen is still a thing or should i do the deps edn stuff or even boot? I'm currently lost what to use.

delaguardo08:05:54

Leiningen is still a very powerful tool with a lot of plugins for every need. But because Clojure cli is getting traction it is worth learning it too. You will find some things much more easy to maintain and debug. Personally i stopped using leiningen for my new projects about a year ago in favor of Clojure cli

schmee08:05:32

Leiningen is definitely still a thing, I’ve never bothered with either deps or boot

delaguardo09:05:59

Also i would recommend looking at the annual Clojure survey for the past few years to see current trends

M3tti09:05:15

Ok great regarding clojure cli i was also seeing that it gains traction currently. So i'll try my best with clojure cli.

practicalli-johnny15:05:09

The Clojure code will be the same regardless of the build too used. If you are used to Leiningen, then stick with it until you get back into Clojure. Leiningen is still used by a large number of projects, especially those that have existed for a year or more. Boot seems extremely niche now. Clojure CLI is growing in adoption and evolving new features, such as the recent tools.build. There can be more to learn with Clojure CLI than Leiningen, although it does bring a greater flexibility in tooling. Adding a deps.edn file to existing Leiningen projects is a relatively simple way to start adoption of Clojure CLI https://practical.li/clojure/clojure-cli/ covers example of using Clojure CLI, along with many community tools.

👍 1
Gabriel Kovacs09:05:40

Hi! I have a function that has to return BigDecimal. At the end I call bigdec. Is there a better way of doing it?

zeitstein12:05:30

Is there a better alternative to the following?

(def ^:dynamic *testing?* false)

;; a function called in multiple other functions
(defn make-response [data]
  (let [eids (calculate-eids data)]
    (if *testing?*
      eids
      (pull-from-db eids))))

;; used in comments and tests
(binding [*testing?* true]
  (make-response some-data))

Darin Douglass13:05:42

You could use with-redefs (with the caveat of being wary around multi-threaded tests):

(with-redefs [pull-from-db identity]
  (make-response data))

zeitstein13:05:49

This is perfect, thanks!

Ben Sless14:05:35

Yes, pass the db as an argument and don't pull your hair our over dynamic bindings

gratitude 1
Ben Sless14:05:37

Then in testing pass a different implementation for the db To manage this use a library like component and a configuration library like aero

zeitstein14:05:19

I've been delaying looking into these kinds of libraries since things are still relatively simple to manage. Will keep in mind, thanks!

Ben Sless14:05:48

When you reach 2-3 logical components that need different implementations it's time, imo

zeitstein15:05:20

Moved it up the to-do list 🙂

Drew Verlee16:05:48

It's worth being critical of what you're testing. Does your app need specific data to work? Could you abstract that into a spec? Is it that you need to be sure of a schema? Could you move that closer to where the data is used and avoid possible mistranslations?

zeitstein17:05:37

All good questions 🙂 I've dabbled with spec, but I'm yet seriously start thinking about schemas and testing. I've definitely felt the desire for spec/malli type of thing.

bortexz17:05:17

Is it fine to do assertions inside swap! ?

robertfw18:05:25

Seems to fail as you'd expect on the repl, just the usual note that assertions can be turned off

user=> (swap! (atom nil) #(assert (some? %)))
Execution error (AssertionError) at user/eval3$fn (REPL:1).
Assert failed: (some? p1__2#)

👍 1
dpsutton18:05:56

if you read the (doc atom) docstring you can see a mechanism is built in to atoms that might serve your needs

💯 1
bortexz07:05:05

Thanks, It is for a public lib API and wanted to use assert to keep with another implementation of a protocol also using assert (but not atoms)

bortexz07:05:43

But given asserts can be turned off, not sure it is what I want, I will probably throw ex-info’s