beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
grazfather 2021-03-06T04:42:20.395Z

So I think I am feeling some of the pains of printing with lazy stuff. What’s the ‘proper’ way to do this? I basically map over a list of commands and run sh on it (this is a babashka script) but I get the prints of everything before the actual invocation of the command

grazfather 2021-03-06T04:47:27.395700Z

Is it just do to a doall? I call my shell func repeatedly and that seems to be the main culprit

dpsutton 2021-03-06T04:51:13.396500Z

Reduce or run! Could be helpful, depending on what you need from the result of the calls

🙏 1
grazfather 2021-03-06T04:56:17.397100Z

yeah I need just a list of the results, so dorun won’t work, and I could get reduce to work but doall does exactly what I need

grazfather 2021-03-06T04:56:32.397500Z

I am basically timing how long something takes and want to run it a few times to get an average

grazfather 2021-03-06T14:15:35.404300Z

Thank you, but none of those fit the bill for me. I need to return values

2021-03-06T09:45:11.402Z

Are there any good video tutorials on using Clojure from the ground up? Not just “this is the language” but rather “this is how you set up a good dev environment and how you design and deploy a Clojure application”. It’s totally fine if it’s focused on the language as well as the other parts are there as well. Paid courses are OK.

sb 2021-03-06T09:52:09.402400Z

https://www.jacekschae.com

sb 2021-03-06T09:52:32.403Z

I think that is the best in this topic

👍 1
dharrigan 2021-03-06T10:13:12.403300Z

How about this too? https://practicalli.github.io/clojure/ and his set of very good videos? https://www.youtube.com/channel/UCLsiVY-kWVH1EqgEtZiREJw

👍 1
🙌 1
Audrius 2021-03-06T16:22:39.408300Z

Hello, I have a very weird situation in my REPL (`lein repl`) that can not be normal behavior https://pastebin.com/QC3RtQgj How can require be not available from everywhere :thinking_face: O I am doing something stupid? :face_with_hand_over_mouth:

lassemaatta 2021-03-06T16:26:59.408400Z

Perhaps this explains that behaviour: https://clojuredocs.org/clojure.core/in-ns#example-542692cdc026201cdc326d13

✔️ 1
☝️ 1
2021-03-06T16:31:47.408600Z

i'm not an expert, but

(ns newgsheet3.core
     (:require [clj-time.core :as time]
             [google-apps-clj.credentials :as gauth]
             [google-apps-clj.google-drive :as gdrive]
             [google-apps-clj.google-drive.mime-types :as mime-types]
             [google-apps-clj.google-sheets-v4 :refer :all :as gsheets])
     (:import [com.google.api.services.sheets.v4.model CellData RowData]))
as for yours
(require '[<http://clojure.java.io|clojure.java.io> :as io])

Audrius 2021-03-06T17:26:44.410100Z

what to require to have source and doc available in my REPL it is available in 'user namespace?

teodorlu 2021-03-08T13:47:35.032700Z

(require '[clojure.repl :refer :all]) should also work.

Audrius 2021-03-10T15:35:12.201Z

thanks 😉

🙂 1
dpsutton 2021-03-06T17:29:43.410300Z

(apply require clojure.main/repl-requires)

dpsutton 2021-03-06T17:30:28.410600Z

that's exactly how a clojure main repl starts up.

✅ 1
seancorfield 2021-03-06T18:44:23.410900Z

Yeah, in-ns catches a lot of people out. When you use it on a "new" namespace that has not been loaded (via require), you get a completely "empty" namespace: with not even clojure.core referred into it.

seancorfield 2021-03-06T18:44:48.411100Z

A good rule of thumb is "never use in-ns" (but of course there are times when it is useful).

✔️ 1
seancorfield 2021-03-06T18:45:27.412100Z

Another good rule of thumb is "never type into your REPL" (always evaluate code from your editor), which also side-steps this issue.

Jan Ignatius 2021-03-06T18:46:31.412900Z

Evening all. I'm just starting and the overall Clojure syntax is giving me a bit of a headache. Could some one shine some light to why these two are not equal? (for [x [1 2 3]         y [1 2 3 4]         :when (not= x y)]     [x y])  (def a [1 2 3])  (def b [1 2 3 4])  (for [a b :when (not= a b)][a b])

Jan Ignatius 2021-03-06T18:46:58.413100Z

They evaluate as follows: ([1 2] [1 3] [1 4] [2 1] [2 3] [2 4] [3 1] [3 2] [3 4]) ([1 [1 2 3 4]] [2 [1 2 3 4]] [3 [1 2 3 4]] [4 [1 2 3 4]])

Audrius 2021-03-06T18:48:32.413200Z

I was experimenting in http://nextjournal.com . I wonder can I see some sources of functions but (apply require clojure.main/repl-requires) because I guess it is CLJS. Also this environment is more like REPL as far as I understand.

2021-03-06T18:51:13.414100Z

You likely meant a a b b in the second for

Jan Ignatius 2021-03-06T18:52:24.416200Z

I may have misunderstood how the a and b expand inside the for statement

2021-03-06T18:52:45.416900Z

Imagine it is a let

2021-03-06T18:53:37.418200Z

The difference is in a let a name is bound to the given value, but in a for a name is bound to each element in the given seq

Jan Ignatius 2021-03-06T18:57:11.418500Z

I'll need to spend some time with this, it seems 😄

Jan Ignatius 2021-03-06T18:57:16.418800Z

Thank you for the help.

futurile 2021-03-06T18:59:18.418900Z

I personally find for quite difficult, I was told you should mostly use Map or Filter

Jan Ignatius 2021-03-06T19:01:08.419500Z

Seems I need to read it as "For a in a and b in b ..."

seancorfield 2021-03-06T19:12:35.420500Z

for can definitely be counter-intuitive/surprising for folks coming to Clojure from other languages where for is a loop rather than a comprehension.

seancorfield 2021-03-06T19:13:35.421500Z

Once you get used to it, it can sometimes make code a lot more readable than a combination of map and filter -- if you specifically need some lazy processing of data.

seancorfield 2021-03-06T19:17:59.425100Z

@email113 Specifically for your example, you can get there in a series of small steps that transform your code:

(for [x [1 2 3] y [1 2 3 4] :when (not= x y)] [x y])
;; define a and b as you have above
(for [x a y b :when (not= x y)] [x y]) ; pure substitution of value with symbol bound to same value
(for [a a y b :when (not= a y)] [a y]) ; rename local symbol x to a
(for [a a b b :when (not= a b)] [a b]) ; rename local symbol y to b
When you have immutable data, substitution and renaming become simpler and more mechanical.

Jan Ignatius 2021-03-06T19:22:06.427100Z

So I could use any unused variable name inside the for that I just need to assign/bind(?) to a and b respectively, something like: (def a [1 2 3]) (def b [1 2 3 4]) (for [asdf a hjkl b :when (not= asdf hjkl)] [asdf hjkl]

Jan Ignatius 2021-03-06T19:22:28.427400Z

I think the re-use of the variable name threw me a bit

Henri Schmidt 2021-03-06T19:32:46.428200Z

@email113 the names bind to the elements of sequence a and b respectively. Not to the sequences themselves.

seancorfield 2021-03-06T19:40:38.429400Z

Right, hence hiredman's comment that it is like let: for introduces new, local symbols bound to (elements of) sequences, as does loop (which also isn't like loops in other languages).

Jim Strieter 2021-03-06T19:48:01.431Z

Noob here Is there a way to use some-> with & args? For instance: (defn f [x & funcList] (some-> x funcList)) ?

phronmophobic 2021-03-06T20:02:09.431500Z

there's probably a cleaner way to write it, but it can be accomplished with reduce:

(reduce (fn [x f]
          (let [new-x (f x)]
            (if new-x
              new-x
              (reduced nil))))
        1
        [inc
         inc
         ;; (constantly nil)
         inc])

🙌 1
Jim Strieter 2021-03-06T20:06:30.432500Z

Is there a way to write a macro to do this?

phronmophobic 2021-03-06T20:09:07.433900Z

There's not much benefit in writing a macro for this example. You could write a function that does this.

phronmophobic 2021-03-06T20:09:33.434400Z

It possibly already exists in clojure.core.

seancorfield 2021-03-06T20:12:13.436100Z

@smith.adriane If x is nil, you would want to short-circuit that earlier, which simplifies the code anyway: (reduce (fn [x f] (if (some? x) (f x) (reduced nil))) x func-list)

👍 2
😬 1
✅ 1
sova-soars-the-sora 2021-03-06T20:13:56.436900Z

What does it do?

Jim Strieter 2021-03-06T20:20:03.437500Z

I want to pass in some parameter x followed by a list of functions fList

Jim Strieter 2021-03-06T20:20:43.438400Z

A single call to the outer function should feed x into the first function in fList, pass that result into the 2nd, etc. until all functions have been called

Jim Strieter 2021-03-06T20:21:54.439100Z

I wrote a function that does this, but I'm trying to learn Clojure libraries so I wanted to check if there's a well known function that already does that

seancorfield 2021-03-06T20:25:54.439400Z

But you also want to stop when you hit a nil right?

seancorfield 2021-03-06T20:26:42.440200Z

Otherwise you could just do ((apply comp func-list) x) or (reduce (fn [x f] (f x)) x func-list)

Jim Strieter 2021-03-07T13:37:19.459Z

Thanks Sean!

Jim Strieter 2021-03-07T13:37:34.459200Z

☺️

Jim Strieter 2021-03-07T13:43:35.459400Z

I want to use this to cascade method calls on a large Java object. Large in that it takes up a few 100 MB RAM. Is Clojure going to mutate the state of that instance for every method call? (I'm not opposed to immutability in and of itself, but if the JVM has to copy several MB by value for each method call, I need to find a mutable way of doing it.)

seancorfield 2021-03-07T17:50:42.491Z

If it's a Java object, it follows Java rules and it will do whatever the methods on it do. If it is a Clojure data structure (immutable), then it will use structural sharing and only copy the minimum that it needs to. For the latter, you can often use transient/`persistent!` around the pipeline of operations to allow Clojure to optimize the mutation/copying.

sova-soars-the-sora 2021-03-06T20:26:54.440500Z

yeah I was thinking comp 😄

sova-soars-the-sora 2021-03-06T20:27:00.440700Z

https://clojuredocs.org/clojure.core/comp

sova-soars-the-sora 2021-03-06T20:27:33.441400Z

relevant things: apply juxt comp partial

sova-soars-the-sora 2021-03-06T20:32:35.443400Z

So I have a question. Sometimes users have an outdated copy of the login page for one of our applications. Which throws an anti-forgery error when they try to log in. Is there a way to make sure the page is fetched freshly when they open it up even if its cached? Or is there some straightforward solution to this outdated x-csrf token?

sova-soars-the-sora 2021-03-06T20:51:29.443600Z

maybe javascript?

sova-soars-the-sora 2021-03-06T22:35:12.444Z

Seems like

&lt;meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" /&gt;
will do the trick

phronmophobic 2021-03-06T22:39:32.444300Z

from https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control : > no-cache > The response may be stored by any cache, even if the response is normally non-cacheable. However, the stored response MUST always go through validation with the origin server first before using it, therefore, you cannot use no-cache in-conjunction with immutable. If you mean to not store the response in any cache, use no-store instead. This directive is not effective in preventing caches from storing your response. > > no-store > The response may not be stored in any cache. Note that this will not prevent a valid pre-existing cached response being returned. Clients can set `max-age=0` to also clear existing cache responses, as this forces the cache to revalidate with the server (no other directives have an effect when used with `no-store`).

phronmophobic 2021-03-06T22:40:21.445200Z

I think the no-cache is superfluous alongside no-store