# Count Items In Sublists

## Recommended Posts

Is there an Opusmodus function that counts the number of items in each sublist?

'((c4 d4 e4) (f4 g4) (a4) (b4 c5))

=> (3 2 1 2)

Thanks!

##### Share on other sites

(mapcar 'length '((c4 d4 e4) (f4 g4) (a4) (b4 c5)))

Applies a function (first argument) to each element of a list.

does the same as

(loop for subl in '((c4 d4 e4) (f4 g4) (a4) (b4 c5)) collect (length subl))

Thank you!

##### Share on other sites

• 2 weeks later...

I just learned from your  post on another topic that there is a opusmodus-function "get-count".

mapcar and loop works, but "get-count" is much more flexible for opusmodus.

So as I learned from you,  that is actually the right answer to the topic-question:  get-count .

Just want to mention it so when someone sees the topic will get the right answer and not my previous half answer.

(and there is also mclength to be found under opusmodus functions)

##### Share on other sites

Thanks, Erka. So using get-count, my above example would produce the same result as mapcar:

`(get-count '((c4 d4 e4) (f4 g4) (a4) (b4 c5)) :length :note)`

##### Share on other sites

(get-count '((c4 d4 e4) (f4 g4) (a4) (b4 c5)) ) would be the same as the mapcar or mclength .

With :length option of get-count you can specify if you only want  to count notes or rests.

In this case :length :note would give the same result.

There are good examples in the docs of get-count.

You used it in your pedal related post:

(setf counts-notes (get-count (omn :pitch mat))).

(get-count '((c4 d4 e4) (f4 g4) (a4) (b4 c5)) )
(mapcar 'length '((c4 d4 e4) (f4 g4) (a4) (b4 c5))).   ;That is basic from a LISP-view without knowing opusmodus.
(mclength '((c4 d4 e4) (f4 g4) (a4) (b4 c5)))

all return => (3 2 1 2)

Have a nice Sunday

Rolf

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×

• Lessons