fork download
  1. (defun all-positions (item sequence &rest args &key from-end (start 0) end key
  2. test test-not)
  3. (declare (ignore from-end start end key test test-not))
  4. (loop for i = (apply #'position item sequence args)
  5. while i
  6. collect i
  7. do (setf (getf args :start) (1+ i))))
  8.  
  9. (defun split-by-positions (sequence positions)
  10. (when (or (null positions)
  11. (> (first positions) 0))
  12. (push 0 positions))
  13. (loop for (start end) on positions
  14. collect (subseq sequence start end)))
  15.  
  16. (dolist (s '("OFOFOFFOFOFFOFOFF"
  17. "FOO"
  18. "BAR"
  19. "O"
  20. ""))
  21. (format t "~S~%" (split-by-positions s (all-positions #\O s)))
  22. (assert (equal (apply #'concatenate
  23. (type-of s)
  24. (split-by-positions s (all-positions #\O s)))
  25. s)))
  26.  
Success #stdin #stdout 0.01s 25428KB
stdin
Standard input is empty
stdout
("OF" "OF" "OFF" "OF" "OFF" "OF" "OFF")
("F" "O" "O")
("BAR")
("O")
("")