Fork me on GitHub
#beginners
<
2016-11-10
>
mss03:11:26

beginner q about partition: why does (partition 10 ‘(1 2 3)) return an empty list? I'd think from the docs it would return either ((1 2 3)) or (1 2 3). am I totally off there?

curlyfry06:11:35

@mss The reason is that you do not have enough elements to make a single partition of that size. partition-all works the way you want.

roelofw06:11:42

someone who can help me telling me if im on the right track ?

avabinary11:11:14

can anyone please help me with a korma raw statement

avabinary11:11:23

i just can't seem to manage

avabinary11:11:43

i looked everywhere and the documentation is just not made for my needs

avabinary12:11:53

is there anybody out there ?

rauh12:11:21

@avabinary Usually you should go ahead and ask away. Don't ask to ask. That being said, I have no clue about Korma

avabinary12:11:48

i need to perform an upsert

avabinary12:11:54

tried using the

(raw ...)
inside
(insert users (values {:name "bob" :job "clojurian"} (raw "ON DUPLICATE KEY `name`="bob" UPDATE  ...")))

avabinary12:11:28

clojure.lang.ArityException: Wrong number of args (2) passed to: core/raw

st12:11:58

Not an expert, but I used korma in the past, anf for raw sql I used jdbc/execute!

avabinary12:11:48

not sure I want to do that, stepping out of korma prepared statements

avabinary12:11:24

on their docs, they say that (raw) will insert strings directly into their api

st12:11:10

@avabinary check that you are correctly escaping double quotes in your string

avabinary12:11:29

I used

(format ...)
and still same error

dominicm13:11:55

(raw "ON DUPLICATE KEY 'name'="bob" UPDATE ...") should be (raw "ON DUPLICATE KEY 'name'=\"bob\" UPDATE ...") I think

roelofw13:11:32

Someone who can give me some hints on exercise 5 here : http://www.braveclojure.com/do-things/#Exercises

roelofw13:11:07

I think I have to rewrite this code :

(defn better-symmetrize-body-parts
  "Expects a seq of maps that have a :name and :size"
  [asym-body-parts]
  (reduce (fn [final-body-parts part]
            (into final-body-parts (set [part (matching-part part)])))
          []
          asym-body-parts)) 

roelofw13:11:16

to make this work

roelofw14:11:48

and use iterate or repeat ?

roelofw14:11:57

Am I on the right track ?

seancorfield15:11:22

@roelofw: the exercise just says to write a similar function that produces 5 of each body part. I'd probably start afresh but use the structure of that function as a guide for my new function.

seancorfield15:11:41

Then exercise 6 wants you to produce another new function that accepts a number as well as a collection and produces a result with that many of each part.

seancorfield15:11:22

At least, that's my reading of what you linked to. I haven't read that book nor tried the exercises.

seancorfield15:11:52

Looking at the code, I'm guessing matching-part is key?

roelofw15:11:49

@seancorfield I think so. this is a part of the output of the orginal function :

[{:name "head", :size 3}
      {:name "left-eye", :size 1}
      {:name "right-eye", :size 1}
      {:name "left-ear", :size 1}
      {:name "right-ear", :size 1}
 

agile_geek15:11:19

@roelofw I too am not familiar with the exercises or the book however I think matching-part is currently returning the right- version of a left handside part e.g. given left-eye returns right-eye which better-symmetrize-body-parts is then adding as a set into the final-body-parts using reduce to accumulate the final body parts collection. Your going to have to create a fn that given a left body part - returns 5 body parts instead i.e. eye-1, eye-2 etc. and use that in a new version of better-symmetrize-body-parts. As with most things there are several ways to do this.

roelofw15:11:51

@agile_geek Oke, im thinking of chancing this line (into final-body-parts (set [part (matching-part part)]))) to (into final-body-parts(iterate 5 (set [part (matching-part part)]))))`

roelofw15:11:04

I only have to find out how I make the number a parameter of the other function so I get the output the challenge wanted

agile_geek15:11:23

@roelof try it but I don't think it will give you what you are expecting.

roelofw15:11:02

@agile_geek nope, it gives me this error message :

ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn  clojure.core/iterate (core.clj:2904) 

roelofw15:11:28

so again back to the drawing board

roelofw15:11:58

maybe I better can do chapter 4 and 5 first , Maybe then I get a idea how to solve these 2

agile_geek15:11:40

@roelofw (iterate f x) returns a lazy sequence of (x, (f x), (f (f x)), ....)

roelofw15:11:06

I still have the idea of doing the call to matching parts several times

agile_geek15:11:02

so you're asking it to call (5 (set [part (matching-part part)])) and 5 is not a function

roelofw15:11:56

Oke, that is not what I wanted. I wanted something like this in a imperatieve language ;

for i = 1 to 5 ; matching-part(i)  

cschep15:11:19

i think it might be better to modify matching-part to return what you want, rather than call it for the same result 5 times.

agile_geek15:11:23

Still think you have a problem - call (matching-part {:name "left-eye" :size 1}) in a repl and see what it returns...then think about calling it many times

agile_geek15:11:50

exactly what I suggested further up

roelofw15:11:41

that one gives this output : (do-things.core/matching-part {:name "left-eye", :size 1})

roelofw16:11:00

but when I do it like this :

(def asym-hobbit-body-parts [{:name "left-eye" :size 1}])

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

(defn better-symmetrize-body-parts
  "Expects a seq of maps that have a :name and :size"
  [asym-body-parts]
  (reduce (fn [final-body-parts part]
            (into final-body-parts (set [part (matching-part part)])))
          []
          asym-body-parts)) 

roelofw16:11:39

I see this output : [{:name "left-eye", :size 1} {:name "right-eye", :size 1}]

roelofw16:11:28

maybe I can change something in this line : {:name (clojure.string/replace (:name part) #"^left-" "right-")

roelofw16:11:39

and make there some sort of loop

roelofw16:11:41

No one who can give a hint how to solve this ?

cschep16:11:44

seems like a few of us have been really responsive

cschep16:11:51

it’s challenging, think about it, draw it out

cschep16:11:08

the prompt specifically says it’s difficult, read ahead and you might learn more and you can revisit it.

roelofw16:11:09

maybe I go that way. These exercises have cost me to many time

roelofw16:11:20

or maybe FP is not my thing

cschep16:11:50

keep reading, it’s good stuff. just know that it’s supposed to feel this way, no one gets it immediately.

roelofw16:11:19

Then time for chapter 4

cschep16:11:30

there we go.

roelofw16:11:09

@cschep ??? What do you mean with there we go

cschep16:11:02

haa, sorry I was just being encouraging. like.. hey that’s the spirit! good luck!

cschep16:11:12

you got this.

roelofw16:11:39

oke, I have read all the text very quick and I think I understand it

roelofw16:11:02

After dinner I will try to make the exercises of that chapter

roelofw16:11:28

and I hope I have more luck then with the exercises of chapter 3

roelofw16:11:32

Sorry, another question. I have made the suspects.cvs into the root directory.

roelofw16:11:00

and set it with this : (def filename "suspects.csv")

roelofw16:11:28

but when I do (slurp filename) I see a file not found error message

roelofw16:11:29

found it. it was a typo

roelofw16:11:29

It this right. Im confused now. I think I need a break

roelofw16:11:35

I have to do this : Turn the result of your glitter filter into a list of names

roelofw16:11:56

I did :

(defn only_names
  [records]
  (map :name records)) 

roelofw16:11:30

and used it like this :

(only_names(glitter-filter 3 (mapify (parse (slurp filename))))) 

roelofw16:11:58

and see this as answer : => ("Edward Cullen" "Jacob Black" "Carlisle Cullen")

roelofw17:11:33

and another one

roelofw17:11:44

I have this code :

(defn append_name
  [name gitter-index records]
  (into records  {:name name :gitter-index gitter-index})) 

roelofw17:11:16

and used it like this : (append_name(mapify (parse (slurp filename))) "Roelof" 2)

roelofw17:11:38

and now I see this error message : ClassCastException java.lang.Long cannot be cast to clojure.lang.IPersistentCollection clojure.core/conj--4345 (core.clj:82)

jjfine17:11:50

looks like records will be equal to 2 and into's first argument should be a collection

roelofw17:11:34

chips. you are right. IM mixing up my parameters

jjfine17:11:00

also never heard of mapify

roelofw17:11:50

nope, was not explained in this chapter

roelofw17:11:35

When I do (append_name "Roelof" 2 (mapify (parse (slurp filename)))) I see this output :

[:gitter-index 2]
 [:name "Roelof"] 

roelofw17:11:48

so something is still not right.

roelofw17:11:59

Clojure is not easy for a beginner

jjfine17:11:35

maybe walk back a bit. what's (mapify (parse (slurp filename))) evaluate to?

roelofw17:11:33

I see then this answer : => (fwpd.core/mapify (fwpd.core/parse (clojure.core/slurp fwpd.core/filename)))

jjfine17:11:59

sorry, i mean what is it's output?

roelofw17:11:25

Sorry, I did it again and saw this output :

mapify (parse (slurp filename)))
=>
({:name "Edward Cullen", :glitter-index 10}
 {:name "Bella Swan", :glitter-index 0}
 {:name "Charlie Swan", :glitter-index 0}
 {:name "Jacob Black", :glitter-index 3}
 {:name "Carlisle Cullen", :glitter-index 6})
 

roelofw17:11:58

@jjfine ^^^

jjfine17:11:27

yeah, ok. i think i understand what you're trying to do

jjfine17:11:59

if you look at into, the second argument is treated as a sequence

jjfine17:11:50

look at what happens when you call seq on {:name "Roelf" :gitter-index 2}

roelofw17:11:22

so I have to do (seq {:name "Roelf" :gitter-index 2} ) ?

jjfine17:11:55

not to get the right answer, but it should help explain why you're getting that result

roelofw17:11:11

when I do that I see this as output : ([:name "Roelf"] [:gitter-index 2])

roelofw17:11:30

and that is not what I want

jjfine17:11:32

that's why it added those two vectors to the list

roelofw17:11:01

so into is not the right function. maybe I have to use conj then

roelofw17:11:56

yes, that is the right one

roelofw17:11:04

I see now the right answer

roelofw17:11:33

I have a lot to learn . I have the feeling that I do baby - steps now

jjfine17:11:05

into and conj aren't the easiest functions to understand at first

roelofw17:11:49

time for a break before my head exploses

roelofw17:11:59

Many many thanks