void local fn DrawBalls( y as long, balls as CFArrayRef, sort as BOOL ) long col, prevCol = 0, x = 10 CFArrayRef cols = @[fn ColorWithSRGB(.78,.06,.18,1.0),fn ColorWhite,fn ColorWithSRGB(0,.24,.65,1.0)] if ( sort ) then balls = fn ArraySortedArrayUsingSelector( balls, @"compare:" ) pen -1 for long i = 0 to len(balls) - 1 col = intval(balls[i]) if ( sort == YES ) if ( col != prevCol ) then y += 30 : x = 10 else if ( i > 0 && i % 15 == 0 ) then y += 30 : x = 10 end if oval fill (x,y,30,30), cols[col] x += 30 prevCol = col next end fn void local fn DutchNationalFlagProblem window 1, @"Dutch national flag problem", (0,0,470,230) WindowSetBackgroundColor( 1, fn ColorWithSRGB(1.0,.61,0,1.0) ) CFMutableArrayRef balls = fn MutableArrayNew text @"Menlo-Bold",, fn ColorWhite for long i = 0 to 29 balls[i] = @(rnd(3)-1) next print @"Unsorted:" fn DrawBalls( 20, balls, NO ) print %(3,100)@"Sorted:" fn DrawBalls( 120, balls, YES ) end fn random fn DutchNationalFlagProblem HandleEvents