This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-03-14
Channels
- # arachne (5)
- # architecture (2)
- # bangalore-clj (7)
- # beginners (96)
- # boot (34)
- # braveandtrue (1)
- # cider (12)
- # cljs-dev (38)
- # cljsrn (59)
- # clojure (326)
- # clojure-dev (35)
- # clojure-greece (1)
- # clojure-italy (6)
- # clojure-russia (47)
- # clojure-spec (16)
- # clojure-uk (25)
- # clojurescript (136)
- # core-async (18)
- # cursive (18)
- # datascript (2)
- # datomic (28)
- # dirac (6)
- # emacs (4)
- # garden (3)
- # hoplon (28)
- # instaparse (1)
- # jobs (4)
- # juxt (1)
- # lein-figwheel (10)
- # liberator (1)
- # mount (3)
- # off-topic (39)
- # om (16)
- # om-next (1)
- # onyx (15)
- # pedestal (9)
- # proton (1)
- # random (1)
- # re-frame (48)
- # reagent (8)
- # ring-swagger (4)
- # rum (3)
- # specter (5)
- # sql (3)
- # unrepl (273)
- # untangled (27)
- # vim (4)
- # yada (7)
Hello! What’s the idiomatic way to append a key-value pair to a sequence? Say I have:
(defn defaults '(:a 1 :b 2))
and I want to define some function such that:
(my-function defaults :c 3) => '(:a 1 :b 2 :c 3)
?I tried conj but it (of course) appends in reverse order:
(conj defaults :c 3) => (3 :c :a 1 :b 2)
What I really want to do is be able to insert an extra argument into a variadic function’s remaining argument sequence prior to invoking another function with the augmented sequence.
@gamecubate concat will do it
@alexmiller Thanks! I didn't think of looking for a browser version of Quil, so good that you suggested it. I think I will offer them Quil and Klangmeister for ultimate chaos.
@gamecubate Don't you want to use vectors?
@tbaldridge concat will do it as long as I wrap the k-v pair in a seq. I guess that’ll do. Thanks.
@yonatanel Probably not. I am creating a constructor function (for Threejs) that takes all the arguments I throw at it (
(defn make-scene [& options] …)
and in turn calls separate helper functions (camera …), (renderer …) with same arguments, augmented with a few extra ones the constructor will graft on.Hence my question about appending extra args to a variadic function’s remaining arguments.
(defn foo [ & {:keys [a b c] :or {a 0 b 0 c 0}}] [a b c])
(defn bar [ a & options] (foo :a a options :c (* a 3)))
(bar 99 :b 13)
As written above, bar fails to forward the args to foo. So needs more work. But that’s the gist of how I would like to invoke bar.
Just need to figure out proper way to define bar such that calling
(bar 1 :b 2)
has same results as calling (foo :a 1 :b 2 :c 3)
.One solution, but seems… heavy:
(defn foo [ & {:keys [a b c] :or {a 0 b 0 c 0}}]
[a b c])
(defn bar [ a & options]
(let [c (* a 3)]
(apply foo (concat [:a a :c c] options))))
(.log js/console (bar 1 :b 2)) ; => [1 2 3]; it works.
More concretely:
(defn camera [ & {:keys [fov near far aspect] :or {fov 50, near 1, far 1000, aspect 1.0}}]
[fov near far aspect])
(defn scene [ w h & options]
(apply camera (concat [:aspect (/ w h)] options))))
(.log js/console (scene 320 240 :fov 80))
So concat was the answer. Thanks for the hint, @tbaldridge .
And I did wrap my extra arg into a vector prior to calling concat, so thanks as well, @yonatanel 🙂
i’m attempting to set up a very basic clojure app with boot but it’s not pulling in my namespace. https://gist.github.com/anonymous/0763262eebeb829611287ce4e58a1410
@decim You have io.pedestal.route
in your :require
but it should be io.pedestal.http.route
instead. See this example https://github.com/pedestal/pedestal/blob/master/samples/hello-world/src/hello_world/service.clj BTW, there's also a #pedestal channel if you have deeper questions about Pedestal.
I am using cljs with react native. I would not like to navigate to another screen on-press. Can anyone please share the peace of example code to do ?
Hello, here is a quick function about escape special characters from input boxes? Like (clojure.string/escape “string?” {\? “”})
, just I don’t want plus I can’t add all special characters possibilities to this function. Similar function to all spec. characters?
@sb “Input box”?
what is the context here? HTML? “special characters” differ from ui technology to ui technology
Somebody want to add in the HTML input boxes… something like this.. ?..,.!!’”+”’”+dsds+’”+’ .. and I want to filter it.. or check it.
@ordnungswidrig the question is.. here is a quick solution for this, like clojure repository or example how to create faster than I think..
@sb In cljs:
ephyra.core=> (require '[goog.string :as gstring])
nil
ephyra.core=> (gstring/htmlEscape "<script>alert()</script>")
"<script>alert()</script>"
@sb do you want for prevent some characters or simply escape them?
But don't rely on client-side escaping. It can be bypassed easily. Escape it on the server side or make sure templating technologies you use escape variables for you.
escaping must happen on the client. The server should support the proper processing of virtually the full unicode set.
There are nasty unicode characters, like invisible spaces. though.
still the example from @not-raspberry is a good start.
@curlyfry in the output of html, yes. I think here it’s about the return of the values to the server, say by HTTP POST
@ordnungswidrig @sb Wasn't it an input box? "Somebody want to add in the HTML input boxes"
sombody enters a “dangerous value” into an input box. You want to ensure the client and server process all the characters correctly and the server (and client) does some validation.
the escaping must be done in the request from the client to the server. on the server side you would unencode this (ring form params middleware will do that for you) and then validate and reject invalid values.
@not-raspberry thank you very.. I see now, you post a solution for this (with clojurescript)
Ok, I see now two option with ClojureScript and with clojure.string/escape
(little bit more work). Thanks for your help again!!
I'm learning clojure from reading the Brave and True online book, and like expected I'm struggling in some parts and doing well in others. Since I'm not a programmer I can't see where I can put what I'm learning in practice in the real world (if that makes any sense). So I was wandering if is there a "place" where I can learn clojure while building something?
Though not clojure specific, you could try implementing a project from one of the lists here https://www.reddit.com/r/learnprogramming/comments/2a9ygh/1000_beginner_programming_projects_xpost/ , or if you were looking for something smaller to do, there is https://www.4clojure.com/ and https://projecteuler.net/
@biofobico SafariBooks online > 14 days free trial > clojure videos/ books
@grierson I guess you wan (map (fn [shape] (shape)) shapes)
since shapes are 0-arity functions
@grierson sean is right, but the code you want in ? is another function (fn [f] (f))
which can be shortened to (map #(%) shapes)
or #(%)
if you want to be really confusing
lol, @val_waeselynck had the same idea
@tbaldridge and the same reaction about it 🙂
@grierson but in general we prefer to be a bit more data-driven in Clojure, so we would normally write this the following way: 1) turn your draw functions into a multimethod or protocol 2) turn your list of shapes into data: {:type :circle} {:type :square} 3) Since the draw function is side effecting use doseq instead of map:
(doseq [shape shapes]
(draw shape))
The idea of a strategy it's as needed in a language where functions are first-class.
Thanks, I will have to read into multimethods and protocols but thanks for pointing me in the right direction.
I've noticed a lot of code now uses a double colon and I'm just wondering what it is?
::confused
It's an autoresolved keyword
It will resolve to a fully qualified keyword
With no namespace it will use the current namespace
With a namespace like ::foo/confused it will resolve using the namespace aliases
https://www.deepbluelambda.org/programming/clojure/know-your-keywords
^^ pretty good
bookmarked that one thanks!
I'm looking at some code someone wrote for me the other day and they use the double colon to reference an atom. I would normally use the @.
(let [playback-rate (atom 1.0)]
(add-watch playback-rate ::playback-rate)
......
that’s not the same as @ at all - in fact that code is very strange
that requests that you apply that keyword as a function to the new number if playback-rate changes, which will return nil and do nothing useful
Oh yea I just realised I'm not dereferencing the atom. I'm getting it to watch the actual atom
right, but even so, the watch is not doing anything that makes sense
that should break, because the function arg to add-watch should take 4 args, and playback-rate takes max 2…
anyway, this probably ins’t important, just saying that code makes very little sense
oh! - it needs a key, and the function arg is missing
I remembered that the function was the last arg, forgot the key arg- never mind
I see the key is to identify the individual watcher and the double colon is to use the current namespace.
right, it just needs a function arg after the keyword
I misread
all good haha. I wasn't copy pasting so I couldn't be bothered typing it all 😛