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.
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.
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 nils when the last subsequence wants to be longer than the remaining list). push adds elements to the left, so the result has to reversed.
seq-take-while and map, and this was kind of the intention of my question. Let’s see if there is an even simpler solution.