Fork me on GitHub
#beginners
<
2016-10-19
>
shooodooken16:10:48

would appreciate some pointers on achieving this (simplified) transformation.. from data to transformed:

(let [data {:parent      {:childn   {:unknown-nested-data {}},
                          :childn+1 {}}
            :known-child {:unknown-nested-data {}}},
      data-expected {:childn      {:unknown-nested-data {}},
                     :childn+1    {},
                     :known-child {:unknown-nested-data {}}}
      transformed (-> (assoc {} :known-child (:known-child data))
                      (assoc,, (keys (:parent data)) {}))]
  (println (= data-expected transformed))
  transformed)
- number of children under :parent unknown (represented here as :childn, :childn+1) - :known-child is known key name

bwstearns17:10:12

that seems like a pretty big let statement. you might want to factor out the transformation into its own function for readability.

bwstearns17:10:49

can you give an example of an input data and the desired output?

bwstearns17:10:33

@shooodooken can you give an example of an input data and the desired output?

samueldev18:10:47

I'm having to do some interop w/ java

samueldev18:10:52

and wondering why this isn't working...

samueldev18:10:09

(defn web-credentials [{:keys [email password]}]
  (microsoft.exchange.webservices.data.credential.ExchangeCredentials. email password ""))

samueldev18:10:49

I'm getting "no matching constructor found", so I think I'm just not reading the java right

andreas-thoelke18:10:28

Hi! I'd like to use case with type, however this doesn't seem to work:

andreas-thoelke18:10:56

Any tricks I forgot? Do I have to use condp? match?

andreas-thoelke18:10:14

This works, but doesn't seem ideal:

Alex Miller (Clojure team)19:10:39

case cases have to be compile-time literals and classes are not compile-time literals

Alex Miller (Clojure team)19:10:03

if you’re going this route though, I’d suggest using .getName rather than str

Alex Miller (Clojure team)19:10:28

note that class names are not unique in a jvm though - every classloader is independent and can load the same class

Alex Miller (Clojure team)19:10:18

if you’re conditionally doing something based on type, the fastest way to do that in Clojure is with a protocol

andreas-thoelke20:10:41

Thank you for the explanation @alexmiller ! Now using (condp contains? (type ch) .. after @jr tip in #clojure

Alex Miller (Clojure team)20:10:53

Cool (but be aware that matching on classes like that may not work 100% of the time due to the classloader caveat above)

shaun-mahood22:10:10

I've got a problem that I know how to do with a C style for loop, but I can't figure out a nice way to do it functionally. I want to iterate over a vector, and if an item matches a specified value, I want to output the same vector with the item before the matching value removed. So if my original value was [1 2 a b c 3 4] and my function was matching on the value 3, the output should be [1 2 a b 3 4] Any ideas?

jswart23:10:38

Why not look for c ? Then you just use filter.

shaun-mahood23:10:46

@jawart: When I run it, I don't know the value of c - example is pretty contrived :)

jswart23:10:16

makes sense

jswart23:10:39

so in FP. You can always solve working with collections with recursion.

jswart23:10:46

That is loops w/o loops.

jswart23:10:51

Above that is reduce

jswart23:10:59

it can do anything recursion can do

jswart23:10:15

above that are ideas like partition or partition-all

jswart23:10:09

Is there anything else about c? Does the sequence repeat? Is it random?

jswart23:10:24

if it repeats I’d use map, take/drop and partition.

jswart23:10:33

If you really can only look at the value, then I would use reduce.

jswart23:10:44

or for really large amounts of data, loop/recur

jswart23:10:55

but reduce is almost always fast enough

jswart23:10:15

@shaun-mahood here is one thing you could do

jswart23:10:18

(filter identity (flatten (map (fn [[h t]] (if (= 'c h) t [h t])) (partition-all 2 x))))

jswart23:10:29

where (def x [1 2 'a 'b 'c 3 4])

jswart23:10:40

also written:

jswart23:10:48

you can of course wrap all taht in a function determine ’c dynamically