This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-12-03
Channels
- # adventofcode (6)
- # bangalore-clj (1)
- # beginners (15)
- # boot (4)
- # cider (14)
- # clara (1)
- # cljs-dev (1)
- # clojure (115)
- # clojure-art (1)
- # clojure-france (1)
- # clojure-greece (1)
- # clojure-korea (9)
- # clojure-russia (1)
- # clojure-spec (62)
- # clojure-taiwan (1)
- # clojure-uk (18)
- # clojurescript (5)
- # component (1)
- # cursive (3)
- # datascript (2)
- # datomic (17)
- # devcards (2)
- # editors (4)
- # emacs (65)
- # events (2)
- # funcool (4)
- # hoplon (92)
- # jobs (6)
- # london-clojurians (1)
- # luminus (1)
- # midje (2)
- # mount (1)
- # off-topic (1)
- # onyx (51)
- # protorepl (6)
- # re-frame (116)
- # reagent (7)
- # ring (2)
- # spacemacs (2)
- # specter (4)
- # untangled (1)
- # yada (1)
hey @olslash one more dumb question hehe how do I createa a partial function using the rest arguments
(defn x [& rest] (partial + rest))
suppose I have a seq of maps who looks like this: ( {:name "Roelof" : city Amsterdam} {:name "Jane Doe" :city "unknown"}}
Can I iterate through it with selmer and print it out like this :
Name : Roelof
City : Amsterdam
Name: JAne Doe
City: Unknown
Can I just do {% for data in ( {:name "Roelof" : city Amsterdam} {:name "Jane Doe" :city "unknown"} ) %}
What is wrong here :
{% for data in ( {:name "Roelof" :city "Amsterdam"} {:name "Jane Doe" :city "unknown"} ) %}
{{ data.name }}
{% endfor %}
CompilerException java.lang.RuntimeException: Unable to resolve symbol: % in this context, compiling:(C:\Users\rwobb\AppData\Local\Temp\form-init5709366776722155252.clj:1:715)
RuntimeException Map literal must contain an even number of forms clojure.lang.Util.runtimeException (Util.java:221)
RuntimeException Unmatched delimiter: } clojure.lang.Util.runtimeException (Util.java:221)
RuntimeException Map literal must contain an even number of forms clojure.lang.Util.runtimeException (Util.java:221)
@roelofw Try (render "{% for p in persons %}Name: {{p.name}}\nCity: p.city\n\n{% endfor %}" {:persons [{:name "Roelof" :city "Amsterdam"} {:name "Jane Doe" :city "unknown"}]})
in repl ? @henriklundahl
Yes, or wherever you need it.
Then I see this as output : "Name: Roelof\nCity: p.city\n\nName: Jane Doe\nCity: p.city\n\n"
No, it's kept the same as in the template snippet (a newline). Try printing the string and you'll see. (println *1)
if you are in the repl.
Yes, the second parameter to render
is the data to be used in the template. If you create a html file with Selmer tags you can use render-file
with the path to the file instead.
@roelofw: remember that \n won't give you a new line in HTML in the browser -- you'll need <br> or other suitable markup for that.
Thanks for the pointer, @seancorfield , This was a test how I can make things working in the browser.
What is wrong with this template :
{% for data in ( {:name "Roelof" : city "Amsterdam"} {:name "Jane Doe" :city "unknown"} ) %}
{{ data.name }}
{% endfor %}
You can't put a literal sequence in a for
tag. It must be a variable (which matches a key in the data hash map you pass to render/render-file) @roelofw
(And even if you could, you'd need a vector since (item1 item2)
would be a function call)
oke, so if I make a variable named names = ( {:name "Roelof" : city "Amsterdam"} {:name "Jane Doe" :city "unknown"} )
I have to do {% for data in names }} @seancorfield
What is the difference in performance between these two:
(transduce (filter odd?)
(fn
([] 0)
([acc] acc)
([acc n] (inc acc))) (range 10000))
(count (filter odd? (range 10000)))
and is there a more common way to write the first one?The first one might make sense if you need to count and there are more xforms involved?
@borkdude If performance is important I'd go with a reduce
since the collections know best how to reduce themselves. Though you could also use +
as the reducer and add a (map (constantly 1))
to the transducer
The filter
will generate the entire sequence in memory, so it's wasteful. The transducer won't.
@rauh if you already have a transducer pipeline, does it still make sense to use reduce?
So its not possible to iterate over something like this in Selmer : ( {:name "Roelof"} { :name "Jane Doe"}
@rauh: https://github.com/borkdude/aoc2016/blob/master/src/day3.clj (see the last bit)
@roelofw It is, but you need to either quote the list, so it isn't interpreted as a function call, or use a vector instead: [{:name "Roelof"} { :name "Jane Doe"}]
. Take a look again at the code I posted before. The key :persons
corresponds to persons
in the template snippet.
Yeah, keep
makes a transducer and I'd call that idiomatic. (IMO keep
is underappreciated 🙂 )
But there would be nothing wrong with your first suggestion to created a reducer like that. Probably even better than the keep, the more I think abot it
Kind of surprising that this reducing fn isn’t in Clojure, that’s why I wondered if I did the right thing there
This guys is at Cognitec IIRC: https://github.com/ghadishayban/reducers/blob/94dd9d3f60d31f8c1f79ea26564f4253e761bf90/src/ghadi/reducers.clj#L103
@henriklundahl I cannot convert it to a vector because I use pmap to construct the input
so something like this could work : {% for data in (quote ( {:name "Roelof" : city "Amsterdam"} {:name "Jane Doe" :city "unknown"} )) %}
You can use the output from pmap as input to render.
No, you can't put the data in the template. The template should be separate from the data.
(render template data)
oke, then I will look like this : (defn homepage [req] (render-file "home.html" ( do-both-in-parallel( read-keys))))
The tranducers version is about 30% faster than the non-transducer version (with ->>
instead of comp
)
@henriklundahl or do I need something like this :
(defn homepage [req] (render-file "home.html" { :paintings ( do-both-in-parallel( read-keys))))`
Yes @roelofw that's how.
I'm trying to understand an obstacle I'm running into in my current, boot-based workflow
So the REPL session initializes to the boot.user namespace. This is true of all boot-based repl configurations I've come across
That namespace appears to consist of build profiles ("dev", "production"). I'm looking for the application logic itself, so I hop over to my-namespace.core....
In the core namespace any attempts to load the file immediately fail because clojure hasn't been loaded. `(use 'clojure.core) loads Clojure, but not the file associated with the namespace, and that's about as far as I've been able to get
At a minimum, I'd like to be able to load my namespaces and definitions into the REPL. Ideally, I'd also like to have shared browser state between the repl and the application (a la figwheel), but I have a hunch that may not be possible.
I do this in my view file :
(defn homepage [req] (render-file "home.html" { :paintings ( api/do-both-in-parallel(api/read-numbers))}))
and my template looks like this :
{% for data in paintings) %}
{{ data.name }}
{% endfor %}
can you pass in as your data [{:name "bob"} {:name "sally"}]
to see if your template is working correctly?
just replace your call to your api stuff with concrete data so there are fewer moving parts
@dpsutton when doing (defn homepage [req] (render-file "home.html" { :paintings [{:name "bob"} {:name "sally"}] }))
still a empty page
is there a way of writing large numbers in a readable format in clojure without using strings? e.g. like java’s 2_000_000_000L
syntax
oke, first get some sleep. A few minutes before you @seancorfield said this is the right way to make it work
Does the loco
Constraint Programming library support constraint precedence, where if the given model isn't solvable, it drops constraints off the bottom until it is?
@torgeir You could make a pretty-num
macro that replaces (pretty-num 2,000,000,000)
with 2000000000
No, you couldn't
The reader is not going to read that
@alexmiller Maybe (p-n "2,000,000,000)
?
Sure, you could use strs and a function/macro
Commas are treated as white space and ignored
I guess maybe the comma version would work if that tokenized ok (would still need a fn or macro to reassemble)
@alexmiller: I said that it would be a macro. Hell, it could work pretty simply: string-concatenate the numbers, then string-to-int