Fork me on GitHub

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.



👍 1

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


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.


how to parse this data?


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.


Is it okay now?


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


If it is delimited by newlines, I'm not sure of the support in 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.


Most recent version of data.json can't read such content.

👍 1

Yes I'm using a correct json file now



👍 1

How to print the total count of persons in clojure?


for the above json data


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

👍 1

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


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


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


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


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?


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?


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?


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


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"


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


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.


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


For more Clojure-specific advice, is also a good read.


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


@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


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

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

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 '[ :as json])
=> nil

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


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.


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

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


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


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.


what does the stacktrace say?


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


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


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 (, then it expands to basically calling the uuid. You would have needed to do #p uuid instead of (#p uuid).

👍 1

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