This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-11-13
Channels
- # beginners (43)
- # boot (318)
- # cider (1)
- # cljs-dev (6)
- # clojure (30)
- # clojure-brasil (2)
- # clojure-korea (2)
- # clojure-russia (31)
- # clojure-spec (13)
- # clojure-uk (7)
- # clojurebridge (1)
- # clojurescript (17)
- # code-reviews (1)
- # cursive (2)
- # data-science (1)
- # datascript (10)
- # hoplon (116)
- # javascript (2)
- # juxt (1)
- # mount (2)
- # onyx (12)
- # parinfer (2)
- # pedestal (1)
- # protorepl (1)
- # re-frame (3)
- # ring (1)
- # untangled (1)
Can someone help me figure out how to solve this one :
"They can easily emulate mapping"
(= '(0 1 4 9 16 25)
(map (fn [x] (* x x))
(range 6))
(for [x (range 6)]
__))
@roelofw They are trying to show you that for
implicitly acts like it is mapping a function in this case. Have you taken a look at what for
does?
Wondering what the significance of a quote after a function means, for example in the source of the range
function I saw this expression (iterate inc' 0)
. I understand that ' before is a syntax shortcut for quote, is this the same if the quote is after the function name?
So I have to make a anymous function which does the same as in the map before it. @mfikes ?
@roelofw: no. While map
applies a function, for
is a macro that essentially evaluates its body once for each value, producing a sequence
@jr0cket: A lot of the numerical functions have "prime" versions that support arbitrary precision
@mfikes I mean the same as in this example with the let :
(for [x [0 1 2 3 4 5]
:let [y (* x 3)]
:when (even? y)]
@roelofw you could use a :let
in the for
to bind the square of x
to a symbol, but there is a simpler way...
What would you do with this, for example @roelofw
(for [x (range 6)
:let [y (* x x)]]
,,,)
@mfikes ah, so there is inc and inc', that makes sense. Thanks
@roelofw: that would produce a sequence of 6 values, where each value is the identity function. Whatever you put in the body of the for
is what will be evaluated (not called) and included in the output sequence
@roelofw: in the example above with :let
, for the first element in the output sequence (associated with x
being bound to 0
), the symbol y
will be bound to the value 0
. Then y
will be bound to 1
, and then 4
, ...
The first one gives a output of ( 0 1 2 3 4 5 ) and the second one ( 0 -1 -2 -3 -4 -5 -6 ) @mfikes
which is the same as (for [x (range 6) (fn [x] (* x x )))
if I do not forget a ( @mfikes
but then the ,, has to be replaced with nothing as I understand what you mean @mfikes
@roelof No… the body of the for
loop needs to contain the expression that will be used to form the elements of the output sequence. You need to move your square expression there.
@roelof This is very close to correct:
(for [x (range 6)] x * x )
but instead, you write x * x
as (* x x)