This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-09-15
Channels
- # aws-lambda (3)
- # beginners (37)
- # boot (294)
- # carry (1)
- # cider (38)
- # cljs-dev (37)
- # cljsjs (88)
- # clojure (187)
- # clojure-android (2)
- # clojure-austin (1)
- # clojure-dusseldorf (9)
- # clojure-hk (3)
- # clojure-italy (12)
- # clojure-russia (36)
- # clojure-spec (55)
- # clojure-uk (27)
- # clojurescript (75)
- # community-development (5)
- # conf-proposals (2)
- # copenhagen-clojurians (3)
- # cursive (9)
- # datomic (54)
- # devcards (5)
- # devops (3)
- # dirac (69)
- # emacs (6)
- # ethereum (1)
- # euroclojure (1)
- # events (3)
- # funcool (1)
- # hoplon (20)
- # immutant (4)
- # luminus (14)
- # midje (4)
- # om (178)
- # om-next (2)
- # onyx (47)
- # pedestal (19)
- # protorepl (20)
- # re-frame (14)
- # reagent (54)
- # ring (2)
- # ring-swagger (7)
- # test-check (10)
- # uncomplicate (11)
- # untangled (9)
- # yada (9)
Thanks @tap for suggesting apply function, that works well. I'd tried map and partial, but not apply. Seems more obvious now.
I’m having terrible trouble using and a partial function. Is it so that I cannot pass a function from bar in require bar :as foo
as argument?
@singen no sure what you're asking
can you show us some code ?
Which resulted in a arityexception that made it look like my function argument was wrong
Would someone mind taking a second to tell me what stupid obvious mistake I'm making here? Trying, just for my own edification, to implement my own (non-lazy) version of filter into a vector, but for some reason it only returns the result of the filter function applied to the last element of the collection. It also seems to not be taking seriously my request to stick it into a vector... I have to be making some silly semantic mistake, but for the life of me I can't see what it is.
(defn flt [func coll]
(let [reducer
(fn [v elem] (if (func elem) (conj v elem)))]
(reduce reducer [] coll)))
(flt even? [1 2 3 4])
; => (4)
(flt even? [1 2 3 4 5 6])
; => (6)
(flt even? [1 2 3 4 5 6 7])
; => nil
First, Clojure on its own will switch collection types around which it feels is best for performance.
Also, reduce
needs to be passed back to it the accumulator else it’s going to just keep taking []
as what it’s conj
ing to.
If you absolutely must have a vector as the result, you need to tell it so once the reduce
is done. Usually into
works well here.
Hmm thanks @akiva --- I must confess I'm super confused by this. Something simple like (reduce conj [] '(1 2 3))
works as expected, without into
or passing reduce back into anything...
It’s probably a closure issue, no pun intended. I’m guessing that when it’s calling the anonymous function outside of the reduce
, it’s resetting itself to factory defaults, so to speak.
Hmm. Interesting. Will have to experiment with defining reducer outside the let block or something. Thanks!
I almost always define functions external to the calling function. I’ve never in my life put a fn
in a let
.
@gowder @akiva: I think the problem is just that the anonymous fn
is missing an else form
ooh. bugger. I always, always forget that conditionals return nil if no else form is supplied, as opposed to just not returning at all.
this is what happens to your brain when you learn programming in imperative languages with an explicit return
No problem! Just try to remember that you always have to return the "result so far" in the reducing function, even if that particular step didn't really change anything
This is where I wish Clojure was a bit more F#-like in that F# won’t even compile a conditional without an else. I think Haskell is the same.
Although I’m almost certain that @alexmiller typed that it’s more efficient to use into
since it’s internally a transducer. But if performance isn’t an issue...