Skip to content

Instantly share code, notes, and snippets.

@remy
Forked from benfoxall/sampled.js
Created December 10, 2012 14:29
Show Gist options
  • Save remy/4250863 to your computer and use it in GitHub Desktop.
Save remy/4250863 to your computer and use it in GitHub Desktop.
function sampled(arr, size) {
var sampled = [], i, n, idx, l = arr.length;
if (arr.length <= size){
return arr
} else {
for (i = 0; i < size; i++) {
n = (i / size) * l;
// faster Math.ceil (because I'm doing this with large arrays inside raf)
idx = (n << 0);
idx = (idx == n)? idx: idx + 1;
sampled[i] = arr[idx];
}
return sampled;
}
}
var letters = 'a b c d e f g h i j k l m n o p'.split(' ');
sampled(letters, 5); // a e h k n
sampled(letters, 10); // a c e f h i k m n p
// TODO I would like this to expand out to fill all the available space - but that's a nice to have
sampled(letters, 100); // a b c d e f g h i j k l m n o p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment