This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2015-12-02
Channels
- # admin-announcements (29)
- # aws (11)
- # beginners (247)
- # boot (11)
- # business (1)
- # cider (73)
- # clara (5)
- # cljs-dev (37)
- # cljsrn (29)
- # clojure (86)
- # clojure-dev (9)
- # clojure-indonesia (1)
- # clojure-italy (3)
- # clojure-nl (1)
- # clojure-russia (195)
- # clojure-sg (2)
- # clojure-uk (3)
- # clojurecup (1)
- # clojurescript (296)
- # clojurex (2)
- # code-reviews (6)
- # core-async (3)
- # cursive (33)
- # datavis (9)
- # datomic (11)
- # funcool (31)
- # hoplon (1)
- # ldnclj (8)
- # lein-figwheel (5)
- # leiningen (5)
- # luminus (4)
- # off-topic (3)
- # om (172)
- # onyx (13)
- # re-frame (5)
- # reagent (84)
Is there are difference between core.async's <! and take! ? Other than the former returns a value and must be used in a go-block, and the latter gives the value to a function
And I guess the answer could be the same as for the same question but about >! and put!
@kauko: <!
returns ‘synchronously’ inside a go block, take!
returns asynchronously from outside of a go block.
take! kind of feels more intuitive to me, but that’s probably because I’m still so used to javascript’s callback way of handling async. I feel like <! is more idiomatic, and should be preferred
Kinda, they’re just for different purposes
but usually it would be more idiomatic to use <!
So...I'm really really really beginning in Clojure, and I've got an idiomatic question. I have a process that munges some data from a file. I have two steps in that process that both have some similar code in them. One function does (into {} (map vector key-vec val-vec)). Another step later on does a similair thing, in fact, it destructures the map back into a vector, does some maths on the value, then repours it back into a map. With some fiddling about I could do both in the same step, but it would be less clear and make the code less reusable. Part of me wants to do it, but I don't know if I should. Thoughts?
Super beginner I know
@rantingbob: you probably want to make it as clear as possible, then if there are performance issues optimise it later
That's what I figured...premature optimization and all that, I just wanted someone to say "yep you're not going mad" thank you
Why do I get this error message : CompilerException java.lang.ClassCastException: java.lang.Long cannot be cast to clojure.lang.IFn on my code : https://www.refheap.com/112259
solved it this way but run into another problem : I have now this (reduce (fn[acc it] (conj acc (drop-last it))) [] (partition number number list) )
but when I do [ 0 1 2 3 4 5 6 7 8] 7 and 8 are cut off.
Of course you won’t want to drop-last from THAT group, so you’ll have to code around that 😉
Last problem to convert it back to a vector I now see [ ( 1 2) ( 4 5) ] instead of [ 1 2 3 4 5]
I have to admit, when you posted it yesterday afternoon I dropped what I was working on and solved it 😉
If it was http://www.4clojure.com/problem/41 then yes
I thought I solved it by ' (apply concat (reduce (fn[acc it] (if (= (count it) 3) (conj acc (drop-last it)) (conj acc it))) [] (partition-all number number list))) '
@snowell: next problem also a nice challenge. split a seq but IM now allowed to use split-at
can you paste it again? is it https://www.refheap.com/112259?
@andrut : here you have : (apply concat (reduce (fn[acc it] (if (= (count it) number) (conj acc (drop-last it)) (conj acc it))) [] (partition-all number list)))
(fn [list number)
(mapcat (fn [partial-list] (take (dec number) partial-list)) (partition-all number list)))
and there no problem with the fact that the last part have only 2 numbers instead of 3
for (f [1 2 3 4 5 6 7 8] 3)
:
[[1 2 3] [4 5 6] [7 8]]
then take 3-1=2 from each partial list
[[1 2] [4 5] [7 8]]
and mapcat
just concat
s them
(defn f [initial-list number]
(loop [list initial-list
result []]
(if (seq (rest list))
(recur (drop number list) (concat result (take (dec number) list)))
result)))
next problem : I have this : (mapcat (fn[acc] (conj acc (take number list) (drop number list))) )
now the answer is a empty () but then I add a [] to the end. I also not see the answer
this is the task : http://www.4clojure.com/problem/49
because if you do (take number list) (drop number list)
, then only the second is returned
@roelof: Clojure’s equality for collection works on the value of the coll regardless whether it’s a list of a vector
thanks, another solved. I find it still difficult when to use mapcat / reduce and when not
I use my favourite editor, write a test, then the function, write a test and use repl extensively; maybe that’ll help ok, bed time
IM puzzeling how to solve this one: (= [1 2 [3 4 5] [1 2 3 4 5]] (let [[a b & c :as d] ] [a b c d]))
oke, If I understand it right this & c :as d
means that the rest of the parameters c are now stored in d
any tip for this exercise : http://www.4clojure.com/problem/83
apply
is probably going to be a feature of your solution
Unless you’re code golfing, you actually probably want to use a fn literal with varags: (fn [& args] …)
I always find http://clojuredocs.org to be a very helpful resource
I look Always here : https://jafingerhut.github.io/cheatsheet/grimoire/cheatsheet-tiptip-cdocs-summary.html
not-every?
expects a function as the first argument and a collection as the second argument
user=> (doc not-every?)
-------------------------
clojure.core/not-every?
([pred coll])
Returns false if (pred x) is logical true for every x in
coll, else true.
nil
You may have to write your own anonymous function for some of the 4clojure problems, and then you could potentially call not-every?
in the implementation of that function
so if you have a function (fn [& bools] …)
then bools
will be bound to a collection of the arguments to your function
@noonian: you mean the idea of this stackoverflow question : http://stackoverflow.com/questions/9242440/how-to-make-a-clojure-function-take-a-variable-number-of-parameters
Ah, that is actually called arity overloading. Multimethods are a form of polymorphism in Clojure and are not related.
That would make sense if you had specific behavior for a fixed set of arities, but the 4clojure problem states that your function should take a “variable number of booleans” so I think you just need one arity that takes n booleans.
instead of having the booleans in specific arguments, a
and b
, adjust your function to operate on a vector a booleans bools
. (def bools [false true false])
Sorry about that, for the 4clojure problem you wouldn’t def anything, but if you were trying this out in a repl you might define a variable holding some booleans to try out functions on them.
cool, so if you call (test-boolean true false true)
inside your function booleans
will be [true false true]
.
So the booleans are a vector then like [ true false] or [ true] or [ false, true. false]
Yes, although there should be a space between the &
and booleans
. I’m not sure if that matters or not to clojure.
so now the functions you were trying before might come in handy, things like not-every?
hmm. I have this (not-every? false? booleans)
but that one fails on (true) and (true true true)
I"m trying to combine my clojurescript app and my compojure app into one:
(defroutes app-routes
(GET "/" [] (-> (resource-response "index.html" {:root "public"})
(content-type "text/html; charset=utf-8")))
(GET "/api/change-log" [] (json-response (get-change-log)))
(GET "/api/task-log" [] (json-response (get-task-log)))
(route/resources "/")
(route/not-found "Not Found"))
(def app app-routes)
As described here: http://zaiste.net/2014/02/web_applications_in_clojure_all_the_way_with_compojure_and_om/ .. when I go to / it attempts to download the html, even after I removed all middleware from wrapping the routes. I see comments on the post with others with the same problem...and their solution doesn't seem to work now..I think this room sees enough churn that people aren’t likely to find your answers when they are solving the problem themselves
BTW figured out out... http://stackoverflow.com/a/14014702 ... Jetty or Tomcat caused conflict with the other solutions i found.
Why would I use transducers over ->> or, given I'm only a beginner, should I just ignore transducers for now? For context, I have a pipeline thats munging a bunch of data. I'm currently using ->> to push data through a bunch of functions. I'd need to learn to write transducers (which I will do at some point anyway) but I'm unsure what benefit it would bring other than intellectual curiosity
Why would you use a transducer over ->>? I think I should maybe come back and watch Rich Hickeys talk again once I'm a bit more experienced
well, a transducer will have better performance than a bunch of maps and filter in sequence because a transducer doesn’t create the intermediate collections
I knew that >_< and now I feel stupid
makes sense