- [Function]
(
fold-objects
function objects value {keyword value}*)
Maps a function of two arguments over a list
or seq
of objects. Value is the intitial value that
will be combined (folded) through the iterative application
of function over objects: each value returned by the
function becomes the next value passed to the
function as its second argument. Once mapping is complete the
final value is returned as the value of
fold-objects
.
fold-objects
supports all keyword parameters of map-objects except :slot!
.
Examples:
Example 1. Using fold-object
.
;;; define some random midi events to demonstrate mapping (define midis (loop for i below 100 collect (new midi :time i :keynum (between 20 100) :amplitude (between .1 .9) :duration (pick .2 .4 .6 .8)))) ;;; find total duration of all midis (fold-objects #'+ midis 0 :slot 'duration) ;;; return list of even keynums in the first 10 events (fold-objects #'cons midis '() :end 10 :slot 'keynum :test #'even?) ;;; return the unique key numbers in the middle-c octave: (fold-objects (lambda (x l) (if (member x l) l (cons x l))) midis '() :slot 'keynum :test (lambda (x) (<= 60 x 72))) ;;; count number of even keynums (fold-objects (lambda (x v) (+ v 1)) midis 0 :slot 'keynum :test #'even?) ;;; sum durations of midis whose keynums are between 60 80 (fold-objects (lambda (x v) (if (<= 60 (sv x :keynum) 80) (+ v (sv x :duration)) v)) midis 0) ;;; return the positions of all f-sharp keynums. (fold-objects (let ((p -1)) (lambda (x l) (incf p) (if (= (mod x 6) 0) (append! l (list p)) l))) midis '() :slot 'keynum)
See also:
map-objects
[Function]subobjects
[Function]sv
[Macro]