Last Updated: February 25, 2016
·
900
· jbilcke

Array::sample in CoffeeScript

Made this small one-liner for print-debugging insanely large arrays in the console:

Array::sample = (size=1) -> if @length then (@[i] for i in [0...@length] by Math.round @length / size) else @

Usage:

[0..1000000].sample 10

Will print:

[ 
  0,
  100000,
  200000,
  300000,
  400000,
  500000,
  600000,
  700000,
  800000,
  900000,
  1000000 
]

Warning: I did not handle the following case:

[0..10].sample 100

FATAL ERROR: JS Allocation failed - process out of memory

:)