Fork me on GitHub
#beginners
<
2021-12-27
>
fearnoeval06:12:45

The error you're seeing (`Unable to resolve symbol: def in this context`) is because def is a special form and you have a bare def in your file. Change your def all-records (...) to (def all-records (...)) and you should get past that error.

Fudge07:12:18

Thanks

👍 1
Fudge07:12:31

After parsing, I'm getting only one data and not all the data

fearnoeval07:12:10

Without seeing the contents of your JSON, I'd guess that it's a file with several objects separated by "something" (such as newlines) instead of one large JSON object, and the parser is returning after the first read completes.

Fudge07:12:42

how to parse this data?

fearnoeval07:12:15

It certainly looks like JSON, but I'm assuming you're still not getting the result that you're expecting. Unfortunately, I can't make any definite conclusions from the screenshot provided as there's not enough information to tell.

Fudge07:12:06

Is it okay now?

fearnoeval08:12:20

It's not ideal since parsing-by-eyeball is tough, but it is warmer. You can see towards the end that there's no comma after an object terminator (image pasted), so the entire contents of the file is not valid JSON, but is perhaps delimited in some other way such as newlines (there are multiple standards for this, including but probably not limited to LDJSON, NDJSON, and JSONL).

fearnoeval08:12:40

If it is delimited by newlines, I'm not sure of the support in clojure.data.json for that, so you'd have to check the source or docs. Alternatively, I would think that there's an alternative JSON library that does support it (perhaps cheshire?). As another alternative, if this is a throwaway-type project, you could (at the cost of perf and elegance) "convert" the file's contents by splitting on newlines, joining with commas, and wrapping the entirety in a pair of square brackets to make it a valid JSON array.

delaguardo08:12:13

Most recent version of data.json can't read such content. https://clojure.atlassian.net/browse/DJSON-50

👍 1
Fudge08:12:37

Yes I'm using a correct json file now

Fudge08:12:41

Thankyou

👍 1
Fudge09:12:39

How to print the total count of persons in clojure?

Fudge09:12:59

for the above json data

fearnoeval09:12:58

If you read your data into all-records, then (count all-records).

👍 1
fearnoeval09:12:35

I don't want to just give everything away, but filter, reduce, map, and count should get you most of the way there.

Fudge11:12:29

(println ((all-records filter [:gender :male]))  )
why are the parameters passed wrong?

fearnoeval17:12:16

There are a few issues there: • You're calling all-records instead of filter[:gender :male] is probably not the function you want to use to do the filtering • The extra set of parens means you are calling the result of calling all-records Given I don't have your exact data structure and don't know if you keywordized your keys, I made a rough equivalent in the following example that will hopefully help:

(def all-records
  [{"id" 1, "gender" "female"}
   {"id" 2, "gender" "male"}
   {"id" 3, "gender" "male"}
   {"id" 4, "gender" "female"}])

;; - There are fancier ways to do this, but I'm following the principle of
;;   walking before running

(filter (fn [record]
          (= "male"
             (get record "gender")))
        all-records)

Fudge06:12:09

I'm trying to parse the json data from a file, what's the error?

popeye07:12:33

How to define a[1] =0 which in python to clojure, where dp is (def s (into [] (range 10001)))

Ben Sless07:12:48

Assoc But why do you want to do that?

popeye07:12:56

i want to initialise 3 values globally

a[1] = 0
a[2] = 1
a[3] = 2

Ben Sless07:12:13

Only 3 out of a bunch?

Ben Sless07:12:44

Why? What's the context of this global state?

popeye07:12:37

i am converting one python program into clojure program, so I wanted that

Antonio Bibiano08:12:41

Isn't the s you defined above exactly what you wanted?

popeye08:12:08

i wrote in this way now

(def dp (int-array (range 10001)))

(aset dp 1 0)
(aset dp 2 1)
(aset dp 3 2)

Ben Sless09:12:51

Is your program modifying this global array?

randomm char05:12:08

(def a(int-array 10001[1 2 3]))

Aquati10:12:40

Hey guys! Can someone recommends me resources to understand patterns and best practices to deal with side effects in Clojure? I am totally new to the language and I looking to understand how to model my program to use as much pure functions as possible, and when not possible, the recommended way to deal with network calls in the mid of the software (using monads or not)

Ben Sless10:12:32

Clojure doesn't exactly lend itself to monads because of dynamic types, but if you start with pure functions and pull IO out of your logic chain you can end up with a pretty clean implementation of "components" which communicate with each other

Ben Sless10:12:33

Another model is using pipelines to build a "production line"

delaguardo11:12:49

Re-frame docs could be a good source of inspiration in that domain

Aquati11:12:44

Thanks for the explanation! So, whenever possible do IO bounds before the application logic, and pass the fetched data over pure functions containing your business logic? And one question, what to do when that are not possible? What the recommended approach when a network call only needs to happen given certain decisions on mid application logic, and would be preferrable or necessary to avoid that call if not needed? Like "call x service if this is true, or else call completely different service y. Never call both". Sorry if it is not clear, I'm trying my best to express my idea. I think I came with the scenario I described sometimes in Java microservices, and I am wondering if functional programming has a preferrable way to deal with that, that we remain dealing with pure functions but can do conditional network calls.

pithyless13:12:41

@U02S178HM6Y I think Eric Normand does a great job separating Actions, Calculations, and Data. He wrote a book - https://grokkingsimplicity.com/ - but you can see these ideas permeating a lot of his blog writing, courses, and podcast as well (if looking for non-book resources).

pithyless13:12:30

For more Clojure-specific advice, https://elementsofclojure.com/ is also a good read.

pithyless13:12:28

(And when that doesn't suffice, you can find and/or start some good discussions on #architecture for building bigger systems.)

Aquati15:12:29

@U05476190 Thanks for the resources! The first book looks very interesting, I think will help me to start thinking in functional way. The channel recommendation is pretty nice too, just joined and found useful discussions

noisesmith18:12:55

regarding when it's not possible to do io first, then pure: I think a more sophisticated / flexible approach is to have io oriented functions and data oriented functions, and to alternate between them in your business logic - the key is that if you are putting data oriented code and io code in one function you are probably making things more complex than they should be

☝️ 1
noisesmith18:12:15

there are patterns for doing this segregation, eg. a "driver function" that bounces back and forth between io and processing at the top level to glue things together and a general looping of : acquire information -> update decision -> act

☝️ 1
Fudge11:12:45

How to calculate the count below a certain age from json file using clojure?

Lennart Buit12:12:54

We kinda need example data to answer that question. Assuming you have some JSON array, with objects with “age” key, you could try something like this:

(require '[clojure.data.json :as json])
=> nil

(->> "[{\"name\":\"John\",\"age\":27},{\"name\":\"Mary\",\"age\":65},{\"name\":\"Fred\",\"age\":31}]"
     (json/read-str)
     (filter (fn [{:strs [age]}]
               (< age 35)))
     (count))
=> 2

grierson15:12:36

Hey, Does Clojure have anything like a .sln file for .net that groups many projects (deps.edn) together? Trying to make a sample microservice project but would like all the services within one Intellij/git project.

Arthur15:12:25

You can probably just create a few aliases for multiple projects using a single deps.edn file. If you need something more robust you can try Polylith though.

👍 1
grierson15:12:23

Thanks. That's a really good idea just to alias. Only needs to be simple for now

v3ga15:12:08

So I'm taking in a UUID and i'm running into a casting issue here when it comes to comparing it to the matching ID in my database. How would I go about writing this correctly? #p[chrysalis-xd.model.iota/delete-iota-by-id:63] uuid => #uuid "5b1ae133-890a-419c-a38f-3c006ab6c073" WARN org.eclipse.jetty.server.HttpChannel - /api/iota/5b1ae133-890a-419c-a38f-3c006ab6c073 java.lang.ClassCastException: class java.util.UUID cannot be cast to class clojure.lang.IFn (java.util.UUID is in module java.base of loader 'bootstrap'; clojure.lang.IFn is in unnamed module of loader 'app')

Lennart Buit15:12:20

you are invoking your uuid as if it is a function

Lennart Buit15:12:32

but; as far as I can see, not in this piece of code

Lennart Buit15:12:11

This would trigger the same ex:

(let [my-uuid (UUID/randomUUID)]
  (my-uuid))
Execution error (ClassCastException) at user/eval169976 (form-init5676996670185437098.clj:2).
class java.util.UUID cannot be cast to class clojure.lang.IFn (java.util.UUID is in module java.base of loader 'bootstrap'; clojure.lang.IFn is in unnamed module of loader 'app')

v3ga16:12:40

yeah, i'm trying to figure out how I can just place the actual # in. So it's it's being called via a web route. I send the ID through...and from the front end it comes in as a string so I convert it back to a true UUID but as you said i'm invoking it as a function. I just can't figure out how to set it correctly.

dpsutton16:12:50

what does the stacktrace say?

v3ga19:12:49

As is.... ok so lets ignore the (#p uuid) thats me using hashp to print the value of uuid out so I can verify that it is indeed the ID that I'm expecting. That gives me the #p[chrysalis...line "#uuid "5b1ae133..." So i'm verifying that I do have the idea I want. The issue seems to be below within my honeysql code [:= :id uuid] where the goal is to compare that UUID value to one in the database. I'm asking how would I go about writing that correctly since it's clearly trying to use 'uuid' as a function. #p[chrysalis-xd.model.iota/delete-iota-by-id:64] uuid => #uuid "5b1ae133-890a-419c-a38f-3c006ab6c073" WARN org.eclipse.jetty.server.HttpChannel - /api/iota/5b1ae133-890a-419c-a38f-3c006ab6c073 java.lang.ClassCastException: class java.util.UUID cannot be cast to class clojure.lang.IFn (java.util.UUID is in module java.base of loader 'bootstrap'; clojure.lang.IFn is in unnamed module of loader 'app') at chrysalis_xd.model.iota$delete_iota_by_id.invokeStatic(iota.clj:64) at chrysalis_xd.model.iota$delete_iota_by_id.invoke(iota.clj:61) at muuntaja.middleware$wrap_format$fn__50047.invoke(middleware.clj:73)...

v3ga07:12:30

Ok so it was my debugging line `(#p uuid) that caused the error. Now on to figure out why my deletion code is bad.

v3ga07:12:23

Ok so this works, just in case anyone new runs across a similar debacle. How to delete a post by UUID.

Lennart Buit07:12:20

oh in hindsight this makes sense, if #p is the debugging thing (https://github.com/weavejester/hashp), then it expands to basically calling the uuid. You would have needed to do #p uuid instead of (#p uuid).

👍 1
v3ga08:12:52

@UDF11HLKC yup...'#p uuid' I'm not sure why but I couldn't escape the thought that to use #p I had to wrap it. But I also commented it out and received the same error. In reality I probably didn't re evaluate my function first and I was dead set on believing the issue was being thrown further down in my query. Then my real issue, I forgot to wrap my code with "jdbc/execute" to even attempt a transaction to the DB.