beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
sova-soars-the-sora 2021-03-15T01:08:16.481700Z

I got a question a really cool question... how do I synchronize logins / authentication between two separate ring servers? They are hosted at the same IP but I suppose I could use something like webhooks(tm?) to send a CSRF-free POST to the other box when there's a successful login and have the other "box" set that person as logged in... ? Hmmm. What do you think friends?

2021-03-15T01:21:08.482100Z

Hey guys, How could this be improved? Signal-cone receives two parameters: 1. An array of numbers (all greater than 0) 2. An array of 3 arrays - each with 2 numbers (all greater than 0) We can only use a range in which the lowest & the highest number does not fall out of the range of either of the other ranges. So in this call it would be [9, 15] Once we have our range we need return which numbers fall within that range. So here it would be [9, 11, 15] (defn return-inner-ranges [ranges] (into [] (map (fn [x] (into [] (range (get-in x [0]) (inc (get-in x [1])))))) ranges)) (defn get-shortest-range [ranges] (let [chosen-range (first (sort (return-inner-ranges ranges)))] (vector (first chosen-range) (last chosen-range)))) (defn get-within-range [numbers range] (filter (fn [x] (and (>= x (get-in range [0])) (<= x (get-in range [1])))) numbers)) (defn signal-cone [numbers ranges] (get-within-range numbers (get-shortest-range ranges))) (signal-cone [3,4,7,8,9,11,15,18,23] [[10 20] [8 16] [9 15]]) Thanks in advance!!

sova-soars-the-sora 2021-03-15T01:23:32.482400Z

@santosx99xx Could you give me another input and output example?

sova-soars-the-sora 2021-03-15T01:24:11.482900Z

If I put in [23, 55] and [[5 6] [77 2] [25 59]] what comes out?

2021-03-15T01:28:45.485800Z

from what I understand, no more than 3 ranges are given. It would probably be like this [[5 6] [25 59] [2 77]]. [25 59] should get chosen. And it would return all numbers that fall inbetween those two

aratare 2021-03-15T01:34:08.489Z

that’s prob what I would do too

1
2021-03-15T01:37:18.491Z

I guess only [59] would be returned.

sova-soars-the-sora 2021-03-15T01:41:13.492200Z

@santosx99xx please let me know if i'm way off. I think I get it. I want to check numbers in [stairway] to see if they fit in the range of each item in [gold silver bronze]. (check-if-medal-lives-in-stairway-range [stairway] [[gold] [silver] [bronze]]) So if [stairway] is 77,78,79,80 and [gold silver bronze] are [[8 78] [78 79] [1 5]] the result will be all the numbers of [stairway] that fit comfortable in [gold] or [silver] or [bronze] ... so here the output would be [77, 78, 79] from the inclusionary endcaps on [gold] and [silver]. I feel that with [gold silver bronze] it would be better to have a function that receives one [stairway] and one [medal] and returns the set that fits, endpoints included, from stairway into medal. And then I would call that function 3 consecutive times, once on gold, once on silver, and once on bronze, and throw them all into the same #{set} so only the uniques remain. How to check which numbers exist within a specific range? It's a cool question. I think you can call (range) on the endpoints in something like [gold] .. (range 0 5) to get [0 1 2 3 4] Then after you have the whole range splayed out, you can see which numbers exist in both [stairway] and [medal] and keep the overlapping ones. You can use (clojure.set/intersection ) and if the inputs have overlapping entries (entries in common) the result will be those.

sova-soars-the-sora 2021-03-15T01:42:07.492300Z

let me know if i didn't understand the challenge, i'm under the impression it takes 2 inputs and produces one output set, right?

2021-03-15T01:45:05.492700Z

Thanks, ill try that

sova-soars-the-sora 2021-03-16T19:39:23.030400Z

did it work out =D?

2021-03-16T21:48:13.031700Z

I couldn't get it to work. But it wasnt that serious. It was posted in another slack channel, and it kinda peaked my interest for a bit. But I lost it.

sova-soars-the-sora 2021-03-22T22:54:32.277600Z

Yeah I can dig that. Some programming challenges are fun. Do you enjoy coding interview questions?

2021-03-22T23:13:32.278400Z

I didn't know it was an interview question. It was posted in a js channel. I wrote my slightly different solution, then rewrote it in clojure. It was fun, but I haven't gone out of my way to do these types of challenges.

2021-03-15T01:53:07.492800Z

the first input being just numbers, the second being 3 ranges. Id have to choose the range that falls inbetween the other two. so given ranges [[8 78] [78 79] [1 5]] - [8 78] would get chosen. say you were given a vector of [0 1 2 3... 100] it would return [8 9 10 11... 78]

2021-03-15T01:59:06.493Z

(signalCone [[8 78] [78 79] [1 5]] [0 1 2 3 4... 100]) -> [8 9 10 11... 78]

Audrius 2021-03-15T05:31:47.494700Z

How to know what symbols can be used for vars and what not? Like , is ignored but . is a valid var name. What is the rule to know what is allowed?

phronmophobic 2021-03-15T05:36:02.494800Z

You can see what's allowed at: https://clojure.org/reference/reader#_symbols

💪 1
Adie 2021-03-15T12:51:34.495800Z

How to convert PersistenceList to vector in clojure?

sguessou 2021-03-15T13:17:16.496300Z

Hello Antti 👍

Antti 2021-03-15T13:18:26.496500Z

thanks, nice to be here

2021-03-15T13:36:16.496600Z

(vec list)

fsd 2021-03-15T14:56:51.499800Z

Hello there, Is it possible to reassign atom with same value ? For example:

(def image (atom "Inital"))
(if (or (= (get-in data [:ParkCategoryId]) 1) (= (get-in data [:ParkCategoryId]) 5) (= (get-in park [:data]) 6) )
  (reset! image image)
  )
I tried doing this but getting error

vanelsas 2021-03-15T14:58:40.002300Z

(reset! image image) -&gt; (reset! image @image)?

🙌 1
dpsutton 2021-03-15T14:58:45.002400Z

(swap! image identity) will keep the same value. you need to reset it to a value. image is an atom. (reset! image @image) is a way to do this. but what purpose do you want to achieve by setting something to its same value?

🙌 2
dpsutton 2021-03-15T14:59:32.003100Z

are you triggering a watch?

fsd 2021-03-15T15:02:48.006200Z

I am working on a function that is responsible for image icon In js I have as following

let image = 'inital';
   if (data.Available &amp;&amp; data.AvailableFiltered)
        image = "AvailableNotCriteria";


    if (data.ParkCategoryId == 1 || data.ParkCategoryId == 5 || data.ParkCategoryId == 6)
        image = image;
    else if (data.ParkCategoryId == 2)
        image = "B_" + image;

This is in Js but this is what I am trying to do

Mark Wardle 2021-03-15T15:02:48.006300Z

Hi all. Are there some rules about fully qualified classes when using metadata? I get a

Syntax error (ClassNotFoundException) compiling deftype* at (com/eldrix/clods/fhir_r4.clj:49:1).
java.lang.Organization
error when using a metadata tag in definterface or deftype that is not fully qualified. So this works:
(deftype OrganizationResourceProvider [^ODS ods]
  IResourceProvider
  (getResourceType [_this] Organization)
  OrganizationGetResourceById
  (^{:tag org.hl7.fhir.r4.model.Organization
     Read true}                                            
    getResourceById [_this ^{:tag    org.hl7.fhir.r4.model.IdType
                            IdParam true} id]
This doesn’t:
(deftype OrganizationResourceProvider [^ODS ods]
  IResourceProvider
  (getResourceType [_this] Organization)
  OrganizationGetResourceById
  (^{:tag Organization
     Read true}                                            
    getResourceById [_this ^{:tag    org.hl7.fhir.r4.model.IdType
                            IdParam true} id]
Even though I have
(:import (ca.uhn.fhir.rest.server RestfulServer IResourceProvider)
         (ca.uhn.fhir.context FhirContext)
         (org.hl7.fhir.r4.model Organization IdType Address Identifier)
         (ca.uhn.fhir.rest.annotation Read IdParam))
in my namespace (ns) declaration. Shortened references seem to work in defn etc..

fsd 2021-03-15T15:09:33.007Z

(if (or (= (get-in data [:ParkCategoryId]) 1) (= (get-in data [:ParkCategoryId]) 5) (= (get-in data [:ParkCategoryId]) 6) )
  (reset! image @image)
Is this the right way of doing it @dpsutton , I am not to sure as I am new to clojure?

dpsutton 2021-03-15T15:12:12.007300Z

yes, but again, it does nothing

dpsutton 2021-03-15T15:12:17.007500Z

just don't do that

simongray 2021-03-15T15:19:40.009400Z

@mark354 try (with-meta …) rather than ^{} for those cases where you are experiencing issues

simongray 2021-03-15T15:22:50.011300Z

The ^{} shorthand is a reader macro so it occurs at read time, which means your Java classes have not been imported yet: https://clojure.org/reference/metadata#_metadata_reader_macros that is my guess anyway

Mark Wardle 2021-03-15T15:27:27.011600Z

I think you’re right; the issue seems to affect only the metadata within definterface and deftype that are declaring the structure of the eventual java classes - the metadata within the forms making up runtime expressions can be short-form. I might just leave it as fully qualified class names but thanks for the with-meta pointer.

Mark Wardle 2021-03-15T15:30:04.011800Z

Just got to ignore the advice of the IDE to shorten an already imported reference!

2021-03-15T17:50:36.014600Z

i have a function i'm importing from a shared lib, and that function is throwing. the stack trace refers to line numbers and namespaces from the required lib, but not the namespace that requires it - not the namespace that actually invokes the required function. is there anything i can do about this to improve the stack trace?

2021-03-15T17:51:33.015Z

the stacktrace while show everything that is on the stack when the exception is thrown

2021-03-15T17:52:35.016200Z

if it doesn't show your calling code it either isn't on the stack (because the called code is being run in another thread) or some tool you are using is eliding it when printing/presenting the stacktrace to you

2021-03-15T17:53:30.017100Z

so a first thing to do is to just enter *e in the repl after you get the exception (assuming the exception is thrown the context of evaluating an expression at the repl)

2021-03-15T17:54:07.017800Z

that should (but not always) get you the default clojure printing of the last exception throw in the repl, which should be complete

2021-03-15T17:57:24.018Z

> code it either isn't on the stack (because the called code is being run in another thread) i suppose with pedestal, it's on another thread

2021-03-15T17:57:53.018200Z

i don't have access to *e because the error is being automatically caught and logged for me

2021-03-15T17:57:57.018400Z

by pedestal

2021-03-15T17:58:01.018600Z

if it is on another thread the information just isn't there

2021-03-15T17:59:11.018800Z

the stacktrace in an exception is literally a trace of the call stack as methods are called on the jvm, and each thread has its own call stack

2021-03-15T18:02:38.019Z

realizing the lazyseq returned by my function dramatically changes the stack trace for the better

2021-03-15T18:03:20.019200Z

perhaps that means my original problem was the second explanation - that my tool/framework is eliding the trace

2021-03-15T19:02:58.019500Z

ah, well, lazy-seqs can behave similar to threads

2021-03-15T19:03:25.019700Z

well, not like threads

2021-03-15T19:03:46.019900Z

a lazy-seq suspends a computation to be completed at some other time

2021-03-15T19:04:12.020100Z

and the stack may be different between when it is suspended and when it is completed

Eric Casteleijn 2021-03-15T20:47:28.021500Z

I have a question about lazy vs. non-lazy sequences and converting between the two. Here’s an interpreter session that’s illustrates my confusion:

(defn prnr [a] (prn a) a)
=&gt; #'grdn.core/prnr

(-&gt;&gt; [1 2 3 4]
     (map prnr)
     (take 1))
1
2
3
4
=&gt; (1)
(-&gt;&gt; (seq '(1 2 3 4))
     (map prnr)
     (take 1))
1
=&gt; (1)
(-&gt;&gt; (seq [1 2 3 4])
     (map prnr)
     (take 1))
1
2
3
4
=&gt; (1)

blak3mill3r 2021-03-15T20:48:16.023Z

@thisfred you are calling a side-effecting function prnr inside your map over a sequence

Eric Casteleijn 2021-03-15T20:48:23.023400Z

so if I pass a vector, map is not lazily evaluated, if I pass a seq literal it is, but if I convert a vector to a seq it once again isn’t.

Eric Casteleijn 2021-03-15T20:48:31.023700Z

thank you, yes

Eric Casteleijn 2021-03-15T20:48:35.024Z

that’s the point

blak3mill3r 2021-03-15T20:48:38.024100Z

and so you are seeing some of the implementation details of clojure sequences

blak3mill3r 2021-03-15T20:48:44.024300Z

namely, that they are realized in chunks of 32

blak3mill3r 2021-03-15T20:48:50.024500Z

this is done for efficiency

blak3mill3r 2021-03-15T20:49:09.025200Z

if you instead do something like this, you'll see results that make sense, I suspect

🙏 1
blak3mill3r 2021-03-15T20:49:32.025600Z

(doseq [a (take 1 [1 2 3])] (prnr a))

blak3mill3r 2021-03-15T20:50:02.026300Z

the thing is that when you call map you are getting a new sequence, and it will be realized in chunks of 32, for efficiency

Eric Casteleijn 2021-03-15T20:50:05.026600Z

thank you so much, I’ll read up on doseq

blak3mill3r 2021-03-15T20:50:25.027200Z

that means that even though you end up discarding most of it (with take) your function that you pass to map still gets called for more elements of the seq

blak3mill3r 2021-03-15T20:50:28.027500Z

you're welcome!

Eric Casteleijn 2021-03-15T20:50:37.027800Z

that makes sense I guess, though I wish the documentation wouldn’t say map was lazy, in that case 😉

blak3mill3r 2021-03-15T20:50:53.028200Z

You're not the first person to be confused by this

blak3mill3r 2021-03-15T20:51:07.028700Z

however it does speed things up for clojure's lazy sequences

blak3mill3r 2021-03-15T20:51:13.029Z

Perhaps the docs could be improved

Eric Casteleijn 2021-03-15T20:51:17.029200Z

(I know, side effects should probably be relegated outside of threading macros)

Eric Casteleijn 2021-03-15T20:51:36.029400Z

Anyway, really helpful!

dpsutton 2021-03-15T20:51:55.029700Z

compare with

(-&gt;&gt; (seq (vec (range 99)))
     (map prnr)
     (take 1))

Eric Casteleijn 2021-03-15T20:53:14.030100Z

Ooh, yeah, thanks, that demonstrates it beautifully

dpsutton 2021-03-15T20:53:57.030900Z

so its still lazy, but they never give you (in the contract) the degree of laziness so to speak. and don't assume you know what it is, or rely on that for side effects or to prevent heavy computation

👍 2
2021-03-15T20:54:51.031400Z

It is lazy, but it is not strictly lazy :-)

😂 4
dpsutton 2021-03-15T20:57:02.033Z

another common gotcha that you might misinterpret at smaller repl-test-sizes is that maps might appear to be ordered (keys {1 1 2 2 3 3}) is (1 2 3) but they are not ordered. just a quirk of small sized maps using an optimization

Eric Casteleijn 2021-03-15T20:57:56.033900Z

gotcha, thanks! I wasn’t expecting them to be, because of prior Python experience (although they since made the ordering stable there.)

Eric Casteleijn 2021-03-15T21:23:26.036100Z

The actual use case we have is we want to return the first non-nil result of an expensive calculation mapped over a vector. My colleague just pointed out that this works instead of using map + first or (take 1):

(-&gt;&gt; vector
     (some expensive-calculation))

Eric Casteleijn 2021-03-15T21:23:56.036700Z

(just in case someone stumbles upon this in the future with that exact need 😉

2021-03-15T21:24:45.037700Z

That works, but the fact that the input is a vector means that it has already been fully realized. If every element is (or might be) expensive to calculate, and you don't need any after the first non-nil to be calculated, then that will cost more than a more lazy solution.

Eric Casteleijn 2021-03-15T21:25:19.038500Z

Yeah, in this case the input is fully realized (passed in from the frontend)

2021-03-15T21:25:21.038700Z

Oh, sorry, I misunderstood your statement. Thinking more on the actual issue.

dpsutton 2021-03-15T21:28:26.040300Z

if you want to control expensive evaluation using map and first is a poor way to get there. (loop [[x &amp; xs] the-seq (when x (if-let [transformed (f x)] transformed (recur xs)))

👍 1
Eric Casteleijn 2021-03-15T21:31:15.040800Z

(some f vector) is neat though