Fork me on GitHub

Clojure and ClojureScript will probably never see the meteoric adoption that e.g. Go went through, but it’s a stable language that is excellent at being productive


How does one set the random seed in clojure? I found this, but it seems odd that there’s no way to do it in base (terminology?) clojure


I’m aiming to unit test a function that produces weighted random output. If there’s a better alternative that setting the seed, I am all ears


@tsulej Curious. Can an if chain show why (and) returns true or (or) returns <yikes> nil? (just checked myself in the repl and got surprised by the latter.)


@hiskennyness in my opinion yes. After macroexpand you can see precisely what is returned and when.


@dpsutton the question was why or or and behave like they behave (ie. or returns nil instead of false) and how to figure it out.


I proposed to look at expanded version of both to see how it's constructed (if chains)

Alex Miller (Clojure team)14:07:31

and and or can return potentially any value, not just true/false. If you want that, wrap in boolean


and even more fun is thinking about the values of (and) and (or)


I was asking what goes inside (if ) to convey (and) returning true.


I cannot think of anything for the conditional. I understand (and) => true as “and returns true if none of its arguments are false”…except no, it returns the last argument. But there is no last argument in (and), so now we have to write a short speech: “and returns its last argument if no other arguments are falsey, but returns true if there are no arguments”. Boy that sounds like a hack.

Alex Miller (Clojure team)15:07:00

It returns true if all values are logically true


Now with (or) we have “or returns the first truthy value”. Hellasweet. 🙂


I thought (and 1 2 3) returned 3


(and) is vacuously true


Of course “returns first truthy” explains why (or) returns nil


Sure, but it could also be vacuously false or 42, so we are still trying to explain why true was chosen. Should it be an error?


Oh, thx. Did not know the math/logic meaning.


everyone has this reaction when learning about universal and existential qualifiers in math.


But my original concern was the idea of an if chain, which I think breaks down in the vacuous case.


yes you are right. If you look at the sources of these the empty cases are handled explicitly.


If I wanted to cast bools to int like this:

(defn bool->int
  (if b 1 0))
would that a) make me a terrible person, and b) is there a better way


the name implies a strictness that isn't there about the input type, but that probably doesn't matter


(bool->int 0) => 1; (bool->int nil) => 0


that 0 case will be surprising to people who are used to implicit bool/int overloading as it's used in most languages with the feature


to get a bool->int that is closer to how python/js/ruby/c etc. do it you could try (not (contains? #{nil false 0 0.0} x))


you can add "0", [] and "" to that list... for that little PHP touch


haha, you could add optional compatibility switches that expand / vary the set... :D

👌 4
Daniel Hines21:07:00

I know I’ve asked this same question here before, but ya’lls good answers didn’t quite make it through my thick head, so let me try again: What’s the right way to ask for credentials when writing a library? I’m tempted to use refs/atoms like this example, but this goes against Alex Millers advice at as well the advice I’ve gotten here. The counter proposal is to use a framework like component or mount, but I don’t want to necessarily lock a user into a framework to access my library. Amazonica gives a bunch of different options listed here, none of which seem to fall directly in the “use component/mount” category.

Daniel Hines21:07:17

@smnirven, I’d readily welcome a defense of the ref/atom method used in biomass, btw.


@d4hines for credentials that are not related to a persistent connection, why does the library need to worry about keeping them around at all? The most natural thing would be to have the client pass in the credentials with each call.


Dependency injection, component/mount etc. are for when there's something stateful that has to managed, like a connection handle


even in those cases the most neutral thing for the library to do would be offer a connect-like function that returns the connection handle, and API functions that take the connection handle as a parameter. That way your clients can choose to manage the connection state however they want


some libraries, like "carmine" is the example that comes to mind, offer a sort of mix-and-max approach where there's a flavor of the API that will hold on to a redis connection handle in an atom inside the library's namespace, and another flavor where the client can manage it


I'll second what @michael.gaare suggests - leave acquisition, storage and scope to applications, and let a library take an argument providing any resources needed. And yeah, component is a good choice for the application but the lib doesn't need to care.


direct arguments are flexible in a way that dynamic vars, global state, and stateful objects are not. If you work in terms of immutable args to a function, a user can do the same, or use what you provide to implement globals, dynamics, mutables, etc. - but none of the other solutions are that flexible or universal


Hi, I’m trying to use a JS library that’s not in cljsjs. (Specifically, ) Is :npm-deps the best (or only?) way to do this? Is writing my own externs file a good way (then I could contribute back to cljsjs) ? What’s the best current way to consume and use that library (among other npm libs…) Any help is much appreciated!