Fork me on GitHub
#beginners
<
2020-09-03
>
Steiner01:09:11

recently I am interested in writing script with Clojure, I don't know if there is any example, can someone help me out?

seancorfield01:09:30

@steiner3044 You'll need to be a bit more specific -- what sort of script do you want to write? What are you already familiar with?

seancorfield01:09:34

How familiar are you with Clojure so far? What tutorials have you already read? This one? https://clojure.org/guides/getting_started

Steiner02:09:01

eh, bash is I want to write, I can write bash script, but I don't like its strange syntax, so I want to use other language,

Steiner02:09:31

however, I am not familiar with IO in Clojure now

seancorfield02:09:41

https://clojuredocs.org/clojure.java.io -- but it's all based on Java IO and you may find it worse than bash...

seancorfield02:09:20

If you want to do file manipulation, you'll need to use Java interop and stuff like https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/Files.html

seancorfield02:09:04

Although this library https://github.com/clj-commons/fs might make your life a bit easier

Steiner02:09:45

oh, sh*t you mean when start clojure, it is needed to run JVM? if it is, it there any example about writing script in Scheme??

seancorfield02:09:16

You might want to look at babashka which is a native application (but a slightly limited ecosystem of libraries)

seancorfield02:09:39

https://github.com/borkdude/babashka and also ask in the #babashka channel

seancorfield02:09:56

That's what some folks are using for shell scripting with "Clojure"...

Steiner02:09:16

thank you:slightly_smiling_face:

Kyle Brodie01:09:07

How would I catch an exception thrown in a lazy sequence and continue? I'm using org.clojure/data.csv and a CSV error is being thrown for one of the rows but, at least for now, I want to just skip those rows and continue

seancorfield01:09:41

@kyle That's going to depend on exactly what's causing the exception and where.

seancorfield01:09:54

If the exception is coming from inside that library and it's failing to produce more elements of the sequence, you won't be able to fix that without patching the library. If the exception is coming from your own code, you probably could make it more robust.

Kyle Brodie01:09:22

Its coming from inside the library. I am transforming the lazy seq produced by org.clojure/data.csv with map . I don't know how I would continue without translating it to a (loop) and getting the first element of the lazy sequence inside a try-catch

seancorfield01:09:36

Well, you could map "bad" rows to nil and then filter those out -- if data.csv continues to produce elements beyond the row you cannot consume.

seancorfield02:09:14

(filter some? (map (fn [row] (try (your-normal-process row) (catch Exception _))) data-csv-rows))

jaihindhreddy05:09:15

Would (keep #(try (your-normal-process %) (catch Exception _)) data-csv-rows) be equivalent to this?

seancorfield05:09:23

Yeah, actually it would... although of course both solutions would fail if your-normal-process could return nil I guess (but then maybe doseq is a better way to process rows 馃檪 ). I tend to forget about keep but I think it's a nicer solution in this case. Thanks.

鉂わ笍 1
seancorfield02:09:04

(assuming your-normal-process itself is eager)

馃挴 1
Kyle Brodie02:09:13

Does that work if (first csv-lazy-seq) would throw if the first CSV row in the file is invalid? It is throwing a plain java Exception

seancorfield02:09:55

Right, that goes back to my question about exactly where the exception is originating.

seancorfield02:09:01

Yeah, if the CSV file is unparseable, there's very little you can do: the library simply cannot recover from the error.

Endre Bakken Stovner06:09:42

I am new to protocols and mixins. In The Joy of Clojure, the authors write that mixins and protocol extensions are fully open. What does fully open mean here?

Endre Bakken Stovner06:09:36

I am guessing it means they can be extended at any time and place (before usage).

alexmiller12:09:43

It means that new extensions can be added at any time, without modifying the original definition

馃憤 1
Michael J Dorian12:09:27

Hey friends, I'm trying to do a basic query in datomic and the docs aren't quite clicking for me. I have this test dataset:

(def first-movies [{:movie/title "The Goonies"
                      :movie/genre "action/adventure"
                      :movie/release-year 1985}
                     {:movie/title "Commando"
                      :movie/genre "thriller/action"
                      :movie/release-year 1985}
                     {:movie/title "Repo Man"
                      :movie/genre "punk dystopia"
                      :movie/release-year 1984}])
And I have this query (d is an instance of the database that the movies have been transacted into)
(d/q '[:find ?title
         :where ;[]
         [?title :movie/title ?title] ] db)
this returns a list of all movie titles, as per the datomic tutorial, but what I want is a list of all movie titles from movies that came out in 1984. In other words, my result set should be ["Repo Man"] What can I put in the commented out where clause to make this happen?

alexmiller13:09:55

for future questions, prob best to use #datomic (but I'd just leave it here now)

馃憤 1
pithyless14:09:17

(d/q '[:find ?title
         :where
         [?movie :movie/year 1984]
         [?movie :movie/title ?title]] db)
will give you a set of tuples, or if you want just the titles:
(d/q '[:find [?title ...]
         :where
         [?movie :movie/year 1984]
         [?movie :movie/title ?title]] db)

pithyless14:09:38

@UNMBR6ATT this is a good resource (if you're not aware of it): http://www.learndatalogtoday.org

Michael J Dorian14:09:58

Thank you! That worked, I'll check out that page!

pithyless14:09:20

The reason that query works, is because you use the same binding ?movie in two clauses; which means it will do a "join" and only match datums such-that :movie/year is 1984 and :movie/title is ?title for the same entity ?movie

sova-soars-the-sora16:09:05

As a point of learning, What is ?movie going to return if we examine it? ?movie is actually going to be an entity ID that matches for the movie/year 1984 and movie/title ?title (in this case, unbound var that will resolve to anything provided other where clause lines are satisfied)

Michael J Dorian16:09:25

Would that ID be in the form of a long number? I think that was actually the result I got for one of my attempts

sova-soars-the-sora16:09:27

Yes something like that. It might look like #GARBLE i don't recall what shape they take off the top of my head.

Michael J Dorian16:09:51

Interesting. Thanks!

sova-soars-the-sora16:09:51

You could also keep 1984 agnostic as ?year and pass in a value for ?year immediately after the where clauses. Something to keep in mind as you develop queries.

parrot 1
alexmiller16:09:34

entity ids are longs

alexmiller16:09:55

so it would look like a big number

pithyless17:09:38

@UNMBR6ATT I gave a talk last year that covers datalog queries step-by-step; some people have found it useful. Perhaps you may too. 馃檪 https://www.youtube.com/watch?v=oo-7mN9WXTw

Michael J Dorian17:09:51

Definitely need to check that out, thanks!

Joel17:09:08

Curious if anyone has experience turning clojure spec into json schema --- if it's viable/worth the effort?

hiredman17:09:57

I've done it

hiredman17:09:17

I did it to describe an api to our front end guys, but in the end it wasn't enough, you can describe data types with it, but you can't describe an operation (do this, then you get this result) and that was more of what I needed

hiredman17:09:33

but it is neat, and atleast for the trivial things, trivial, because you can get the un-eval'ed form of a spec out of a spec, you just write an interpreter for those

Joel17:09:41

yeah, i was wondering how complete of a json spec you can create. I would only be describing data (preferably to the level of "if you pass this, then this other thing is required"). Actually i didn't know you could specify operations in json schema, or missing what you mean there... sounds like swagger-like (which i don't need)

Joel17:09:09

cool. i think i need to see if/how to compose specs. For example, have a basic spec, where little/nothing is required, but be able to layer on specific/more stringent cases.

hiredman18:09:38

yeah, swagger like (but of course swagger didn't work because it isn't a rest api, and there are async parts, etc)

gekkostate23:09:16

Hi all ! Quick question. I鈥檓 using reitit router on the frontend and the backend. How can I forward urls to the frontend router? I have routes which work like /#/great/person . However, when I navigate to that route using the browser, I鈥檓 redirected to #/ instead of the route.

Gargarismo23:09:15

Not that you won't get an answer here, but check out https://clojurians.slack.com/archives/C7YF1SBT3

Gargarismo23:09:57

I'm willing to bet the chances of finding someone who knows the specs of that library are higher there

gekkostate01:09:08

Sweeet! I鈥檒l try there! Thank you!