Fork me on GitHub
#beginners
<
2017-02-03
>
rauh06:02:44

``````(let [prevnext 6
num-pages 466
page 466
lower (max 0 (- page prevnext))
upper (min num-pages (+ page prevnext))]
(range lower upper))
``````

roelof07:02:22

@rauh thanks. Can you explain a little wat prev-next , num-pages and page are ?

roelof07:02:44

I think num-pages is the total of pages and page is the current page, right ?

rauh07:02:27

prev-next is a bad name tbh. It's the number of previous and next pages that you want to show

roelof07:02:10

so if im at page 4 . then prev-next is 6 . num-pages is 466. page = 4 , lower (max 0 (- 4 6) lower (max 0 -2) Lower = 0

roelof07:02:56

uppper min (466 (+ 4 6) ) = min(466 10) = 10

roelof07:02:08

range ( 0 10) , that is right

roelof07:02:53

@rauh : it;s not complete working as expected. When page = 10 then lower = 0 , upper = 16 so I see a range of 0 till 16 where I expect to see a range of 6 - 14

rauh07:02:04

I don't get that result @roelof

rauh07:02:46

actually there is a tiny bug, ti shoudl be `upper (inc (min num-pages (+ page prevnext)))`

rauh07:02:56

to be symmetric

roelof07:02:55

sorry, in my head i was busy with 10 numbers, sorry

roelof07:02:05

but still I see something wierd

roelof07:02:24

``````(let [prevnext 6

num-pages 450

page 466

lower (max 0 (- page prevnext))

upper (min num-pages (+ page prevnext))]

(range lower upper))
``````

roelof07:02:33

gives in my repl : ()

roelof07:02:13

and when I do :

``````(let [prevnext 10

num-pages 460

page 466

lower (max 0 (- page prevnext))

upper (inc (min num-pages (+ page prevnext)))]

(range lower upper))
``````

roelof07:02:38

I see as output :

``(456 457 458 459 460)  ``

roelof07:02:55

and that are no 10 numbers 😞 @rauh

roelof07:02:20

and when I do :

``````(let [prevnext 10

num-pages 466

page 460

lower (max 0 (- page prevnext))

upper (inc (min num-pages (+ page prevnext)))]

(range lower upper))
``````

roelof07:02:45

I see as output :

``(450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466)  ``

roelof07:02:46

I think lower must be in a if-then statement

roelof07:02:59

I can play with it

roelof07:02:52

Do you know how I can use this in selmer. Can I do something like

``(for [x [(range lower upper)]  ``

roelof08:02:05

Can I do something like this :

``````(defn home-page [page]
(let [page-num (s/conform ::page page)
url ""
options {:as :json :query-params {:key (env :key) :format "json" :type "schilderij" :toppieces "True" :p page-num :ps 10}}]
(if (s/invalid? page-num) 1 page-num)
(layout/render
"home.html" {:paintings (-> (client/get url options)
(api/get-objectNumbers)
(api/fetch-paintings-and-images-front-page))},
{:current-page: page-num})))
``````

roelof08:02:28

to take care that I can use the current page in my selmer template ?

roelof08:02:24

nope, I cannot, :current-page is a invalid-token 😞

roelof08:02:54

someone a tip how to solve this ?

sveri08:02:42

roelof09:02:01

@sveri what do you mean exactly

roelof09:02:36

I tried several things but they all fail with the same error message

sveri09:02:07

Just as I say it, there is a syntax error in the code you pasted. Not sure if that is the reason for your error, but in general it can not work like that.

roelof09:02:08

@sveri : I think you mean something like this :

``````(defn home-page [page]
(let [page-num (s/conform ::page page)
url ""
options {:as :json :query-params {:key (env :key) :format "json" :type "schilderij" :toppieces "True" :p page-num :ps 10}}]
(if (s/invalid? page-num) 1 page-num)
(layout/render
"home.html" {{:paintings (-> (client/get url options)
(api/get-objectNumbers)
(api/fetch-paintings-and-images-front-page))},
{:current-page: pagenum}})))
``````

roelof09:02:22

but then I see the same error 😞

roelof09:02:01

Invalid token: :current-page:

roelof09:02:20

and I cannot see what I do wrong, that is why I asked here

sveri09:02:20

@roelof syntax is the way you write your code. From my point of view you need more practice. And if I tell you the syntax error you wont learn as much as if you find it yourself, so go ahead and search for it. Every seasoned programmer had to go through this several times in his life

sveri09:02:49

Usually this means looking at a few lines of code for several hours and banging your head at the wall when you see it.

roelof09:02:14

I think I found it

roelof09:02:32

there was a : behind the current-page 🙂

sveri09:02:55

🙂 nice

roelof10:02:38

I do this in repl :

``test = {:data {:painting "painting" :current-page 2}} ``
`

roelof10:02:47

now when I do

``(:data test) ``
I see nill where I expected to see this part
``test = {:data {:painting "painting" :current-page 2}} ``

roelof10:02:09

can someone explain this to me so I see where I did go wrong ?

sveri10:02:19

test = ... How is that valid clojure?

roelof10:02:00

oke, I mixing up things

joshkh12:02:57

could someone help me out with clojure(script) and regex? i'm trying to un-camelcase a word but my output is the same as my input:

``````(defn uncamel [s]
(let [pattern (re-pattern "/([A-Z])/g")]
(clojure.string/replace s pattern "\$ 1")))
``````
``````(uncamel "unCamelCase")
=> unCamelCase
``````

poooogles12:02:49

what's the expected output?

joshkh12:02:40

"Un Camel Case" (or at least in that simpler regex, "un Camel Case")

poooogles12:02:13

try splitting on a regex match for [A-Z] and then join the result with spaces?

poooogles12:02:03

or tbh I think your regex is wrong

joshkh12:02:55

but in principle i'm using the right functions?

poooogles12:02:08

``````user=> (str/join " " (str/split "unCamelCase" #"(?=[A-Z][^A-Z])"))
(str/join " " (str/split "unCamelCase" #"(?=[A-Z][^A-Z])"))
"un Camel Case"``````

poooogles12:02:17

would be what I'd do, but there's lots of ways to skin a cat.

joshkh12:02:48

hey thanks, that's really helpful 🙂

roelof13:02:22

I have this :

``{:data {:painting {:name "painting 1"},{:name "painting2"}}}  ``

roelof13:02:21

now I want to display all the name in Selmer. So I do :

``````{% for painting in (:painting(:data data)) %}

{{ painting.name}}

{% endfor %}
``````

roelof13:02:33

but nothing is shown.

roelof13:02:50

Where did I go the wrong lane ?

henriklundahl13:02:12

roelof13:02:09

@henriklundahl yep, but I cannot see where it is invalid

henriklundahl13:02:30

`{:painting {:name "painting 1"},{:name "painting2"}}` is invalid.

roelof13:02:31

is there a paste that I can use to see the real output @henriklundahl ?

sveri13:02:11

sveri13:02:16

Also try to load the code you pasted, in a repl

roelof13:02:37

Can I test selmer templates in repl ? sveri I did not know that

dpsutton13:02:17

can't you just render them?

dpsutton13:02:26

you're just making strings from my understanding

dpsutton13:02:40

your handler function just calls `(render template data)` so do that from a repl

roelof14:02:20

yep, Im now at a template which render needs to render @dpsutton

dpsutton14:02:39

i don't understand

dpsutton14:02:50

then just call render?

sveri14:02:54

You can paste this: {:data {:painting {:name "painting 1"},{:name "painting2"}}} into a repl

sveri14:02:58

and try to work with it

roelof14:02:17

Im just try to find out how in selmer I only want to see the painting part

sveri14:02:28

but you need to fix your data structures first

sveri14:02:38

otherwise nothing will work, neither selmer, nor any function

roelof14:02:01

I can paste a real output , that one was a simpler version

sveri14:02:41

that simple version was an invalid data structure

roelof14:02:25

yep, here you can find the real output when I do {{ data }} in selmer : http://lpaste.net/591130986197024768

sveri14:02:45

Now you can test it with the render function: https://github.com/yogthos/Selmer#templates It returns a string IIRC

roelof14:02:33

This is the output of the render function

henriklundahl14:02:59

You'll need to fix the template as well. You can't use `(:painting(:data data))` in `{% for painting in (:painting(:data data)) %}`.

roelof14:02:13

normally I would do

``(:paintings data)  ``

roelof14:02:37

and I would get the painting part but in Selmer that would not work

sveri14:02:49

You need to read up on selmer and how it works

sveri14:02:55

and then test drive it with some simple examples

sveri14:02:21

you are trying to push a mountain instead of shoveling it step by step

roelof14:02:00

found it. I can do data.paintings to get the paintings part

dpsutton14:02:07

it looks like selmer navigates maps differently than clojure syntax

roelof14:02:15

solved. I can do this :

``````{% for painting in data.paintings %}

{{painting.name}}

{% endfor %}
``````

roelof14:02:30

to display the data

roelof16:02:21

Is it possible to make changes to the dimensions of a image in clojure or must I use something like jquery for that

roelof16:02:20

I have a image of 1200 800 and I want it to have it 500 333

dpsutton16:02:38

java can do it. i've done it in the past

dpsutton16:02:54

do you want to just display it at a different size though?

dpsutton16:02:03

this is for your website, correct?

roelof17:02:21

yes, I need the images at a certain size.

roelof17:02:45

The example I have found is using 500 x 500

roelof17:02:25

So at some way I have to make squares out of rectangles

roelof17:02:46

and then the chapter : Example: loading all tiles in a zoomlevel

roelof17:02:15

@dpsutton but I wonder if this can be done with clojure or clojurescript

dpsutton17:02:19

you need to display the images at a certain size or you need the source image at a certain size?

dpsutton17:02:30

i would only adjust the size you are displaying which is easily done in html

dpsutton17:02:53

i'm guessing these aren't your images so modifying them seems not what you want to do

dpsutton17:02:58

and would be cpu intensive on your side

dpsutton17:02:27

but as far as i can tell you need this functionality: `<img src="smiley.gif" alt="Smiley face" height="42" width="42">`

roelof17:02:44

Oke, I will try that way

roelof17:02:14

but first I need to compose the images. The images are in several parts

dpsutton17:02:36

> So at some way I have to make squares out of rectangles if you need an aspect ratio change you have a significantly different problem

roelof17:02:38

maybe I use the example of the api page directly. It looks jquery to me

roelof19:02:17

How can I rewrite this : `(fn[x] (+ x 20 ) ` into this `#(+ % 20)` but that one gives error messages

roelof19:02:29

what is the right syntax for this ?

joost-diepenmaat19:02:04

the #(..) one is correct, the (fn ..) version is missing a closing parenthesis

joost-diepenmaat19:02:41

`user> (#(+ % 20) 5) => 25`

joost-diepenmaat19:02:40

`user> ((fn[x] (+ x 20 )) 5) => 25`

joost-diepenmaat19:02:17

other than that, as you can see, they’re equivalent

roelof19:02:27

wierd . when Im doing :

``````(do
(dosync (alter the-world ( #(+ % 20 ) @the-world)))) ``````

roelof19:02:42

I see this error message :

``ClassCastException java.lang.String cannot be cast to java.lang.Number  clojure.lang.Numbers.add (Numbers.java:128) ``

joost-diepenmaat19:02:57

`the-world` here is just a number right?

joost-diepenmaat19:02:20

you want `(dosync (alter the-world #(+ % 20 )))`

dpsutton19:02:22

@roelof what is that error message trying to tell you?

roelof19:02:23

nope, the world is a ref :

``(def the-world (ref "hello")) ``

joost-diepenmaat19:02:29

a ref to a number

joost-diepenmaat19:02:44

otherwise, why do you want to add 20 to it?

dpsutton19:02:50

@roelof what should the result of `(+ 5 "horse")` be?

roelof19:02:11

you cannot add a number to a string

dpsutton19:02:12

it should not be an error?

dpsutton19:02:20

` ClassCastException java.lang.String cannot be cast to java.lang.Number clojure.lang.Numbers.add (Numbers.java:128) `

joost-diepenmaat19:02:17

there’s a whole lot of things going wrong in that snippet @roelof

roelof19:02:35

wierd, I thought here the world would be a integer :

``````"Don't forget to do your work in a transaction!"
(= 0 (do
(dosync
(ref-set the-world 0 ))
@the-world))
``````

joost-diepenmaat19:02:09

so it’s not “hello” but 0

roelof19:02:35

as far as I see , yes

joost-diepenmaat19:02:41

if so, my snippet above should work: `(dosync (alter the-world #(+ % 20 )))`

alexmiller19:02:56

an equivalent to that is `(dosync (alter the-world + 20))` btw

roelof19:02:15

when I do this in repl :

``````(do
(dosync
(ref-set the-world 0 ))
@the-world)
``````

roelof19:02:41

I see as output 0 so therefore I assume it's a number ?

joost-diepenmaat19:02:46

@roelof the ClassCastException you get was (probably) because the ref contained a string at the time you tried it.

roelof19:02:54

why does that work and #(+ % 20) not ?

alexmiller19:02:18

both should work

joost-diepenmaat19:02:21

not because of any difference between (fn .. ) #( …) and + 20 - those constructs are all exactly equivalent

roelof19:02:22

then I think that the-world was containing something else then I thought

alexmiller19:02:41

that seems likely :)

joost-diepenmaat19:02:44

like if you did ` (def the-world (ref "hello")) ` before

dpsutton19:02:19

i have a sneaky suspicion that the jvm didn't know how to convenrt a string to a number

alexmiller19:02:54

correct - that will never happen automatically

dpsutton19:02:10

yeah i was joking around. that was the text of the error thrown