Fork me on GitHub
#beginners
<
2015-12-09
>
kyle_schmidt03:12:38

Anyone have experience sending a file to download in compojure?

kyle_schmidt03:12:30

Is it possible for a user to submit a file via POST, the server does stuff to the file, then returns the changed file all in one swoop?

trancehime03:12:34

I've done image upload via compojure before, but I haven't had much experience with returning the changed file in the same operation

trancehime03:12:52

Though I suggest taking a look at output-stream to do the returning part

roelof06:12:03

Good morning. I try to solve the last exercise of the do-things of the brave book : http://www.braveclojure.com/do-things/ . To solve this one. Do I need to change the symmetrize-body-parts or the matching-part function ?

roelof08:12:45

Second question : Why do I see this error : CompilerException java.lang.IllegalArgumentException: Don't know how to create ISeq from: java.lang.Long, compiling:(C:/Users/rwobb/IdeaProjects/clojure-exercises/src/clojure_exercises/core.clj:12:1) on this code :

(defn my_intersection
  [set1 set2]
  (reduce (fn[acc item] (if (contains? set2 item) (conj acc item) acc)) {} set1 )
)


(my_intersection #{0 1 2 3} #{2 3 4 5})

cjmurphy09:12:54

Not sure, but don't you want to return a set, so the 'seed' for your reduce should be #{} rather than {}.

cjmurphy09:12:13

Yeah I just tested and that fixes it. Apart from that small error I think you nailed it! Gives the answer #{3 2}

trancehime10:12:03

So, when I use partition without supplying step I should get a sequences of lists containing n items each as I specified in n... If I want to check if the count of each list in that sequence is equal to a number, should I use map or apply

cjmurphy10:12:21

That sounds right to me. Your last one may have less in it or some may be left off I can't remember, I always look up https://clojuredocs.org/clojure.core/partition, then check its variants, to get what I want.

trancehime10:12:03

Well, partition when not supplying a step will drop the last set if it does not form a full partition

cjmurphy10:12:38

Yep, so you know what your count result will be

roelof11:12:39

@cjmurphy: thanks, you are my hero

roelof11:12:02

anyone who wants to help me to solve the last exercise of the do-things of the brave book

cjmurphy11:12:29

I only do 4clojure. ®️

roelof11:12:24

pity. I wonder to solve this I have to change the symmetrize function or also the parts function

roelof11:12:04

@cjmurphy: I do both to get a better feeling of the fp way of solving things

cjmurphy11:12:55

I need all my brain space right now for 96 on 4clojure. The last few easy ones are pretty hard. Maybe I'll still be at it by the time you catch up to me, and then you can give me a hand...

roelof11:12:41

@cjmurphy: yep, the last one are pretty hard

roelof11:12:00

For me there are not "easy" ones

roelof11:12:44

the next I have to do is re-implement iterate

cjmurphy11:12:56

You can tell they are not that easy when not so many people have completed them, yet they have a low number.

roelof11:12:37

for me there are not because I have trouble to solve them

cjmurphy12:12:45

You got that reduce pretty well, and reduce is pretty difficult when you first see it.

roelof12:12:28

thanks, I had lots of help here to find out what the right syntax is for reduce

roelof12:12:58

I try to do every day 1 or 2 challenges of 4clojure

roelof12:12:18

I hope someone else can help me

roelof12:12:23

I can wait

roelof12:12:57

For this one (http://www.4clojure.com/problem/62) Can I use loop - recur. Then if so, then I have to think how to stop the loop

cjmurphy12:12:23

96 is "Beauty is Symmetry"

cjmurphy12:12:20

That's a smart way of finding out!

cjmurphy12:12:59

If only I was more webby!

roelof12:12:20

try it but if Im wrong I have a function which works till infinity and a computer which will be slower and slower 😞

roelof12:12:28

more webby ??

Chris O’Donnell12:12:53

more knowledgeable about the web, I think he means

roelof12:12:52

oke, thanks

roelof12:12:10

I get the feeling that the remark of @cjmurphy was not intented to answer my question ?

cjmurphy12:12:45

Um - affirmative, that one, 96.

roelof12:12:24

For this one (http://www.4clojure.com/problem/62) Can I use loop - recur. Then if so, then I have to think how to stop the loop

cjmurphy12:12:21

Which you do by wrapping it with lazy-seq, which means nothing will happen unless you force it out, with something like a take.

cjmurphy12:12:24

So do it exactly the way you are thinking, then (lazy-seq it)

cjmurphy12:12:49

i.e. lazy-seq it!

roelof12:12:22

@cjmurphy: oke, time to hit the manuals to learn more about lazy-seq

roelof12:12:28

thanks again

cjmurphy12:12:34

Conceptually very interesting the whole laziness thing. Not exactly easy to think about, but possible to get results with regardless.

cjmurphy12:12:15

Actually even map is lazy, so we use laziness all the time.

roelof12:12:17

This is a whole new concept for me

roelof12:12:07

oke, I know map but never knew it was lazy

roelof12:12:19

clojure makes me learn every day new things

cjmurphy12:12:06

I actually think that here is the best place for questions about 'exercises in the Brave book'. If people want to help they join this beginners channel.

eggsyntax13:12:38

Laziness in Clojure is a pain point, IMO. 99% of the time everything just works and you don't even notice what parts are lazy and what parts aren't, and then every once in a while you get an error because of it, and those errors seem cryptic for quite a while after you start working w/ Clojure.

eggsyntax13:12:08

In some ways it'd be easier if the language were either consistently eager or consistently lazy. It's one of the few complaints I have about the language.

eggsyntax13:12:23

So @roelof it's not just you 😉

roelof14:12:44

I know it not just me

roelof14:12:51

already someone who wants can help me with my brave problem ?

trancehime14:12:32

Sorry, I shall slep

roelof14:12:07

good night then , @trancehime

roelof15:12:54

Why do I get this error message : ArityException Wrong number of args (0) passed to: core/eval1476/fn--1477 clojure.lang.AFn.throwArity (AFn.java:429) on this code :

(defn roelof
  [function number]
  (lazy-seq (cons number(function) )
  ))

(take 5 (roelof #(* 2 %) 1)) 

surreal.analysis15:12:27

You need a space between number and (function) would be my guess

surreal.analysis15:12:08

Nope, that's not it. I never realized that didn't matter in Clojure at all

surreal.analysis15:12:33

The issue is that you're calling function

surreal.analysis15:12:36

With no arguments

surreal.analysis15:12:53

But the function you passed in #(* 2 %)

surreal.analysis15:12:01

Takes an argument (represented by %)

roelof15:12:49

oke, the argument is number if I understand everything well ? @surreal.analysis

surreal.analysis15:12:26

I think so, but I'm not positive what you're trying to do. What do you expect the result of the last statement to be?

surreal.analysis15:12:03

Because cons also takes 2 arguments, but if you move number it will only take 1. It also needs a base collection

roelof15:12:09

I try to solve this challenge : http://www.4clojure.com/problem/62

surreal.analysis15:12:14

As it takes an element and a collection

roelof15:12:09

@surreal.analysis: then I have to think good what the collection is

surreal.analysis15:12:25

I think the issue is you likely want some recursion

roelof15:12:02

I think so, that why im using lazy-seq

surreal.analysis15:12:52

Right, but you want to call the roelof function from inside roelof

roelof15:12:47

oke, back to the manual to read more about lazy-seq

roelof15:12:16

@surreal.analysis: so more a function like this :

(defn sum-last-2 
   ([] (sum-last-2 1 2)) 
   ([n m] (cons n (lazy-seq (sum-last-2 m (+ n m))))))
 

surreal.analysis15:12:47

Yes, pretty similar, but you shouldn't need the 0 arguments option, and you need to replace the m (+ n m) part with your code

roelof15:12:13

Oke, thanks, I know then Im on the right track now

roelof15:12:06

hmm, still a problem : I have now this (cons number (lazy-seq (my-iterate number (function number))) but see this error message : ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn

roelof15:12:11

and when I do (cons number (lazy-seq (my-iterate function number)))) the function does not get executed I see as output { 1 1 1 1 1 }

roelof16:12:18

think I found it after looking good at the examples

roelof16:12:24

and solved simple_smile

roelof16:12:26

blush blush

roelof16:12:58

now hope someone can help me with my brave problem and I solved the number of exercises I want on a day

cab16:12:58

whats the brave problem?

roelof16:12:41

exercise 6 of the do-things chapter. See here :

6.Create a function that generalizes  symmetrize-body-parts  and the function you created in Exercise 5. The new function should take a collection of body parts and the number of matching body parts to add

roelof16:12:45

the functions can be found here : https://www.refheap.com/112518

roelof16:12:03

I wonder which function I have to change for it

roelof16:12:14

@cab : is my problem clear ?

cab16:12:45

which function did you make in Exercise 5?

cab16:12:50

matching-part?

roelof16:12:26

nope, I changed symmetrize-body parts

roelof16:12:39

I changed this

(set [part (matching-part part))
to this
(set [part (matching-part part "right1-")
                                         (matching-part part "right2-")
                                         (matching-part part "right3-")
                                         (matching-part part "right4-")
                                         (matching-part part "right5-") 

cab16:12:57

so you want to generalize that — make it so it accepts any number of parts

roelof16:12:46

and

{:name (clojure.string/replace (:name part) #"^left-"  "right-")  
to this
{:name (clojure.string/replace (:name part) #"^left-" replacement) 

cab16:12:14

do you know what you want to do conceptually?

roelof16:12:18

@cab : I believe that is what the exercise wants

roelof16:12:51

I think I change the multiple parts to one call and use something like repeatly

roelof16:12:10

I only do know if that is the right way

roelof16:12:18

and if you can make the output like left-arm1

roelof17:12:00

@cab that is why I asked the experts for help

roelof17:12:46

anyone here ??

roelof18:12:19

This is not working

(into final-body-parts (set [part ( repeatedly (matching-part part "right1-") number)
                                 ]))) 

roelof18:12:57

Then I see this error message : clojure.lang.PersistentArrayMap cannot be cast to java.lang.Number

roelof18:12:48

and I cannot change the replacent part to things like right- 1- / right-2-

base69819:12:30

@roelof: hmmm. are you trying to do set! or make a set out of a vector with part?

base69819:12:37

or maybe a let?

roelof19:12:13

no, im trying to solve a exercise of the brave book where I ,must generalize a function so it works on a given number and not on a fixed number

base69819:12:25

which excercise?

roelof19:12:09

exercise 6 of the do-things chapter

roelof19:12:21

I have to generilaze this function : https://www.refheap.com/112520

base69819:12:23

Here is one hint: (map #(str %1 "-") (range 1 n)) to change up how you have the symmetric thing in there.

roelof20:12:17

@base698: it this part right ;

(defn symmetrize-body-parts
  "Expects a seq of maps that have a :name and :size"
  ([asym-body-parts] (symmetrize-body-parts asym-body-parts 2))
  ([asym-body-parts n]

roelof20:12:32

why 2 functions with the same name

base69820:12:06

so it can take the optional number. You can call it like (symmetrize-body-parts parts-list) or (symmetrize-body-parts parts-list 5)

roelof20:12:07

oke, but here you set already a number :

symmetrize-body-parts asym-body-parts 2 

base69820:12:09

yeah, that's the default case when you call it like that it does 2 (mimicing left/right)

base69820:12:23

instead of left right it's 1/2

base69820:12:31

i'll leave that to you to figure out simple_smile

roelof20:12:44

oke. thanks

roelof20:12:31

oke, it works also without the optional number and then it looks like this : https://www.refheap.com/112526

roelof20:12:09

but now the output looks like this :

{:name "left-eye", :size 1}
 {:name "2-eye", :size 1}
 {:name "1-eye", :size 1} 

roelof20:12:55

What do I have to change to make it work like this :

{:name "eye1", :size 1}
 {:name "eye2", :size 1}
 {:name "eye3", :size 1} 

roelof21:12:10

the matching part function looks like this :

(defn matching-part
  [part replacement]
  {:name (clojure.string/replace (:name part) #"^left-" replacement)
   :size (:size part)})

roelof21:12:21

I hope anyone can help me ?

base69821:12:13

just swap the str part

base69821:12:35

and matching