This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-11-20
Channels
- # beginners (102)
- # boot (23)
- # cljs-dev (1)
- # clojure (52)
- # clojure-canada (7)
- # clojure-korea (2)
- # clojure-poland (1)
- # clojure-russia (35)
- # clojure-spec (39)
- # clojure-uk (5)
- # clojurescript (64)
- # cursive (11)
- # events (1)
- # hoplon (168)
- # lein-figwheel (2)
- # luminus (14)
- # off-topic (47)
- # om (3)
- # om-next (1)
- # onyx (31)
- # quil (4)
- # re-frame (21)
- # spacemacs (1)
- # sql (1)
- # untangled (3)
- # yada (4)
@drewverlee one idea: (map (fn [[k v]] {:new1 k :new2 v}) (partition 2 (flatten (map vec things))))
@jswart nice, thats more clear then what i came up with.
@jswart flatten is almost always the wrong thing - consider that vals and keys are both allowed to be collections
for one layer of flattening, you can use (apply concat ...)
or replace (map f (flatten ...))
with (mapcat f ...)
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
I wonder what that example should do if the map has more than one key in it
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})
#() 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.
although acc seems to imply that you are accumulating using a reducer maybe so not sure if it will be enough for your exercise 🙂
I already solved it this way : (fn [x] reduce (fn [acc n] (+ acc n)))
where I had to calculate the total of a seq
you are right , no coding when just out of bed (fn [x] (reduce (fn [acc n] (+ acc n)) x ))
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.
Reduce does not take a transducer
It takes a reducing function
+ works in a reduce because it happens to satisfy the shape of a reducing function
A transducer is a reducing function transformer
@alexmiller thanks, but I still do not get it . What do you mean wih a function transformer. Can I change on runtime a function ?
Another question : Can I somehow check if a outcome contains characters so I can do str
to make a string out of it
No, a transducer is just a function that takes a reducing function and returns a reducing function
Usually you know you have a list of chars and can (apply str the-list)
Oke, this time I do not know. Im trying to solve a 4clojure function where I have to reverse a seq
That doesn't have anything to do with chars?
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
That looks correct to me
There is no way to magically turn that back into a string just in that particular case
And I don't think you should
reduce and all sequence functions implicitly call seq on their argument. For strings, this turns them into a seq of chars
What's the problem with the reduce above?
When you call a function that takes a seq on a string, it coerces the string into a seq of chars.
Your solution will work, you just need to convert it back into a string when you are done reversing it.
Are you checking more than one thing at a time? Or just checking if a string is a palindrome?
Don’t forget that in 4clojure they only give you one “space”, but in Clojure everything is an expression.
So inside the (…)
as long as you get the single answer to return, you will pass the 4clojure test case.
otherwise I do the same for a string except for a string I have to convert it back to a string
so something like (fn [x] (string? x (= x (str(reverse x)) (reverse x))))
right out of my head
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)
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.
@jswart can I follow you at 4 clojure and do you know other people where I can learn from it