This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-11-04
Channels
- # 100-days-of-code (1)
- # announcements (1)
- # beginners (51)
- # boot (1)
- # calva (1)
- # cider (16)
- # cljsrn (2)
- # clojure (52)
- # clojure-spec (19)
- # clojure-uk (8)
- # clojurescript (8)
- # events (1)
- # figwheel-main (8)
- # fulcro (57)
- # midje (1)
- # nrepl (10)
- # off-topic (38)
- # pedestal (4)
- # portkey (1)
- # reagent (3)
- # reitit (4)
- # spacemacs (8)
clojure-scratch.core> (defn curried-add [x] (fn [y] (+ x y)))
#'clojure-scratch.core/curried-add
clojure-scratch.core> (-> 1 (curried-add 1))
ArityException Wrong number of args (2) passed to: core/curried-add clojure.lang.AFn.throwArity (AFn.java:429)
clojure-scratch.core> (-> 1 ((curried-add 1)))
2
why do I need the extra set of parens?
the expression (curried-add 1)
is already a function of arity 1
so (-> 1 (x 1))
is (x 1 1)
, and if x
is a function that only takes 1 argument, that is going to throw an error when it runs
oh, i thought (-> x (y))
would be something like (apply (y) x)
oh, maybe my mistake was thinking that the expression (curried-add 1)
would evaluate to a unary function before being rearranged by ->
but it's done in the opposite order
Correct. Macros apply first. Then code is evaluated.
(because macros take "code as data" and produce "code as data" -- and then that "data" is read (into code) and evaluated)
is there a function, then, like (function-version-of-thread-first-macro val f1 f2 f3)
that produces the equivalent of (f3 (f2 (f1 val)))
?
With ->
at the beginning, you mean? Yes.
Ah, to your edited question: no.
The closest would be (reduce (fn [v f] (f v)) val [f1 f2 f3]))
thank you!
The decomplecting of dispatch and taxonomies through multimethods and hierarchies is amazing. But where can I find a non-trivial usage of this in the wild, prefer-method
and all?
The use of taxonomies starts to approach towards the OOP design pattern, and people tend to avoid that in Clojure.
The closest I've seen is in @U0BKWMG5B's talk about integrant.
I know it's also used in Clojure for printing. Where everything has a default print, and prefer-method is used to specify specific prints for child objects.
@U0K064KQV oh yeah! Thanks!
how do i use private github repos with deps.edn? i'm getting the following error: `Cloning: [email protected]:myorg/myrepo.git Error building classpath. [email protected]:myorg/myrepo.git: Auth fail`
(some #(= % [i j]) my-list)
works, but just wondered if something like contains?
existed for values instead of keys
You can use .contains
. Since Java defines a value based contains (as opposed to the key based one of clojure.core) for all Collection types.
not that I'm aware of! I generally use the some method, but have wondered if it's better to cast the vector/list into a set. (contains? (set my-list) element)
Another somewhat related approach is to employ .indexOf
, perhaps combined with nat-int?
This will work in Clojure, and is also guaranteed to work in ClojureScript: https://clojurescript.org/reference/javascript-api#indexOf
Is there an idiomatic way to do a filter w/ multiple functions+accumulators such that you can compute several unrelated values via a single iteration? I can’t think of a way to do it without using a vector or dict as the accumulator which seems not great to me.
I'd have a look at transjuxt
from https://github.com/cgrand/xforms
It lets you do this with transducers.
You could also use a normal juxt
if you don't care about performance, but only code clarity.
((juxt (partial filter even?)
(partial filter odd?))
[1 2 3 4])
And if you do care about performance, and don't want to use transducers, look at juxt-reduce
from this so answer: https://stackoverflow.com/a/36321312/172272(reduce (fn [m v]
(cond-> m
(even? v) (update :even conj v)
(odd? v) (update :odd conj v)))
{:odd []
:even []}
(range 1 10))
;; => {:odd [1 3 5 7 9] :even [2 4 6 8]}
The above was basically what I had in mind (using a map as an accumulator). I just wasn’t sure if that was the most common/idiomatic way to handle that, especially for a larger number of operations/results.
yeah, my example would definitely be better off in group by, but part of what @risinglight was asking for was filtering and applying functions totally independently of each other in one iteration - which group-by would not be ideal for
maybe this is a better example where group-by wouldn't work
(reduce (fn [m v]
(let [c (fnil conj [])]
(cond-> m
(even? v) (update :even c v)
(odd? v) (update :odd c v)
:always (update :squares c (* v v)))))
{}
(range 1 10))
;; => {:odd [1 3 5 7 9]
;; :even [2 4 6 8]
;; :squares [1 4 9 16 25 36 49 64 81]}
I'd have a look at transjuxt
from https://github.com/cgrand/xforms
It lets you do this with transducers.
You could also use a normal juxt
if you don't care about performance, but only code clarity.
((juxt (partial filter even?)
(partial filter odd?))
[1 2 3 4])
And if you do care about performance, and don't want to use transducers, look at juxt-reduce
from this so answer: https://stackoverflow.com/a/36321312/172272