Fork me on GitHub
#clojure
<
2023-11-21
>
vlaaad12:11:49

is there something in clojure core that act like or , but is a runtime value (instead of a macro)? i.e. for use like (reduce or true bools)

delaguardo12:11:51

(reduce #(or %1 %2) true bools)

Martín Varela12:11:25

just don't put true for the init value...

vlaaad12:11:37

yeah I know I can make an anon fn, the question is if something like this already exists in core

Martín Varela12:11:57

not that I know of

delaguardo12:11:34

(some true? ...) maybe? but unlike reduce it will give you nil in case of all false

vlaaad12:11:48

Btw, there is (some-fn true?) that acts that way, but I find #(or %1 %2) better at communicating the intent, so I guess I’ll stick to that

👆 2
oyakushev14:11:04

Could also do either (some identity bools) or (some true? bools), it returns nil if all are false, but has the benefit of early termination.

vlaaad14:11:46

my real context was (update acc :bool-field or another-bool) , so there was no coll of bools, that was an example…

👍 2
daveliepmann12:11:52

Is a double colon legal syntax in the middle of a var name? Example in 🧵

daveliepmann12:11:59

$ clj
Clojure 1.11.1
user=> (def foo:bar 5)
#'user/foo:bar
user=> foo:bar
5
user=> (def baz::qux 10)
Syntax error reading source at (REPL:3:14).
Invalid token: baz::qux
10
Syntax error reading source at (REPL:3:18).
Unmatched delimiter: )
user=>

p-himik12:11:06

The error is pretty clear, no? From https://clojure.org/reference/reader#_symbols: > • Symbols beginning or ending with ':' are reserved by Clojure. A symbol can contain one or more non-repeating ':'s.

daveliepmann12:11:32

Context: I've been using single colons as a light-weight grouping prefix, e.g. widget:foo and widget:bar. In the context of debugging clojure-mode's failure to syntax-highlight this correctly (https://github.com/clojure-emacs/clojure-mode/issues/653), I ran into this.

daveliepmann12:11:40

@U2FRKM4TW the section you quote suggests that the syntax error is a bug, no?

p-himik13:11:27

No need to remove the original message though. It just creates clutter since the thread is still there.

👍 1
timvisher14:11:40

Is there anything that lets me turn an arbitrary form into the string of what would be executed if it was evaled? Essentially, IIUC, going from

(println foo (bar) (…))
to
`(println ~foo ~(bar) ~(…))
I think? So you evaluate all the subforms but leave the final outer form unevaled with just the values that would be passed to it. My intent is something akin to a 'dry run' mode in a program I'm writing where I can log the form that would be evaluated but with the arg values in place.

p-himik14:11:16

You can write a macro that rewrites the initial form.

timvisher14:11:20

For sure. Wondering if one like it already exists. :)

valerauko15:11:42

I just had to

(defmacro debug
  [body]
  (if (seq? body)
    `(list '~(first body) ~@(rest body))
    body))
#'user/debug
(debug (println (+ 1 2) (- 2 1)))
(println 3 1)

timvisher15:11:20

(defmacro dry-run
    [[f & b :as body] & _]
    `(log/infof "Would execute ‘%s’"
                (list (quote ~f) ~@b)))

timvisher15:11:16

@UAEH11THP Thanks! Yours looks more robust. :)

valerauko15:11:46

it's the same as yours haha (other than the consideration "what if body is a plain value") mine still breaks probably if it's used on a macro (like (debug (-> foo (bar 1) (baz 2))) )

timvisher16:11:18

Yeah. I imagine that something that would be truly robust here would actually be a bit difficult.

valerauko16:11:17

hmm if only macros then

(defmacro debug
  [body]
  (if (seq? body)
    `(if (not (:macro (meta (var ~(first body)))))
       (list '~(first body) ~@(rest body))
       ~body)
    body))

emccue15:11:07

Does anyone know if it is technically permissible to send multiple cookies with the same name in one request? like how urlparams you can do a=b&a=c to get multiple values

dharrigan16:11:04

My feeling is whilst it's possible (after all, it's just k/v pairs on the response), the browser's behaviour might make it indeterminate

dharrigan16:11:59

if you have Set-Cookie: foo=bar;foo=baz then it might be interesting to see if the browser accepts that and what happens when it sends it back to the server.

dpsutton16:11:47

https://httpwg.org/specs/rfc6265.html#sane-set-cookie > If the user agent receives a new cookie with the same cookie-name, domain-value, and path-value as a cookie that it has already stored, the existing cookie is evicted and replaced with the new cookie.

dharrigan16:11:34

You would be breaking the specs 'tho

dpsutton16:11:35

> Although cookies are serialized linearly in the Cookie header, servers SHOULD NOT rely upon the serialization order. In particular, if the Cookie header contains two cookies with the same name (e.g., that were set with different Path or Domain attributes), servers SHOULD NOT rely upon the order in which these cookies appear in the header.

dharrigan16:11:36

` Servers SHOULD NOT include more than one Set-Cookie header field in the same response with the same cookie-name. (See Section 5.2 for how user agents handle this case.)`

dharrigan16:11:13

Interesting stuff

emccue16:11:36

I know with a Set-Cookie header there is defined behavior, i'm thinking of the Cookie header

emccue16:11:58

>> e.g., that were set with different Path or Domain attributes >

emccue16:11:18

so it sounds like you can make a browser send 2 values for the same cookie?

Ben Lieberman17:11:28

I'm finding this verbiage (in bold) difficult to understand (from docstring for clojure.data.json/read):

:value-fn function

      Function to transform values in maps ("objects" in JSON) in
      the output. For each JSON property, value-fn is called with
      two arguments: the property name (transformed by key-fn) and
      the value. The return value of value-fn will replace the value
      in the output. If value-fn returns itself, the property will
      be omitted from the output. The default value-fn returns the
      value unchanged. This option does not apply to non-map
      collections.
If value-fn returns itself, the property will be omitted from the output. That sounds like identity but I am not sure.

delaguardo17:11:36

not identity but something similar to (fn foo [k v] foo)

Ben Lieberman17:11:10

makes sense now, thanks. seems like a bit of a strange choice on implementation though.

delaguardo17:11:13

this is the way to effectively remove some properties from output

dpsutton18:11:40

it’s a clever way to indicate “omit this” without a sigil that could be a valid object in json

dpsutton18:11:46

(if-not (= value-fn out-value)
  (assoc! result key out-value)
  result)

dpsutton18:11:14

it’s an unambiguous object in memory available to both the value-fn definition and the calling code

dpsutton18:11:23

kind of a hack but clever

Ben Lieberman18:11:14

Definitely too clever for my very smooth brain. I understand now with the examples provided, though, thanks @U11BV7MTK

ghadi19:11:56

wow, I didn't know it had that knob

ghadi19:11:08

it's actually a common pattern for "impossible" values

ghadi19:11:57

promise uses a CountDownLatch for coordination and an atom for the result, the atom's initial value is the CountDownLatch

🧠 3
David G20:11:37

Is there a doc about function naming conventions? Are they usually named with a verb or just the thing it returns? For example, say I have an env that sometimes is called (by mistake) test but it should be staging. I write a function to correct it, would the convention be to call (correct-env env) or (corrected-env env)? Or is there's no consensus on that

Alex Miller (Clojure team)21:11:08

verbs are good. noun may be implied by the arg name and/or namespace context so might not even be necessary. "correct" seems like it doesn't say much about what is being corrected or what a correction would be, so if there's something more specific, I'd try to use more specific words ("staging" seems like an important word here for example)

🙏 1
seancorfield21:11:53

Zach Tellman's "Elements of Clojure" book has a lot of good advice on naming https://elementsofclojure.com/

🙏 2
seancorfield21:11:10

And the community style guide has a big section on naming things https://guide.clojure.style/#naming

David G21:11:19

Awesome, thanks!