Fork me on GitHub
#beginners
<
2018-07-30
>
lilactown01:07:30

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

torvaney14:07:52

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 https://github.com/trystan/random-seed

torvaney14:07:03

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

hiskennyness14:07:43

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

dpsutton14:07:07

what do you mean @hiskennyness

tsulej14:07:41

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

tsulej14:07:47

@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.

tsulej14:07:53

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

alexmiller14:07:31

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

dpsutton14:07:54

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

hiskennyness15:07:46

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

hiskennyness15:07:14

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.

alexmiller15:07:00

It returns true if all values are logically true

hiskennyness15:07:25

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

hiskennyness15:07:05

I thought (and 1 2 3) returned 3

sundarj15:07:09

(and) is vacuously true

hiskennyness15:07:37

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

hiskennyness15:07:53

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?

hiskennyness15:07:50

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

dpsutton15:07:55

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

hiskennyness15:07:20

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

dpsutton15:07:30

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

torvaney16:07:41

If I wanted to cast bools to int like this:

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

alexmiller16:07:08

Looks ok to me

noisesmith16:07:49

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

noisesmith17:07:10

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

noisesmith17:07:53

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

noisesmith17:07:11

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

joelsanchez17:07:59

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

noisesmith17:07:54

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

d4hines21: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 https://github.com/smnirven/biomass/blob/master/src/biomass/request.clj#L16-L25, but this goes against Alex Millers advice at https://puredanger.github.io/tech.puredanger.com/2014/01/03/clojure-dependency-injection/ 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 https://github.com/mcohen01/amazonica#authentication, none of which seem to fall directly in the “use component/mount” category.

d4hines21:07:17

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

michael.gaare22:07:37

@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.

michael.gaare22:07:28

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

michael.gaare22:07:07

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

michael.gaare22:07:41

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

noisesmith23:07:33

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.

noisesmith23:07:09

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

hoopes23:07:56

Hi, I’m trying to use a JS library that’s not in cljsjs. (Specifically, https://www.npmjs.com/package/react-edit-inline ) 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!