Fork me on GitHub
#clojure
<
2016-12-03
>
tdantas00:12:01

hey @olslash one more dumb question hehe how do I createa a partial function using the rest arguments

(defn x [& rest] (partial + rest))

olslash00:12:19

maybe this

olslash00:12:23

user=> (def a (apply partial + [1 2 3]))
#'user/a
user=> (a 2)
8

tdantas00:12:17

cool trick

olslash00:12:20

i havent used it before, i think it works?

tdantas00:12:23

but suppose that instead of + i’m using (defn sum [a b] (+ a b))

tdantas00:12:32

with + works

tdantas00:12:12

oh , works mate

tdantas00:12:17

just tested

roelofw08:12:54

suppose I have a seq of maps who looks like this: ( {:name "Roelof" : city Amsterdam} {:name "Jane Doe" :city "unknown"}}

roelofw08:12:26

Can I iterate through it with selmer and print it out like this :

Name :  Roelof
City : Amsterdam 

Name: JAne Doe 
City: Unknown 
 

roelofw08:12:46

Can I just do {% for data in ( {:name "Roelof"  : city Amsterdam} {:name "Jane Doe" :city "unknown"} ) %}

roelofw08:12:48

and then name: {{ data.name }}

roelofw09:12:29

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)
 

roelofw09:12:36

I do this in repl

roelofw09:12:42

I have done (use 'selmer.parser) before it

henriklundahl11:12:37

@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"}]})

henriklundahl12:12:05

Yes, or wherever you need it.

roelofw12:12:51

Then I see this as output : "Name: Roelof\nCity: p.city\n\nName: Jane Doe\nCity: p.city\n\n"

roelofw12:12:13

so the '/n' is not executed

henriklundahl12:12:48

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.

roelofw12:12:20

Thanks, I hope I can use this idea for a page in a website

roelofw12:12:33

where the {} is a parameter

henriklundahl12:12:39

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.

seancorfield15:12:31

@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.

roelofw16:12:52

Thanks for the pointer, @seancorfield , This was a test how I can make things working in the browser.

roelofw16:12:00

I try to do one step at the time

roelofw16:12:01

What is wrong with this template :

{% for data  in   ( {:name "Roelof"  : city "Amsterdam"} {:name "Jane Doe" :city "unknown"} )  %}
   {{ data.name }}
{% endfor %}
 

roelofw16:12:27

when I use it. I do not see any output in my browser

roelofw16:12:52

This is my first step to make a template to show something

roelofw17:12:26

I use this to call the template : (defn homepage [req] (render-file "home.html" {}))

seancorfield18:12:42

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

seancorfield18:12:07

(And even if you could, you'd need a vector since (item1 item2) would be a function call)

roelofw18:12:53

oke, so if I make a variable named names = ( {:name "Roelof" : city "Amsterdam"} {:name "Jane Doe" :city "unknown"} )

roelofw18:12:03

I have to do {% for data in names }} @seancorfield

roelofw18:12:16

and then I can do data.name

roelofw18:12:22

O , I cannot This is not a vector. I think I have used the wrong data-type here 😞

roelofw18:12:32

and I cannot convert this to a vector with (vector .... )

roelofw18:12:59

I think I can better practice more with clojure and put this project on a hold

borkdude18:12:13

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?

borkdude18:12:34

The first one might make sense if you need to count and there are more xforms involved?

rauh18:12:19

@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

rauh18:12:33

The filter will generate the entire sequence in memory, so it's wasteful. The transducer won't.

borkdude18:12:58

@rauh if you already have a transducer pipeline, does it still make sense to use reduce?

roelofw18:12:54

So its not possible to iterate over something like this in Selmer : ( {:name "Roelof"} { :name "Jane Doe"}

rauh18:12:54

@borkdude No, then I'd use the reducer you created

rauh19:12:12

@borkdude You can always combine filter+map into (keep #(when (valid-triangle? %) 1)

henriklundahl19:12:19

@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.

borkdude19:12:24

@rauh doesn’t the implementation of transducers provide that kind of fusing?

rauh19:12:12

Yeah, keep makes a transducer and I'd call that idiomatic. (IMO keep is underappreciated 🙂 )

borkdude19:12:57

@rauh I didn’t have keep, because I didn’t generate 1’s before 😉

rauh19:12:04

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

borkdude19:12:28

Kind of surprising that this reducing fn isn’t in Clojure, that’s why I wondered if I did the right thing there

roelofw19:12:52

@henriklundahl I cannot convert it to a vector because I use pmap to construct the input

roelofw19:12:01

so I need to use quote here

roelofw19:12:03

so something like this could work : {% for data in (quote ( {:name "Roelof" : city "Amsterdam"} {:name "Jane Doe" :city "unknown"} )) %}

henriklundahl19:12:41

You can use the output from pmap as input to render.

henriklundahl19:12:26

No, you can't put the data in the template. The template should be separate from the data.

henriklundahl19:12:43

(render template data)

roelofw19:12:06

oke, then I will look like this : (defn homepage [req]  (render-file  "home.html" ( do-both-in-parallel( read-keys))))

roelofw19:12:08

but what do I then put into the '(% for in .... ) '

roelofw19:12:40

I find this very confusing

borkdude19:12:38

The tranducers version is about 30% faster than the non-transducer version (with ->> instead of comp)

roelofw19:12:34

someone who can help me see how things work in selmer

ghadi20:12:48

@rauh (I am not at Cognitect -- just a friend of theirs.)

roelofw20:12:27

@henriklundahl or do I need something like this : (defn homepage [req] (render-file "home.html" { :paintings ( do-both-in-parallel( read-keys))))`

roelofw21:12:50

and then I can do

roelofw21:12:12

for data in paintings

kzeidler21:12:16

I'm trying to understand an obstacle I'm running into in my current, boot-based workflow

kzeidler21:12:23

So the REPL session initializes to the boot.user namespace. This is true of all boot-based repl configurations I've come across

kzeidler21:12:37

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....

kzeidler21:12:00

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

kzeidler21:12:00

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.

kzeidler21:12:31

(There's a closed ticket about that on the Figwheel repo)

roelofw21:12:58

I do this in my view file :

(defn homepage [req] (render-file "home.html" { :paintings ( api/do-both-in-parallel(api/read-numbers))})) 

roelofw21:12:38

and my template looks like this :

{% for data  in  paintings)  %}
   {{ data.name }}
{% endfor %} 

roelofw21:12:19

but still a empty page where I expect to see 10 titles

roelofw21:12:39

What is wrong now ?

dpsutton21:12:53

can you pass in as your data [{:name "bob"} {:name "sally"}] to see if your template is working correctly?

roelofw21:12:42

@dpsutton oke, so I have to change the api part by your code ?

dpsutton21:12:05

just replace your call to your api stuff with concrete data so there are fewer moving parts

dpsutton21:12:14

once you know your template is working then switch back to the api clals

dpsutton21:12:22

have as few moving parts while figuring this all out

roelofw21:12:45

@dpsutton when doing (defn homepage [req] (render-file "home.html" { :paintings [{:name "bob"} {:name "sally"}] })) still a empty page

dpsutton21:12:59

well now you know the issue is in your template

dpsutton21:12:07

so find some documentation and start playing around

torgeir21:12:21

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

roelofw21:12:43

oke, first get some sleep. A few minutes before you @seancorfield said this is the right way to make it work

roelofw21:12:06

This is very frustating

seylerius22:12:07

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?

seylerius22:12:53

@torgeir You could make a pretty-num macro that replaces (pretty-num 2,000,000,000) with 2000000000

seylerius22:12:12

Or something loosely to that effect.

Alex Miller (Clojure team)22:12:52

The reader is not going to read that

torgeir22:12:57

2e9, I guess, but for not that clean numbers..

torgeir22:12:39

I had a go with 2_000_000 already, seems it would take reader macros, no?

seylerius22:12:10

@alexmiller Maybe (p-n "2,000,000,000)?

seylerius22:12:27

@torgeir Clojure doesn't support adding reader macros.

seylerius22:12:56

Hmm... Are commas dropped, or treated as whitespace?

torgeir22:12:02

yea, I know, thats why it wasnt working 😉

Alex Miller (Clojure team)22:12:48

Sure, you could use strs and a function/macro

Alex Miller (Clojure team)22:12:10

Commas are treated as white space and ignored

Alex Miller (Clojure team)22:12:50

I guess maybe the comma version would work if that tokenized ok (would still need a fn or macro to reassemble)

seylerius22:12:51

@alexmiller: I said that it would be a macro. Hell, it could work pretty simply: string-concatenate the numbers, then string-to-int

seylerius23:12:28

Need some name ideas for a general roster-builder application. It uses constraint programming, is being requested by a team that works in a hospital, and can sync with google calendar for availability.