Fork me on GitHub
#beginners
<
2017-05-03
>
jeff.engebretsen20:05:08

So I’ve gotten myself into this situation… I have a list of maps [{:a 1} {:a 2} {:b 1} {:b 2}] I need to split the list into two so that it’s [[{:a 1} {:b 1}] [{:a 2} {:b 2}]] But the number of entries is unknown. so the list will have n*m entries and I need to split them into m lists of n.

jeff.engebretsen20:05:14

Is there an elegant way to use take-nth or something like it to get m lists?

donaldball20:05:07

partition-all probably

jeff.engebretsen20:05:47

Doesn’t that just chunk in order?

noisesmith20:05:27

oh, so you probably want group-by and vals

jeff.engebretsen20:05:30

Basically I need to take-nth for 0..m

jeff.engebretsen20:05:17

Yeah, I’m doing that now, but my maps aren’t unique so my current solution of using mod of the index position isn’t working.

jeff.engebretsen20:05:23

(let [split (map (partial f path) views)
        flat (flatten split)
        cnt (count (first split))]
    (vals (group-by #(mod (.indexOf flat %) cnt) flat))))

jeff.engebretsen20:05:07

flat gives me the first thing i put above.

noisesmith20:05:11

as a rule of thumb, avoiding flatten is a good practice - it’s rarely the best choice

jeff.engebretsen20:05:38

Yeah, I was wondering if the way I’m manipulating the data may be an issue.

jeff.engebretsen20:05:24

A view starts like this…

{
      "id" : "background",
      "frame" : [
        "0.000000 0.000000 1.000000 1.000000",
        "0.000000 0.000000 1.000000 1.000000"
      ],
      "image_name" : "bg.png",
      "type" : "image",
      "alpha" : [
        1,
        1
      ],
      "hidden" : false
    }

jeff.engebretsen20:05:50

f is returning two of those with frame and alpha being a literal instead of a list.

jeff.engebretsen20:05:16

so since frame has 2 items it’ll return two copies. One for each frame index.

jeff.engebretsen20:05:28

Then I flatten and try to group.

noisesmith20:05:52

for a structure like that, I’d concat or mapcat instead of flatten

noisesmith20:05:10

it does less work, and is more specific to the input

jeff.engebretsen20:05:41

Would there be an easier way to get the same result (list of maps) from this [[{:a 1} {:a 2}] [{:b 1} {:b 2}]]?

jeff.engebretsen20:05:10

maybe a nested partition? is that a thing?

jeff.engebretsen20:05:47

Then I do the group-by. But my group-by fn isn’t working well.

jeff.engebretsen20:05:02

meh, I’ll just add a key/value to make them unique so that index works.

dpsutton20:05:50

(map (partial take-nth n) (take m (iterate rest coll)) would something like this work?

dpsutton20:05:23

walk down the collection one at a time with (iterate rest coll), and then to each of those map take-nth?

dpsutton20:05:44

(mapv (partial take-nth 3) (take 3 (iterate rest [:a :b :c :a :b :c :a :b :c])))
[(:a :a :a) (:b :b :b) (:c :c :c)]

dpsutton21:05:40

@jeff.engebretsen I believe the above should work for you

jeff.engebretsen21:05:45

I'll play with it. Thanks!

mss23:05:00

relatively new to the jvm. is there an idiomatic way in clojure to get the length in bytes of a string? the internet seems to be pointing me towards (.length (.getBytes "some string" "UTF-8")) I’m getting a funky error, though: No matching field found: length for class [B

mss23:05:15

struggling to parse what that class [B is

mss23:05:19

ah just found byte-array, which you can take the count of. nvm

noisesmith23:05:04

[B is a byte array, and it supports count, yes

noisesmith23:05:38

did that just smartquote on me?

noisesmith23:05:36

real repl

user=>  (count (.getBytes "hello, world!"))
13

noisesmith23:05:36

@mss the issue is that javac (which compiles java to bytecode) lets you use A.length() for arrays, but that isn’t a method call, it’s a special java syntax. Clojure doesn’t replicate java syntax (though it can access real methods)

mss23:05:21

oh interesting, that’s helpful to know

mss23:05:23

thanks for the feedback

fingertoe23:05:49

So I have a sample Java code - list.addJobSelectionCriteria(JobList.SELECTION_PRIMARY_JOB_STATUS_ACTIVE, Boolean.TRUE); Can somebody coach me on translation to Clojurescript interop?