Fork me on GitHub
#beginners
<
2016-11-20
>
jswart01:11:29

once they are defined with defn in the repl it should work

jswart01:11:41

defn works in the repl, not sure what you mean

dorianc.b01:11:14

I think it is because I’m running the repl line by line in the koans file

jswart01:11:39

Not really sure what that means either. How are you starting your repl?

jswart01:11:45

lein repl or boot repl ?

jswart01:11:13

@drewverlee one idea: (map (fn [[k v]] {:new1 k :new2 v}) (partition 2 (flatten (map vec things))))

jswart01:11:00

where things is [{:a 1} {:b 2}]

Drew Verlee02:11:23

@jswart nice, thats more clear then what i came up with.

noisesmith02:11:45

@jswart flatten is almost always the wrong thing - consider that vals and keys are both allowed to be collections

noisesmith02:11:35

for one layer of flattening, you can use (apply concat ...) or replace (map f (flatten ...)) with (mapcat f ...)

jswart02:11:33

sure, i likely wouldn’t use flatten but as the question is posed to beginners I figured it a decent way to answer the question

jswart02:11:07

with ~30 seconds of REPL'ing

jswart02:11:33

but i see what you mean, good points

noisesmith02:11:25

I wonder what that example should do if the map has more than one key in it

noisesmith02:11:43

user=> (map (fn [m] {:new-key1 (first (keys m)) :new-key2 (first (vals m))}) [{:a 1} {:b 2}])
({:new-key1 :a, :new-key2 1} {:new-key1 :b, :new-key2 2})

roelofw09:11:55

Is this the same (fn [acc n] (+ acc n)) and (#{+ acc n})

bbss09:11:34

The first is a function definition, the second is an attempt to call a set of symbols.

bbss09:11:09

#() with parens is a shorthand for a function call where arguments get bound to % (or %1, %2 etc). #{} is a notation for a set literal.

roelofw09:11:22

oops, back to the books

roelofw10:11:09

how do I make it work then the { replacing with ( and using % ?

bbss10:11:41

#(+ %1 %2) I think

bbss10:11:56

And you can then call it: (#(+ %1 %2) 1 1)

bbss10:11:11

although acc seems to imply that you are accumulating using a reducer maybe so not sure if it will be enough for your exercise 🙂

roelofw10:11:02

I already solved it this way : (fn [x] reduce (fn [acc n] (+ acc n))) where I had to calculate the total of a seq

roelofw10:11:18

but I wonder if I could it solve otherwise

bbss10:11:02

the total of a seq can be solved with (reduce + seq)

bbss10:11:16

I don't think that piece of code you pasted would work

bbss10:11:27

you are not calling reduce, and not using x

roelofw10:11:54

you are right , no coding when just out of bed (fn [x] (reduce (fn [acc n] (+ acc n)) x ))

bbss10:11:25

Yeah, that's a good solution.

roelofw10:11:45

I know, it's gets me on all tests of that challenge

roelofw10:11:04

so (reduce + seq) does also the job, @bbss ?

bbss10:11:35

yeah, but that's rather advanced, I wouldn't dive into that yet. It's because reduce can take a transducer, and + can function like a transducer.

roelofw10:11:58

Thanks, then look up what a transducer is

Alex Miller (Clojure team)14:11:37

Reduce does not take a transducer

Alex Miller (Clojure team)14:11:57

It takes a reducing function

Alex Miller (Clojure team)14:11:33

+ works in a reduce because it happens to satisfy the shape of a reducing function

Alex Miller (Clojure team)14:11:30

A transducer is a reducing function transformer

roelofw14:11:10

@alexmiller thanks, but I still do not get it . What do you mean wih a function transformer. Can I change on runtime a function ?

roelofw14:11:00

Another question : Can I somehow check if a outcome contains characters so I can do str to make a string out of it

roelofw14:11:07

the outcome is a list

Alex Miller (Clojure team)14:11:48

No, a transducer is just a function that takes a reducing function and returns a reducing function

roelofw14:11:33

oke, that part I have not learned till now. It sounds very advanced

Alex Miller (Clojure team)14:11:05

Usually you know you have a list of chars and can (apply str the-list)

roelofw14:11:57

Oke, this time I do not know. Im trying to solve a 4clojure function where I have to reverse a seq

Alex Miller (Clojure team)14:11:35

That doesn't have anything to do with chars?

roelofw14:11:14

and I noticed that (reduce conj '() <list>) works except when x is the string "racecar" then the outcome is (\r \a \c \e \c \a \r) and not the whole string

roelofw14:11:44

for ' ( 1 2 3) ` the function works very well

bbss14:11:58

right, I was afraid that wasn't entirely correct 🙂

roelofw14:11:31

@bbss my function is not correct ?

Alex Miller (Clojure team)14:11:00

That looks correct to me

bbss14:11:19

no I was referring to my naming things around the transducer

Alex Miller (Clojure team)14:11:29

There is no way to magically turn that back into a string just in that particular case

Alex Miller (Clojure team)14:11:28

And I don't think you should

Alex Miller (Clojure team)14:11:28

reduce and all sequence functions implicitly call seq on their argument. For strings, this turns them into a seq of chars

roelofw15:11:14

oke, then back to the drawing board

roelofw15:11:29

maybe use a map

roelofw15:11:59

I have to reverse a seq without using reverse

Alex Miller (Clojure team)16:11:56

What's the problem with the reduce above?

roelofw16:11:51

it does not work on strings

roelofw16:11:04

then the test on the4clojure is failing

roelofw20:11:54

sorry, I mixed up 2 challenges

roelofw20:11:15

I have to find out if something is a palindrome

roelofw20:11:44

so I did fn[x] (= x (reverse x)))

roelofw20:11:48

but I found out if you do reverse "ab" you get ( "/b" "/a) instead of "ba"

roelofw20:11:39

for a list it does work well reverse (1 2 3) gives `( 3 2 1) ' as expected

roelofw20:11:21

is there a way I can make it work for strings

jswart20:11:41

When you call a function that takes a seq on a string, it coerces the string into a seq of chars.

jswart20:11:53

Your solution will work, you just need to convert it back into a string when you are done reversing it.

roelofw20:11:15

oke, but how can I check if it's a string ?

jswart20:11:48

Are you checking more than one thing at a time? Or just checking if a string is a palindrome?

jswart20:11:56

If the latter, then you already know its a string.

roelofw20:11:59

I only check one thing at the time

roelofw20:11:23

I just check if something is a palindrome

roelofw20:11:50

but the input could be a string, a list , a #{}

jswart20:11:34

(type “string”)

roelofw20:11:59

Thanks all, I think I have to reconsider my code or make 2 functions

jswart20:11:13

You can do it with 1 function and an if.

jswart20:11:32

Don’t forget that in 4clojure they only give you one “space”, but in Clojure everything is an expression.

jswart20:11:53

So inside the (…) as long as you get the single answer to return, you will pass the 4clojure test case.

roelofw20:11:19

otherwise I do the same for a string except for a string I have to convert it back to a string

roelofw20:11:39

so something like (fn [x] (string? x (= x (str(reverse x)) (reverse x)))) right out of my head

roelofw20:11:48

I think it can be done better

jswart20:11:45

boot.user=> (map (fn [input] (let [reverser (if (string? input)
       #_=> (comp (partial apply str) reverse)
       #_=> reverse)]
       #_=>   (= input (reverser input)))) ['(1 2 3) [3 3 3] "abba"])
(false true true)

jswart20:11:59

that was what i camp up with

roelofw21:11:08

@jswart thanks, I will study this example

jswart21:11:44

np, the main “shift” in my example over yours is recognizing that functions are things we can pass as values and combine with comp and paritial It takes some getting used to and when i was doing 4clojure like you several years ago those ideas weren’t obvious. The best part of 4clojure is reading other peoples answers and then trying to understand them. Keep it up! Clojure is fun.

roelofw21:11:15

yep, it is fun

roelofw21:11:29

but hard to learn when to use which function

roelofw21:11:48

but when it was easy it was no so much fun

roelofw21:11:15

@jswart can I follow you at 4 clojure and do you know other people where I can learn from it

jswart21:11:52

Not sure what my account is, but after you answer a question you can go back and then view the answers from other people.

jswart21:11:06

Its built into the site and very helpful.

roelofw21:11:44

I know, but I have no clue which people to follow

jswart21:11:03

I just looked at random peoples answers, didn’t really follow anyone.

roelofw21:11:39

oke, thanks

roelofw21:11:53

time to sleep now