This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-10-01
Channels
- # beginners (2)
- # boot (1)
- # cljs-dev (126)
- # cljsrn (4)
- # clojars (1)
- # clojure (109)
- # clojure-austin (5)
- # clojure-austria (1)
- # clojure-boston (2)
- # clojure-brasil (33)
- # clojure-canada (1)
- # clojure-russia (1)
- # clojure-spec (62)
- # clojurescript (23)
- # core-async (1)
- # cursive (33)
- # datomic (26)
- # ethereum (2)
- # hoplon (7)
- # jobs (1)
- # lein-figwheel (1)
- # luminus (12)
- # off-topic (4)
- # om (3)
- # om-next (49)
- # perun (2)
- # protorepl (2)
- # specter (6)
- # test-check (1)
- # untangled (2)
- # vim (6)
- # yada (30)
could you show how you want to use the thing?
So the reason this doesn't work with an if is because an if must evaluate to a single form. Basically I want something like this:
(if true-or-false
(wrapping-func arg1 arg2)
arg1
arg2)
But that obviously doesn't work.I'm more curious than anything else, I have already resolved this a different way in my own code.
Can I define a ns alias just so that ::x/y will work even though that ns doesn’t really exist so I can’t simply require it?
@bcbradley Honestly I think the best way to go is for you to go ahead and just build a very basic app and stack overflow/wikidepdia/etc your way through it
You’ll probs want to start by learning some basic html/css/javascript and learn to make basic pages with those before you dive into the actual http protocol world
@lvh - :foo/bar works regardless of whether or not foo
is a real namespace / whether or not you’ve required it, if that’s helpful information
Trying my luck in here. I understand that fdef-ed functions (clojure.spec) should include the spec in the docstring of the function, but I don't seem to be seeing that. Is there anything special I need to do?
Nevermind, it seems like it is a cider issue. At the repl, (doc my.namespace/fn) works.
@lvh not yet, although you can use create-ns to do that part separately
@shdzzl you're saying that can work if wrapping-func is a macro?
@lvh schpec has a function for that
@gfredericks No, in truth it's just unquote splicing that makes it work. But I just associate that with macros I guess. My bad.
@gfredericks No, in truth it's just unquote splicing that makes it work. But I just associate that with macros I guess. My bad.
Rephrasing my original question: Without unquote splicing, is there a way to replace one form with two or more?
@shdzzl no, and that can only work when the surrounding context is a macro. So just changing wrapping-func to a macro doesn't even help.
I didn't mean to suggest changing wrapping-func to a macro was a solution. The surrounding context needs to be quoted, yeah. I don't actually need to create a macro to do that though. I can just do something like this:
(def x [arg1 arg2])
(defn decide-for-me [pred inner]
(if pred
[(apply wrapping-func inner)]
inner))
`(~@(decide-for-me some-bool x))
if
produces a single expression by definition so I can’t even imagine how you’d want to use the result…?
I think i confused the issue with my original example, I don't want to actually use if
to return two forms, I wanted a good way to conditionally return either one form or two or more forms.
Based on your code above why not just do:
(cond->> [arg1 arg2]
some-bool (apply wrapping-func)
true vec)
Or even (vec (cond->> [arg1 arg2] some-bool (apply wrapping-func)))
Hmm, yeah, it’s not quite correct as written…
(cond-> [arg1 arg2]
some-bool (->> (apply wrapping-func) vector))
I think that more accurately matches your code.@seancorfield That still returns just a single vector doesn't it? Or am I missing something?
Your decide-for-me
function either returns [(apply wrapping-func inner)]
or inner
— my code returns the exact same thing...
My earlier attempt tried to turn the result into a vector — which isn’t the same.
But my initial question was: what problem are you actually trying to solve?
What situation makes you want two expressions in a row (if it isn’t just getting a vector of expressions)?
I have already solved my original problem, I just refactored to avoid the situation. But out of curiosity, I was asking about the possibility of replacing a single form with two or more. It appears the answer is unquote splicing or nothing.
Is this for an argument list to another function? Why not use apply
in the first place?
(apply some-func (cond-> [arg1 arg2] some-bool (->> (apply wrapping-func) vector)))
or if it’s in the middle of the arg list use (into (cond-> …) [the other arguments])
in there.
(I’m coming at this from the position that macros are usually the wrong answer — there’s a right answer with functions first and then maybe some syntactic sugar with a macro later)
(defn lispify [html]
(w/postwalk (fn [e] (if (vector? e) (apply list e) e)) (t/parse html)))
t/parse returns things that look like [:html {} [:head {} [:title {} "Example Domain"]]]
if i do
(w/postwalk (fn [e] (if (vector? e) (apply list e) e)) [:html {} [:head {} [:title {} "Example Domain"]]])
But if i try
(lispify "")
i get an error: ClassCastException clojure.lang.Keyword cannot be cast to java.util.Map$Entry clojure.lang.ATransientMap.conj (ATransientMap.java:44)
yet
(t/parse "")
gives me something that looks like [:html {} [:head {} [:title {} "Example Domain"]]]
Something like (defn lispify [html] (w/postwalk ... html))
, and then call it like (lispify (t/parse url))
At first glance I don't know what might be wrong, but I generally try to split up stuff and simplify when I'm debugging
@seancorfield The original context was a wrapping a
in Om Next (ClojureScript) that I wanted to apply conditionally. So original code was:
(dom/a #js {:href (:link contact)}
(dom/span #js {:className (str "typcn " (:icon-class contact))})
(dom/p nil (:value contact)))
And I wanted to conditionally replace it with just the inner two elements, or:
(dom/span #js {:className (str "typcn " (:icon-class contact))})
(dom/p nil (:value contact)))
It does present some other readability problems (because this is basically proxy html), but they're Om Next problems, not Clojure problems.
Curious... why does every?
have a question mark and some
does not? Not a big deal, but I figure there is probably a reason.
?
signals that every?
is a predicate; some
isn't a predicate as it returns a value instead of true
Ah, for some reason I didn't realize that some?
existed and some
was a special case, since so much Clojure code uses some
when it only cares about the coerced boolean value.
Oh strange... some?
and some
have totally different uses, though. Well, it makes enough sense for now.
why does clojure.walk do this?
(clojure.postwalk-demo [:html {:a "b"} ""])
yields Walked: :html; Walked :a; Walked "b"; Walked [:a "b"]; Walked {:a "b"}; Walked ""; Walked [:html {:a "b"} ""}
Notice the: Walked: [:a "b"]
and yet there is no such vector in the inputI am trying to use clojure.walk to recursively change all vectors in a nested structure into lists
But this strange behavior is causing me to receive ClassCastException clojure.lang.Keyword cannot be cast to java.util.Map$Entry clojure.lang.ATransientMap.conj (ATransientMap.java:44)
@bcbradley map entries are vectors
So what you are saying is that there is no real way to differentiate between a vector and a map for the purposes of walking through it?
a map and a vector, yes, a map entry and a vector, not generally; there might be some way of doing what you want though
it might be better to back up and ask why you want to do that though
long story short I am going to interpret the vector as a list so that I can execute it
your data really is recursive?
oh, that's pretty restricted then
does it always have a map as the second element?
and those are the only maps, so you know where they are, and you can write a recursive function to do it
it does feel like clojure.walk is lacking a little bit here though
i had spent hours on this because I didn't think to consider that walk operated that way with maps
It doesn't generally matter, unless you want to be rigorous about testing with multiple versions, in which case you'll want a fancy profile setup