Reputation: 7705
Input, by example (4
is the maximum length):
(my-partition-randomly '(a b c d e f g h i j k l) 4)
Output:
'((a b c) (d) (e f g h) (i j k) (l))
The code should run in the Emacs Lisp interpreter.
Upvotes: 1
Views: 113
Reputation: 241988
My elisp-fu is weak, but I was able to write the following function:
(defun my-partition-randomly (list max-length) ""
(let ((result '()))
(while list
(push (seq-take-while (lambda (x) x)
(map 'list
(lambda (x) (pop list))
(number-sequence 1 (+ 1 (random max-length)))))
result))
(reverse result)))
It extracts random initial sequences of the input list and adds them to result (the seq-take-while
is needed not to include nil
s when the last subsequence wants to be longer than the remaining list). push
adds elements to the left, so the result has to reversed.
Upvotes: 2