This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-05-03
Channels
- # aws-lambda (6)
- # beginners (38)
- # boot (39)
- # cider (44)
- # cljs-dev (9)
- # cljsrn (96)
- # clojure (142)
- # clojure-dev (6)
- # clojure-dusseldorf (8)
- # clojure-greece (45)
- # clojure-ireland (3)
- # clojure-italy (7)
- # clojure-norway (6)
- # clojure-russia (26)
- # clojure-sg (16)
- # clojure-spec (31)
- # clojure-uk (39)
- # clojurescript (125)
- # cursive (38)
- # datascript (4)
- # datomic (18)
- # emacs (34)
- # figwheel (2)
- # hoplon (3)
- # immutant (23)
- # jobs (1)
- # lambdaisland (2)
- # lumo (13)
- # off-topic (77)
- # om (8)
- # onyx (9)
- # pedestal (2)
- # play-clj (1)
- # re-frame (52)
- # reagent (3)
- # rum (4)
- # spacemacs (2)
- # specter (4)
- # unrepl (37)
- # untangled (8)
- # vim (79)
- # yada (1)
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.
Is there an elegant way to use take-nth
or something like it to get m lists?
partition-all
probably
Doesn’t that just chunk in order?
oh, so you probably want group-by and vals
Basically I need to take-nth
for 0..m
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.
(let [split (map (partial f path) views)
flat (flatten split)
cnt (count (first split))]
(vals (group-by #(mod (.indexOf flat %) cnt) flat))))
flat gives me the first thing i put above.
as a rule of thumb, avoiding flatten is a good practice - it’s rarely the best choice
Yeah, I was wondering if the way I’m manipulating the data may be an issue.
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
}
f is returning two of those with frame and alpha being a literal instead of a list.
so since frame has 2 items it’ll return two copies. One for each frame index.
Then I flatten and try to group.
for a structure like that, I’d concat or mapcat instead of flatten
it does less work, and is more specific to the input
Would there be an easier way to get the same result (list of maps) from this [[{:a 1} {:a 2}] [{:b 1} {:b 2}]]
?
maybe a nested partition? is that a thing?
Then I do the group-by. But my group-by fn isn’t working well.
meh, I’ll just add a key/value to make them unique so that index works.
(map (partial take-nth n) (take m (iterate rest coll))
would something like this work?
walk down the collection one at a time with (iterate rest coll)
, and then to each of those map take-nth?
(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)]
@jeff.engebretsen I believe the above should work for you
I'll play with it. Thanks!
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
[B is a byte array, and it supports count, yes
did that just smartquote on me?
real repl
user=> (count (.getBytes "hello, world!"))
13
@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)
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?
https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_72/rzahh/javadoc/com/ibm/as400/access/JobList.html#addJobSelectionCriteria(int, java.lang.Object) for full context….