Fork me on GitHub
#beginners
<
2019-10-14
>
lockdown-00:10:34

Is there a better way to achieve (keyword (name :foo/bar)) ?

seancorfield01:10:33

@lockdown- To "unqualify" a keyword? No, that seems a reasonable way to do it.

mbbailey9605:10:37

When using the ->> macro, how to take the result of the last line (which will be a seq of some sort) and operate on each part. In other words, how can i accomplish `(->>

mbbailey9605:10:27

`(->> (returns-seq) (for [x ,,,]))`

mbbailey9605:10:54

I am looking into the as-> macro but I am having trouble understanding how it works

seancorfield05:10:13

@mbbailey96 start your pipeline with ->

seancorfield05:10:00

(-> expr
    (->> your pipeline here)
    (as-> s (for [x s] ,,,)))

mbbailey9605:10:00

@seancorfield thank you!

fabrao05:10:28

Hello all, how to I pass operator for a filter? I know that is wrong, but how is the correct way to do this?

(defn- todos-alarmes-filtrados-por-chave [endereco chave valor & {:keys [operador] :or {operador .contains}}]
  (->> (alarme/todos-alarmes endereco)
       (filter #(not (nil? (chave %))))
       (filter (apply operador (chave %) valor))))

jaihindhreddy06:10:59

Tiny style point. You can use remove instead of filtering with complement of predicate (filter #(not (nil? (chave %)))) is same as (remove #(nil? (chave %))) is same as (remove (comp nil? chave))

jaihindhreddy06:10:27

What is operador here? Not quite sure what the problem is.

jarvinenemil05:10:07

When using protocols, do you usually use them for your SPI's? I've started to get the habit to define protocols for all my SPI actions (save user-information to a database etc), since it is easy to create a "FakeStorage"-implementation.

seancorfield05:10:39

@fabrao Not sure what your question is here? What doesn't work about that code?

seancorfield05:10:06

@jarvinenemil Perhaps you are over-using protocols?

fabrao05:10:08

Unable to resolve symbol: .contains in this context <-

fabrao05:10:58

I don´t know how to define .contains as 'default' operator

seancorfield05:10:13

Oh, @fabrao you can't use Java methods directly you need to wrap them in Clojure functions: #(.contains %1 %2)

jarvinenemil05:10:01

Yeah @seancorfield i still get the notion that I am thinking too much in Java-interface terms when using Protocols in Clojure (sometimes).

seancorfield05:10:44

There's no point in using protocols unless you have multiple implementations for them, IMO.

fabrao05:10:38

{:keys [operador] :or {operador #(.contains %1 %2)}} ?

seancorfield05:10:07

@fabrao Something like that, yes

jarvinenemil05:10:27

@seancorfield currently I have two record implementations for each protocol, one implementation that runs in the prod-environment and one that is used in tests

seancorfield05:10:56

Mmm, still sounds a bit unnecessary...

seancorfield05:10:21

That's a very OO way of dealing with mocking for tests.

jarvinenemil05:10:02

Indeed, I need to find another way of dealing with the mocking for tests. Thinking of using with-redefs instead

jarvinenemil05:10:18

(I did read that one should avoid that though)..

jysandy06:10:55

@jarvinenemil that really depends on context. It’s fine for most use cases.

jarvinenemil07:10:41

@ multithreaded tests comes into mind for me

jysandy07:10:13

with-redefs is problematic for parallel tests, that is correct.

seancorfield05:10:57

If you can refactor to pure data and function arguments, you'll probably be better off...

jarvinenemil05:10:48

I will try some ideas, in my current implementations all state is defined as function arguments. I just need to remove some records 🙂

jarvinenemil07:10:23

I found this comment section quite interesting: (Protocols and records): https://www.reddit.com/r/Clojure/comments/42nkkw/how_i_use_component/

juan.ignacio84816:10:34

(defmethod pp :a [ g #SOMETHING]...) is #_SOMETHING like a comment?

alexmiller17:10:46

it's a special reader macro that says to skip the next form read. So here SOMETHING will be read (as a symbol) and thrown away

noisesmith17:10:05

one useful variant:

user=> {:a 0 #_#_ :b 1 :c 2}
{:a 0, :c 2}
inline comment of key/value pair by stacking #_ inside a hash-map

noisesmith17:10:41

(maybe that only works if you consciously adapt the idiom, but #_#_ is pretty unmistakable once you learn to recognize it)

juan.ignacio84817:10:25

That means skip : b and 1? (the next two forms)?

noisesmith17:10:19

another variant I like - sadly cljfmt doesn't like it though:

#_
(defn broken-on-load
 "commented out for now, but will uncomment when it doesn't break the namespace" 
 []
 ...)

noisesmith17:10:29

the advantage is in git it doesn't add whitespace diffs

noisesmith17:10:37

and it's less invasive than (comment ...) which would do the same thing (and imposes indentation based on standard paren nesting rules, where the reader macro doesn't)

juan.ignacio84817:10:16

makes sense (once you learn to recognize it, as you said before) !