This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2015-11-25
Channels
- # admin-announcements (3)
- # beginners (165)
- # boot (123)
- # cider (106)
- # clara (1)
- # cljsrn (20)
- # clojure (199)
- # clojure-canada (2)
- # clojure-dev (3)
- # clojure-poland (29)
- # clojure-russia (7)
- # clojure-taiwan (2)
- # clojurescript (487)
- # cursive (25)
- # datavis (89)
- # datomic (26)
- # gorilla (2)
- # hoplon (15)
- # ldnclj (12)
- # lein-figwheel (9)
- # leiningen (2)
- # liberator (1)
- # off-topic (25)
- # om (380)
- # onyx (26)
- # parinfer (52)
- # portland-or (12)
- # re-frame (28)
- # reagent (132)
Hi @nando ! No, that whole approach — which is common in procedural code — just doesn’t fly in FP.
About the closest you get is loop
/ recur
where you have an "accumulator" variable that you rebind in the recur
...
(loop [n 5 acc 0] (if (zero? n) acc (recur (dec n) (+ acc (* n n)))))
which produces 55
but even there the more idiomatic approach is to use map / reduce: (reduce + (map (fn [n] (* n n)) (range 1 6)))
or: (reduce + (map #(* % %) (range 1 6)))
if you prefer the shorthand version of (fn …)
@nando: one thing that helped “twist my brain” was to draw on paper. Draw the data you have and the result you want, and then start sketching the steps. Granted, this can be difficult in the beginning when you don’t know the tools you have at your disposal
@seancorfield: Thanks very much for the clarification. I’m going to try #30 again with reduce after rereading some material on it last night.
@kauko: That’s a good suggestion! I've used pen and paper when I have a difficult algorithm to work through procedurally, and I hadn’t yet thought to try that with Clojure.
Hello all. Does anyone here using Atom for Clojure development?
Cool, I’m asking it because I’m using Atom for working with other languages and it would be great to use the same tool for all my needs…
I have a question for myself. Why is this one false : (= {:line 13, :column 28, :end-line 13, :end-column 34, :division "West"} (meta '^{:division "West"} Giants))
@adlermedrado: if I google it looks like you can use Atom for clojure
Great.
@adlermedrado: but maybe I will keep track of Atom. Im not complete satisfied with Light table
@adlermedrado: I’ve found several packages for Atom for developing with Clojure
Proto REPL https://atom.io/packages/proto-repl Parinfer for Atom https://atom.io/packages/Parinfer linter-clojure https://atom.io/packages/linter-clojure
Thank you. I’ll try it.
Derek Slager mentioned parinfer in his talk ClojureScript for Skeptics https://www.youtube.com/watch?v=gsffg5xxFQI&index=8&list=PLZdCLR02grLrl5ie970A24kvti21hGiOf
I haven’t had time to try the Clojure plugins on Atom - working at the moment in my day job
… but I just took a moment to open a clj file in Atom and started the REPL and executed a block.
Seems to work fine, although the REPL is in a separate window side by side with the code, rather than inline as is the case in Light Table
nando : IM trying to solve the koans found here : https://github.com/functional-koans/clojure-koans/blob/master/src/koans/23_meta.clj and with Cursive and Light table no success
Nando : how can I send something to the repl of a external file. I have opened the koans file and want to evalute a function
@roelof: what's meta and what is Giants and why is it uppercase?
@meikemertsch: when Im done with that , I solved all koans and I will start with 4clojure
maybe this one : https://en.wikipedia.org/wiki/San_Francisco_Giants
the only reason Clojure would complain about not being able to resolve Giants
is if it's trying to evaluate it
oke, if I look at the Original it looks like this : (= (meta '^{:division "West"} Giants))
I've never used cursive. I'm just suggesting you to start a new repl session and evaluate that single line to make sure nothing is messing with your env
@roelof: To open a REPL in Atom, first install the packages I’ve listed before. Then from the Packages menu, go to proto-repl > Start REPL
;; You can disable this help text in the settings. ;; Loading REPL... Error starting repl: Error: spawn lein ENOENT You may need to configure the lein path in proto-repl settings REPL Closed
@roelof not sure if you've solved it, but your first post looks like you were using LightTable. It was adding extra metadata to the "Giants" entity (the :line, :column, etc. stuff). Running in a "bare" REPL, like 'lein repl' shows the answer as the koan expects. I like LT, but sometimes it's a bit too helpful.
I know, when I only have this (= {:division "West"} (meta '^{:division "West"} Giants)) LT says wrong where a bare repl says im right
or do we misunderstood each other. I like to see that LT is saying that the koan is right
@samflores: ping
@roelof that's absolutely correct. The two repls give different answers because LT is adding more metadata on its own. It looks like it adds line numbering metadata probably for live-repl usage so it can show the results in the right places.
I saw when I change it to this : (= {:division "West"} (meta '^{:division "West"} 'Giants)) can work with it
Im trying to solve a challenge from 4 clojure , Where I have to duplicate every item in a list
So I did https://www.refheap.com/112064 but now I see ( ( 1 1) ( 2 2) ( 3 3) instead of ( 1 1 2 2 3 3)
Is there a way I can make from ( () ()) to () again. I have looked at the cheat-sheet but it do not seem to be a function for it
I think the issue with using map is that it will always return the same number of elements in the seq
The problem is I think how I take the element that reduce has , make a argument for repeat
@roelof: take a look at mapcat
and compare it to map
— that should help you with duplicating the list items.
Mornin’ @nando ! Great to see you here my friend!
seancorfield: chips, now no output : https://www.refheap.com/112069
You’re passing two lists to mapcat
— one is empty so mapcat
stops processing.
Both map
and mapcat
can accept multiple lists and apply functions to elements of each of those lists.
Your anonymous function is still designed for reduce
, not map
(or mapcat
).
In your original solution — that produced ((1 1) (2 2) (3 3))
you were very close, and that was where I was suggesting mapcat
instead of map
.
@seancorfield: you mean the one where I use partial ?
Oh, I didn’t even look at the code — I just assumed from your answer you had used map
… I see now you didn’t (and your code was rather more complicated).
Ah, wait, you do have map
in there… what’s the merge
for?
OK, so let’s go back to first principles: given a list of items, you want to duplicate each one. So that sounds like a map
problem and there are lots of ways to "duplicate" an item. I’d probably use vector
: (map #(vector % %) my-list)
or (map (fn [x] [x x]) my-list)
As you found, that produces a sequence of pairs so you want those concatenated which is what mapcat
does, compared to map
.
I hadn’t thought of using replicate
— that’s a good approach too!
Then the next one is also easy. I have to replicate a number of times. So I can make a variable of the number
@seancorfield: so my solution is good
One of the hardest things I found when learning Clojure was becoming familiar with enough of the functions in core to reach for the correct one when I’m "close" to a solution.
I’d often reach a complicated solution, show it to more experienced Clojurians, and they’d say "Take a look at… (some new-to-me function)" and it was a revelation: always making my solutions simpler!
Sounds like you’re having fun with 4clojure?
@seancorfield: I looked how other people have solved it
@seancorfield: can you say if Im on the right track with the reduce function ?
Sorry, had to step away to talk to an electrician...
That works because conj
takes a collection and any number of additional items to add to it — and conj
on a vector adds the elements at the end (`conj` on a list adds them at the beginning)
eight.core=> (conj [1 2] 3 4)
[1 2 3 4]
eight.core=> (conj '(1 2) 3 4)
(4 3 1 2)
So the function reduce
takes is called with the initial value []
and the first element of the list, then with the result of that call and the second element of the list and so on.
So you get (conj [] 1 1)
which produces [1 1]
and then (conj [1 1] 2 2)
produces [1 1 2 2]
In a functional language, when working with pure functions (no side effects), you can think of "evaluation" as pure substitution rules which sometimes helps when writing out the solution’s evaluation by hand.
Yes, that’s correct. Sorry, I was away working
You too! Remember most Americans will be away from their computers for a few days: tomorrow is Thanksgiving and most Americans take Thursday and Friday off.
I’m off tomorrow (it’s my wife’s birthday) but I’ll be around Friday (working).