Tips for simplifying a pattern that uses `cat`

Yesterday I was slicing up some breaks in a variety of ways and chaining them together into sequences like this:

d1 $ cat [
  slice 8 "0 1 2 3 4 5 6 7" $ sound "brk:1",
  slice 8 "0 1 2 0 1 2 5 [6 6]" $ sound "brk:1",
  slice 8 "0 1 [2 2 2 2] 0 1 2 6 6" $ sound "brk:1",
  slice 8 "0 1 2 0 1 2 0 0" $ sound "brk:1",
  slice 8 "0 1 2 ~ 4 4 [6 6] 7" $ sound "brk:1",
  slice 8 "0 1 2 0 1 2 5 [6 6]" $ sound "brk:1",
  slice 8 "0 1 [2 2 4 [~ 4]] 0 1 2 6 6" $ sound "brk:1",
  slice 32 "[0 1 2 0 1 2 6 6]/4" $ sound "brk:1"
  ]

Is there a more idiomatic way of doing this - the slice 8 and sound "brk:1" are quite repetitive - could I extract a function here? Or should I turn off my programmer brain and enjoy the repetition :slight_smile:?

cat is fine, you want to simplify the construction of the list. For instance, using map

d1 $ cat $ map (\ p -> slice 8 p $ sound "brk:1")
   [ "0 1 2 3 4 5 6 7"
   , "0 1 2 0 1 2 5 [6 6]"
   ]

Oh and I strongly recommend to put the commas in front. In Haskell circles, this is the "dutch style" of indentation.

[EDIT] I typed too fast - didn't see you want the 32 in the last item. Then use tuples:

cat $ map (\ (s, p) -> slice 8 p $ sound "brk:1")
   [ (8, "0 1 2 3 4 5 6 7")
   , (8, "0 1 [2 2 2 2] 0 1 2 6 6")
   , (12, "0 1 2 0 1 2 5 [6 6]")
   ]

Now you might not want those repeated 8s, so you could abstract again

cat $ map (\ (s, p) -> slice 8 p $ sound "brk:1")
   $ map (\ p -> (8,p))
       [ "0 1 2 3 4 5 6 7"
       , "0 1 [2 2 2 2] 0 1 2 6 6"
       ]
   ++ [ (32, "[0 1 2 0 1 2 6 6]/4")
      ]
3 Likes

alternatively, you can extract the slice and sound from cat, like:

d1 $ slice "<8!7 32>" (cat [
    "0 1 2 3 4 5 6 7",
    "0 1 2 0 1 2 5 [6 6]",
    "0 1 [2 2 2 2] 0 1 2 6 6",
    "0 1 2 0 1 2 0 0",
    "0 1 2 ~ 4 4 [6 6] 7",
    "0 1 2 0 1 2 5 [6 6]",
    "0 1 [2 2 4 [~ 4]] 0 1 2 6 6",
    "[0 1 2 0 1 2 6 6]/4"
  ]) 
  $ sound "bd:1"

the < > is the equivalent of cat in the mininotation, so <8!7 32> means: "8 for 7 cycles, 32 for 1"

4 Likes

Thank you both! Two different but very interesting ways of solving the problem. I've learnt a lot!