Idiomatic way to group a sorted list of integers?

inklesspen

I have a sorted list of integers, (1 2 4 5 6 6 7 8 10 10 10). I want to group them all, so that I get ((1) (2) (4) (5) (6 6) (7) (8) (10 10 10)).

So far I have this, which works:

(let ((current-group (list)) (groups (list)))
  (dolist (n *sorted*)
    (when (and (not (null current-group)) (not (eql (first current-group) n)))
      (push current-group groups)
      (setf current-group (list)))
    (push n current-group))
  (push current-group groups)
  (nreverse groups))

But I'm sure there must be a much more LISPy way to do this. Any ideas?

Rainer Joswig

Not that bad. I would write it this way:

(defun group (list)
  (flet ((take-same (item)
           (loop while (and list (eql (first list) item))
                 collect (pop list))))
    (loop while list
          collect (take-same (first list)))))


CL-USER 1 > (group '(1 2 4 5 6 6 7 8 10 10 10))
((1) (2) (4) (5) (6 6) (7) (8) (10 10 10))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Better way for concatenating two sorted list of integers

From Dev

Idiomatic Lisp way to create a list of sorted random numbers?

From Dev

How to group in ranges a sorted array of integers in Bash

From Dev

Efficient Scala idiomatic way to pick top 85 percent of sorted values?

From Dev

Is there an idiomatic D way to produce an array containing the integers from 1 to n?

From Dev

How to renumber reverse-sorted list of integers?

From Dev

Idiomatic way of handling nullable or empty List in Kotlin

From Dev

Idiomatic way of extracting lists from a list of tuples

From Dev

Idiomatic way to sum a list of Maybe Int in haskell

From Dev

Idiomatic way to do conditional list comprehension

From Dev

Idiomatic way to add multiple copies of an enum to a List

From Dev

What is the idiomatic way to write prepend for a linked list?

From Dev

Idiomatic way of extracting lists from a list of tuples

From Java

Is there a way to measure how sorted a list is?

From Dev

Given a sorted list of integers of length N, determine if an element x is in the list

From Dev

Java 8 idiomatic way to apply a Lambda to a List returning another List?

From Dev

How to group strings with similar beginnings in a sorted list?

From Dev

How to group strings with similar beginnings in a sorted list?

From Dev

How get a sorted list combined with group

From Dev

What is the most idiomatic (or the fastest) way to sum up a list in OCaml?

From Dev

What is the idiomatic way to write a linked list with a tail pointer?

From Dev

Scheme/Racket: most idiomatic way to append single element to end of list

From Dev

Idiomatic way to unpack variable length list of maximum size n

From Dev

Most efficient or idiomatic way to test singleton list contents in Haskell?

From Dev

Fastest or most idiomatic way to remove object from list of objects in Python

From Dev

Idiomatic Scala way to filter void values from list

From Dev

Is there an idiomatic way to print a vector(list) or a character string diagonally in J?

From Dev

Most efficient or idiomatic way to test singleton list contents in Haskell?

From Dev

An idiomatic way to construct a tree in python from a list of locations / namespaces?

Related Related

  1. 1

    Better way for concatenating two sorted list of integers

  2. 2

    Idiomatic Lisp way to create a list of sorted random numbers?

  3. 3

    How to group in ranges a sorted array of integers in Bash

  4. 4

    Efficient Scala idiomatic way to pick top 85 percent of sorted values?

  5. 5

    Is there an idiomatic D way to produce an array containing the integers from 1 to n?

  6. 6

    How to renumber reverse-sorted list of integers?

  7. 7

    Idiomatic way of handling nullable or empty List in Kotlin

  8. 8

    Idiomatic way of extracting lists from a list of tuples

  9. 9

    Idiomatic way to sum a list of Maybe Int in haskell

  10. 10

    Idiomatic way to do conditional list comprehension

  11. 11

    Idiomatic way to add multiple copies of an enum to a List

  12. 12

    What is the idiomatic way to write prepend for a linked list?

  13. 13

    Idiomatic way of extracting lists from a list of tuples

  14. 14

    Is there a way to measure how sorted a list is?

  15. 15

    Given a sorted list of integers of length N, determine if an element x is in the list

  16. 16

    Java 8 idiomatic way to apply a Lambda to a List returning another List?

  17. 17

    How to group strings with similar beginnings in a sorted list?

  18. 18

    How to group strings with similar beginnings in a sorted list?

  19. 19

    How get a sorted list combined with group

  20. 20

    What is the most idiomatic (or the fastest) way to sum up a list in OCaml?

  21. 21

    What is the idiomatic way to write a linked list with a tail pointer?

  22. 22

    Scheme/Racket: most idiomatic way to append single element to end of list

  23. 23

    Idiomatic way to unpack variable length list of maximum size n

  24. 24

    Most efficient or idiomatic way to test singleton list contents in Haskell?

  25. 25

    Fastest or most idiomatic way to remove object from list of objects in Python

  26. 26

    Idiomatic Scala way to filter void values from list

  27. 27

    Is there an idiomatic way to print a vector(list) or a character string diagonally in J?

  28. 28

    Most efficient or idiomatic way to test singleton list contents in Haskell?

  29. 29

    An idiomatic way to construct a tree in python from a list of locations / namespaces?

HotTag

Archive