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
:)
Written by Julian bilcke
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Coffeescript
Authors
Related Tags
#coffeescript
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#