This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-04-24
Channels
- # aws-lambda (1)
- # beginners (99)
- # boot (46)
- # cider (8)
- # cljs-dev (20)
- # cljsrn (37)
- # clojure (189)
- # clojure-dev (22)
- # clojure-dusseldorf (28)
- # clojure-italy (1)
- # clojure-russia (28)
- # clojure-spec (10)
- # clojure-uk (33)
- # clojurebridge (1)
- # clojurescript (64)
- # core-matrix (2)
- # css (3)
- # cursive (3)
- # datascript (34)
- # datomic (101)
- # defnpodcast (2)
- # dirac (5)
- # events (1)
- # funcool (3)
- # ldnclj (1)
- # lumo (11)
- # mount (1)
- # off-topic (95)
- # pedestal (2)
- # perun (10)
- # re-frame (3)
- # reagent (6)
- # ring-swagger (4)
- # specter (102)
- # test-check (1)
- # untangled (1)
- # vim (8)
- # yada (17)
@ghadi Ah ok. I temporarily forgot he was giving reverse advice.
is there a reasonable way to compose a function signature? I would like to dispatch to a function if it has the necessary arguments and it would be nice if i didn’t have to specify those twice, once in the signature of the method and then again in the dispatch function. Also, its amazing that that works.
(defmethod get-tags
#(clojure.set/superset? % :base-url :repo :image :user :password)
[{:keys [:base-url :repo :image :user :password]}]
(-> (client/get (str base-url "/api/" repo "/v2/" image "/tags/list") {:basic-auth [user password]})
(get :body)
(json/read-str)
(get "tags")))
or possible that isn’t working
and i need to refresh my repl 😕
ok, yea. clearly you cant specify a function as the dispatch value, but i feel like it would be awesome if you could. Why doesn’t this wizardry exist? Too slow?
its like a que
i should probably just use a que
there's no assignment - you are creating an arg to recur
but pop is a silly way to reverse, because different collections pop from different directions
but how is there no assignment. Even if I'm not assigning anything, the function has to, doesn't it?
your declaring the bindings for the next recursion
it's just as if you did (defn foo [a b] ... (foo (f a) (g b)))
- except the recur
form ensures you don't use stack
I'd use first / rest instead of peek / pop since first and rest are guaranteed to work from the correct end of the collection
or, I would simply put the data into a new collection that builds up in the right direction (too clever for #beginners I think)
you saw conj right? into repeatedly uses conj on each input to attach to the output as it builds it up
into is a lot like a loop that uses conj - it has some other features too (including optimizations, and it takes a transducer arg to transform inputs too if needed)
I think I understand most of what you said... I'm not sure what a transducer arg is but I get the overall idea of into
that it transforms the input should suffice for now
I don't think they are good #beginners material, but transducers are good for isolating data transformation from data context, using them well improves code quality a lot in my experience
Yeah, it looks concise. But the point is that it is more efficient, right? It's not generating the sequence twice.
or five times eg. (comp (map f) (filter pred?) (mapcat g) (take N) (partition-all 3))
if you use the regular functions, each one creates intermediate collections, as a single composed transducer, only one collection need be created (if any) - the consumer of the transducer takes care of that
that's not the only point though - they reduce complexity by isolating what you do with the data from the place the data comes from and where you put it
which incidentally eliminates the creation of lazy-seqs you don't need, but it also simplifies code
Writing your own transducers from scratch is what makes me flinch a bit. The stateful variety looked especially confusing. I'm sure if I wrote a few of them, it'd come easier.
But using the built in versions now, like (map inc)
look even simpler than their traditional counterparts.
;; are always comments
I'm not sure if I'm counting the 0s correctly, but it wants a result larger than the thing it is testing for
so equal will not suffice
what I understood is that it wants it to be smaller than the thing with many 0s. I had just inserted a condition to return the limited value
but that's not larger - it's equal, so it fails
I just realized he wanted something smaller and not equal, but it still returns an error if I make it (- 1000000000000000000000000N 1)
larger
not smaller
the way I remember is that < and > each want args that go in the direction of the symbol's size
< gets larger left to right
> gets smaller left to right, just like the symbol itself
err, > - got ate
how can I generalise the code so that I can generate any number of maps? Should I be thnking about macros here?
macros are almost never the answer when it comes to making a functions more general
But the snippet is not clear enough - what's get-movements
and product-id
?
@foamdino also what do you mean by any number of maps? Like, more arguments to merge-with?
It already supports any amount of maps:
I had everything working here apart from I was using a hardcoded set of maps as input to (merge-with +) when I really needed to dynamically the seq of maps based on the input
@foamdino I'm curious, what are the values of the maps? In Clojure +
will only sum numbers. In cljs it will also concatenate strings but shouldn't be used that way.
O, I see 'qty' - quantity.
I got 99% of this code written in a couple of hours, then bashed my head against a brick wall because merge-with wouldn't accept a LazySeq... TIL about apply 😉
i guess i was thinking it can take an arbitrary number of things to work on and yeah you're right
wait, what treats a list as an indexed array?
OK, because I was going to say, I can’t think of anything that treats a list as an indexed array in clojure, and merge-with isn’t designed to work with either (though it will happily give you garbage back if you use one of them accidentally)
the garbage might even be accidentally correct for your use case…
^^ There you go, closest to a hack you can do with merge-with 😆. I'm leaving it there.
busy trying to learn clojure. used to using apply to pass arguments to a function. but don't really follow the logic here. perhaps something silly, but dont understand how it works here. could anybody explain? (encountered the solution in 4clojure):
@sakalli in this case , (apply map list (partition 2 x))
is likes (map list [1 2] [3 4] [5 6])
that's what apply does
and you need to understand what map
does when given more than one input collection
aah. now i got it... clear. cheers @val_waeselynck
what was the problem statement ?
yeah you rarely use loop
directly in Clojure
yes. have mostly so far used R, so feel a little dirty each time i resort to loop
or for
, although like the recur
I bring this up a lot, but for
is not a loop, it's a list comprehension - it's lazy and not even apropriate for side effects