Created
July 29, 2011 01:29
-
-
Save shanejonas/1112953 to your computer and use it in GitHub Desktop.
Working with arrays in coffeescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # | |
| # Working with arrays, ranges and numbers in coffeescript: | |
| # I've been working with a lot of arrays to do lazy loading | |
| # heres how awesome the array syntax is: | |
| # you can use ranges to loop over functions. | |
| # heres a list of the numbers 100 through 115 | |
| big_list = (num for num in [100..115]) | |
| # i want to count to 100 to 110 | |
| console.log "count from 100 to 110." | |
| # count to 4 | |
| big_list[1..4].map (i)-> | |
| console.log i + '.' | |
| # each loop over an array | |
| big_list[5..6].forEach (i) -> | |
| console.log i + '.' | |
| # count to 7 to 10 | |
| # 3 dots means drop the end | |
| console.log i + '.' for i in big_list[7...11] | |
| # | |
| console.log 'count the rest anyways' | |
| console.log i + '.' for i in big_list[11..big_list.length] | |
| # count from 100 to 115. | |
| # 101. | |
| # 102. | |
| # 103. | |
| # 104. | |
| # 105. | |
| # 106. | |
| # 107. | |
| # 108. | |
| # 109. | |
| # 110. | |
| # count the rest anyways | |
| # 111. | |
| # 112. | |
| # 113. | |
| # 114. | |
| # 115. | |
| #produces the following javascript | |
| # | |
| # var big_list, i, num, _i, _j, _len, _len2, _ref, _ref2; | |
| # big_list = (function() { | |
| # var _results; | |
| # _results = []; | |
| # for (num = 100; num <= 115; num++) { | |
| # _results.push(num); | |
| # } | |
| # return _results; | |
| # })(); | |
| # console.log("count from 100 to 115."); | |
| # big_list.slice(1, 5).map(function(i) { | |
| # return console.log(i + '.'); | |
| # }); | |
| # big_list.slice(5, 7).forEach(function(i) { | |
| # return console.log(i + '.'); | |
| # }); | |
| # _ref = big_list.slice(7, 11); | |
| # for (_i = 0, _len = _ref.length; _i < _len; _i++) { | |
| # i = _ref[_i]; | |
| # console.log(i + '.'); | |
| # } | |
| # console.log('count the rest anyways'); | |
| # _ref2 = big_list.slice(10, (big_list.length + 1) || 9e9); | |
| # for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) { | |
| # i = _ref2[_j]; | |
| # console.log(i + '.'); | |
| # } | |
| # |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment