 
 : vsum ( vector-of-vectors -- vec ) { 0 0 } [ v+ ] reduce ;
 
-: vaverage ( seq-of-vectors -- seq ) dup vsum swap length v/n ;
+: vaverage ( seq-of-vectors -- seq ) [ vsum ] [ length ] bi v/n ;
 
 : average-position ( boids -- pos ) [ boid-pos ] map vaverage ;
 

: random-boids ( n -- boids ) [ drop random-boid ] map ;

! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

! : distance ( boid boid -- n ) boid-pos swap boid-pos v- norm ;

: distance ( boid boid -- n ) [ boid-pos ] [ boid-pos ] bi* v- norm ;



! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

: new-pos ( boid -- pos ) [ boid-pos ] [ boid-vel time-slice> v*n ] bi v+ ;

: new-vel ( boid -- vel )
  [ boid-vel ] [ acceleration time-slice> v*n ] bi v+ normalize* ;

: wrap-pos ( pos -- pos ) { [ wrap-x ] [ wrap-y ] } parallel-call ;

: iterate-boid ( self -- self ) [ new-pos wrap-pos ] [ new-vel ] bi <boid> ;

! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

