This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-11-23
Channels
- # admin-announcements (1)
- # arachne (3)
- # aws (1)
- # bangalore-clj (2)
- # beginners (209)
- # boot (81)
- # capetown (2)
- # cider (16)
- # clara (13)
- # cljsjs (16)
- # cljsrn (6)
- # clojure (217)
- # clojure-czech (1)
- # clojure-greece (4)
- # clojure-italy (3)
- # clojure-korea (3)
- # clojure-russia (3)
- # clojure-sg (3)
- # clojure-spec (104)
- # clojure-uk (23)
- # clojurescript (7)
- # component (7)
- # cursive (18)
- # datomic (12)
- # devcards (34)
- # dirac (17)
- # editors (3)
- # emacs (1)
- # events (1)
- # hoplon (62)
- # immutant (12)
- # incanter (1)
- # jobs (1)
- # klipse (2)
- # ldnclj (1)
- # luminus (1)
- # mount (1)
- # off-topic (8)
- # om (50)
- # onyx (46)
- # parinfer (5)
- # pedestal (4)
- # perun (2)
- # reagent (1)
- # rum (1)
- # schema (5)
- # specter (30)
- # untangled (5)
- # vim (46)
After reading, it looks like it's a matter of setting a :ring-handler
option. I'm going to proceed under the assumption that I have no idea how to do something so complicated that it's not a "simple" ring server anymore...
Why here a class exception :
(fn [s] (reduce (fn[acc n] (if (not= n (last acc)) (conj n acc))) [(first s)] s ))
You may want to invest into getting an editor setup, Cursive is super simple and can print the last stacktrace very nicely
It has the command "Print last exception", which you should def. bind to something if it's not already
Wierd, the code
(fn [s] (reduce (fn[acc n] (if (not= n (last acc)) (conj n acc))) [(first s)] s ))
=> #object[user$eval1273$fn__1274 0x16b6478 "user$eval1273$fn__1274@16b6478"]
and with pst
I see this error message :
ClassCastException java.lang.Long cannot be cast to clojure.lang.IPersistentCollection
clojure.core/conj--4345 (core.clj:82)
clojure.core/conj--4345 (core.clj:82)
user/eval1525/fn--1526/fn--1527 (form-init7761432892955560103.clj:1)
clojure.lang.PersistentVector.reduce (PersistentVector.java:341)
clojure.core/reduce (core.clj:6544)
clojure.core/reduce (core.clj:6527)
user/eval1525/fn--1526 (form-init7761432892955560103.clj:1)
user/eval1525 (form-init7761432892955560103.clj:1)
user/eval1525 (form-init7761432892955560103.clj:1)
clojure.lang.Compiler.eval (Compiler.java:6927)
clojure.lang.Compiler.eval (Compiler.java:6890)
clojure.core/eval (core.clj:3105)
=> nil
I thought when I did [ (first acc) ] I see a vector but it looks not. conj places the new entries on the front and not in the end
@roelofw If conj
adds to the front, then your collection is a list. If conj
adds to the end, then your collection is a vector.
except for strings then I see this as output : '[\L \e \r \o \y] ` instead of "Leroy"
So I have to think how to make some changes here :
((fn [s] (reduce (fn[acc n]
(if (not= n (last acc)) (conj acc n) acc))
[(first s)] s ))
Your problems is NOT "how to solve this problem for strings", you already solved it. Your problem is "how to turn [\a \b \c] into "abc"" in case the input is a string
@roelofw: he is giving you the tip you asked for
No you don't need reduce twice
(let [res (reduce ...)] (if (string? input) ... ...)
, also cond->>
if you wanna get fancy
Why would you need to do reduce twice?
I was thinking about this :
(if (string? input) (apply str( reduce ..... )) (reduce ......)
@seancorfield I do not want to get fancy. I still have to study how --> works
:thumbsup::skin-tone-2: always learning!
(cond->> (reduce ...) (string? input) (apply str))
is rather nice @rauh
@seancorfield that means that the reduce is executed and the apply str only if the input is a string ?
@seancorfield if I do this :
(fn [s] (cond ->> (reduce (fn[acc n] (if (not= n (last acc)) (conj acc n) acc)) [(first s)] s ) (string? s) (apply str)))
I see this error message : java.lang.RuntimeException: Can't take value of a macro: #'clojure.core/->>, compiling:(NO_SOURCE_PATH:0)
on 4 clojure
Then I see this error : java.lang.RuntimeException: Unable to resolve symbol: cond->> in this context, compiling:(NO_SOURCE_PATH:0)
hopefully some of you fellow clojurians can help me out
i have a map that has an event keyword as key and a fn as a value
in some of these pairs the i get this result :user.handler/confirm-invite #object[clojure.lang.Var$Unbound 0x31e50ab5 Unbound: #'users/confirm-invitation-handler]
other pairs in the same map, coming from the same namespace do not have these issues
I don't think we can answer that without knowing a lot more about how that map is built. Somehow you have an unbound var rather than a function
Hey, how would you advice to write tests with clojure.spec? Do I need to wrap it with some other test framework like test.check or midje? How do you do that, I use spec in repl, but I’d like those verifications to be run from CI as well...
I have to re-implement interleave . Someone who has a tip how I can do this. IM thinking about a loop with a acc
(ns api.users ..) (defn invite-user-handler []
...)
(def event-handlers {:frontend.users.profile.handler/user-profile get-user-handler
:frontend.users.list.handler/process-user-list get-users-handler
:frontend.users.list.handler/invite-user invite-user-handler})
(defn start []
(doseq [[event handler] event-handlers]
(println "event - handler" event handler)
(ws/register event handler)))
@alexmiller all in the same namespace
the println for the last event handler produces
:frontend.users.list.handler/invite-user #object[clojure.lang.Var$Unbound 0x31e50ab5 Unbound: #'api.users/invite-user-handler]
whereas the other two are not unboundsry, wrong copy paste
i get that output for more than one handler
but others are not unbound
same namespace, fn definetly exist
stacktrace:
:cause Attempting to call unbound fn: #'api.users/invite-user-handler
:via
[{:type java.lang.IllegalStateException
:message Attempting to call unbound fn: #'api.users/invite-user-handler
:at [clojure.lang.Var$Unbound throwArity Var.java 43]}]
:trace
[[clojure.lang.Var$Unbound throwArity Var.java 43]
[clojure.lang.AFn invoke AFn.java 32]
[
is there any importance that above you have user.handler
and here you have api.users
?
today i don't seem to get naming right 😉 i just wanted to shorten the namespaces for readability and screwede that up
just assume it's (ns api.users)
i shortended in the snippet, not in actual code 😉
no biggie, my error, i edited in the messages
same thing
api.users=> (invite-user-handler ....)
IllegalStateException Attempting to call unbound fn: #'api.users/invite-user-handler clojure.lang.Var$Unbound.throwArity (Var.java:43)
`another handler from the same namespace
api.users=> (get-users-handler "")
2016-11-23 15:11:46,877 WARN [....]
goes straight to my db and tries to find a blank stringed user 😉ok ok, found the error... wrong parens -.-'
How do I get the first item of s1 and the first item of s2 in the anymous function. Here my code so far : fn [s1 s2] (reduce [acc n ] (conj acc n1 ..) [] s1 s2 )
?
n1 schould be a number of the first seq and n2 schould be the first number of the second seq which I want to interleave
so if we had [a b c] [d e f] as our lists, if we wanted an intermediate step, we could easily end up with ([a d] [b e] [c f])
and if we had that form, is there a way to easily make it look like the final result that we want?
@dpsutton yep flatten
or (mapcat identity ....)
It's the equivalent of flatMap
in other langs
so @roelofw if we know how to get from the intermediate step to the final step, do you know how to get to the intermediate step?
i.e. map
a fn and concat
the results
and its a very common function but used in a way that's not used as much, ie, over two collections rather than over just one
BTW Once you can do it in two steps- with the intermediate result ([a d] [b e] [c f])
it's easy to make it one step using something I mentioned above.
and i think your idea of a reduction makes sense, you just need to figure out how to begin. This should be nicer as it only traverses your collection in one pass
If anyone is interested in picking up Clojure Applied, it’s 40% until Dec 2 the Pragmatic Press’s annual Black Friday sale - https://pragprog.com/book/vmclojeco/clojure-applied - use code turkeysale2016
other Clojure books can be had with the same
or if passed multiple collections, takes an element at a time from each and passes them in to your function
so if we want to make some vectors so that we get ([a d] [b e] [c f])
we are making vectors
f must take the 1 element of c1 and one of c2 and the output must be [ element c1, element c2]
map calls the function you provided with the successive elements of the collection(s)
so here, map is calling vector with (vector 1 2), then (vector 3 4) then (vector 5 6)
-> is the thread-first macro, where the result is put as the first argument of each subsequent expression
I use that often when I have to calculate or count things and im not allowed to use count for example
If I have to flatten a seq and not allowed to use flatten, the only way is to use recursion with a if then ?
You could probably use a loop
form