This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-11-19
Channels
- # beginners (98)
- # boot (9)
- # business (2)
- # cljs-dev (10)
- # clojars (4)
- # clojure (36)
- # clojure-brasil (1)
- # clojure-dev (3)
- # clojure-poland (3)
- # clojure-russia (39)
- # clojure-spec (29)
- # clojure-uk (8)
- # clojurebridge (1)
- # clojurescript (70)
- # component (3)
- # cursive (2)
- # datomic (2)
- # figwheel (1)
- # hoplon (9)
- # klipse (29)
- # off-topic (7)
- # om (3)
- # onyx (11)
- # overtone (3)
- # rdf (1)
- # re-frame (18)
- # schema (1)
- # spacemacs (1)
- # untangled (5)
@roelofw how did you get on with that problem? There are number of ways of thinking about it but remember you are consuming a vector and producing a map so think about what assoc'ing into a vector might do? Two ways to solve this might be (without giving away the answers) 1. Start with an empty map and add key-value pairs to it - i.e. accumulate values in a map that starts empty- think about what fn might accumulate values 2. Create a sequence of key-value pair from the vector of keys and then change the sequence into a map
There's several variations on 2. as well including a handy fn that creates a map from a sequence of keys and vals.
@agile_geek I solved that problem with zipmap after some trying
Yep zipmap is that handy fn I mentioned.
@agile_geek Am I right that it is reduce <beginvalue> <function> ?
(reduce <fn> <initial-value> <collection>)
Where fn
takes two args [acc value]
acc
is the accumulated value and value
is the latest value from the collection
acc
starts as the initial-value
and if the initial-value
is not supplied it's the first value of the collection
@agile_geek thanks
this seems like an easy one:
{:a [1 2 ]}
=> [[:a 1] [:a 2]]
@agile_geek : this works :
(fn [s] (reduce (fn [n _] (inc n)) 0 s))
@drewverlee do you want a hint ?
@roelofw im leaning towards needing to map twice. Once for each key and then again for each value. But that is breaking my circuit somehow.
You can do that one with reduce too
Although it's probably a bit convoluted
are keys
and vals
ordered (they always return the same thing?)
i guess so
@drewverlee already solved the puzzle?
not yet, im getting close though. I had to draw out the intermediate states.
(defn foo [x]
(map (comp #(apply zipmap %) #(conj [(repeat (count (val %)) (key %) )] (val %))))
x)
{:a [1 2], :b [3 4]}
wait lol
maybe my i outlined the wrong problem. What i actual need is:
{:a [{:x 1 :z 2}]
=> [{:x 1 :z 2 :new :a}]
@drewverlee im still confused what you are trying to do
new is a key ill have to supply to the function.
i realized i hadn’t simplified the problem correctly.
(let [ds {:x [{:e 1} {:e 2}]
:y [{:e 5} {:e 5}]
}
m (fn [nk k a] (map #(assoc % nk k) a))
]
(map #(m :kind (first %) (last %))
ds))
(({:e 1, :kind :x} {:e 2, :kind :x}) ({:e 5, :kind :y} {:e 5, :kind :y}))
that seems awfuly convoluted
When I do this in 4clojure (fn [s] (filter odd? s))
I see this error message
Unable to resolve symbol: fn in this context, compiling:(NO_SOURCE_PATH:0)
4clojure might have some restrictions on using anonymous functions? maybe try #(filter odd? %)
could be done with a single pass through the data with reduce if you are doing a very big set of data
oke, I have to think. I thought partial is used for functions that a partially executed
I was going to say if you did, then a very simplified and not totally correct version of partial is:
only partial actually works with any arity, but this is just to demonstrate the intuitive idea
There is no “better”, but partial
has meaning where an anonymous function doesn’t. Using partial is telling the reader of the code that: I’m going to partially apply this function to create a new function that will take the rest of the information.
Partial can be used to make a very general function more specific. So you can use partial to create additional functions with some of the values already specified.
man this transformation should be easier then i’m making it:
(??? [{:a 1} {:b 2}] [:new-key1 :new-key2])
=>` [{:new-key :a :new-key2 1} {:new-key :b :new-key2 2}]`