Jack.jack 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. vars it, p
  2. p = {label, value|
  3. print("\n" + label)
  4. print(inspect(value))
  5. }
  6. -- Create an array from 0 to 15
  7. p("range", i-collect(range(5)))
  8. -- Create an array from 0 to 15 and break up in chunks of 4
  9. p("chunked range", i-collect(i-chunk(4, range(16))))
  10. -- Check if all or none items in stream pass test.
  11. p("all < 60 in range(60)", i-all?({i|i<60}, range(60)))
  12. p("any < 60 in range(60)", i-any?({i|i>60}, range(60)))
  13. p("all < 60 in range(70)", i-all?({i|i<60}, range(70)))
  14. p("any < 60 in range(70)", i-any?({i|i>60}, range(70)))
  15. -- Zip three different collections together
  16. p("zipped", i-collect(i-zip(
  17. range(10),
  18. [1,2,3,4,5],
  19. i-map({i|i*i}, range(10))
  20. )))
  21. vars names, person, i, doubles, lengths, cubeRange
  22. names = ["Thorin", "Dwalin", "Balin", "Bifur", "Bofur", "Bombur", "Oin",
  23. "Gloin", "Ori", "Nori", "Dori", "Fili", "Kili", "Bilbo", "Gandalf"]
  24. for name in names {
  25. if name != "Bilbo" && name != "Gandalf" {
  26. print(name)
  27. }
  28. }
  29. person = {name: "Tim", age: 30}
  30. for key, value in person {
  31. print(key + " = " + value)
  32. }
  33. i = 0
  34. while i < 10 {
  35. i = i + 1
  36. print(i)
  37. }
  38. print("range")
  39. for i in range(10) {
  40. print(i + 1)
  41. }
  42. for i in range(10) {
  43. print(10 - i)
  44. }
  45. -- Dynamic object that gives the first 10 doubles
  46. doubles = {
  47. @len: {| 10 }
  48. @get: {key|
  49. if key is Integer { key * key }
  50. }
  51. }
  52. print("#doubles", #doubles)
  53. print("Doubles")
  54. for k, v in doubles {
  55. print([k, v])
  56. }
  57. -- Dynamic object that has names list as keys and string lenth as values
  58. lengths = {
  59. @keys: {| names }
  60. @get: {key|
  61. if key is String { #key }
  62. }
  63. }
  64. print ("Lengths")
  65. for k, v in lengths {
  66. print([k, v])
  67. }
  68. cubeRange = {n|
  69. vars i, v
  70. i = 0
  71. {
  72. @call: {|
  73. v = i
  74. i = i + 1
  75. if v < n { v * v * v }
  76. }
  77. }
  78. }
  79. print("Cubes")
  80. for k, v in cubeRange(5) {
  81. print([k, v])
  82. }
  83. print("String")
  84. for k, v in "Hello World" {
  85. print([k, v])
  86. }
  87. print([i for i in range(10)])
  88. print([i for i in range(20) if i % 3])
  89. -- Example showing how to do parallel work using split..and
  90. base = {bootstrap, target-dir|
  91. split {
  92. copy("res", target-dir)
  93. } and {
  94. if newer("src/*.less", target-dir + "/style.css") {
  95. lessc("src/" + bootstrap + ".less", target-dir + "/style.css")
  96. }
  97. } and {
  98. build("src/" + bootstrap + ".js", target-dir + "/app.js")
  99. }
  100. }
  101. vars Dragon, pet
  102. Dragon = {name|
  103. vars asleep, stuff-in-belly, stuff-in-intestine,
  104. feed, walk, put-to-bed, toss, rock,
  105. hungry?, poopy?, passage-of-time
  106. asleep = false
  107. stuff-in-belly = 10 -- He's full.
  108. stuff-in-intestine = 0 -- He doesn't need to go.
  109. print(name + ' is born.')
  110. feed = {|
  111. print('You feed ' + name + '.')
  112. stuff-in-belly = 10
  113. passage-of-time()
  114. }
  115. walk = {|
  116. print('You walk ' + name + ".")
  117. stuff-in-intestine = 0
  118. passage-of-time
  119. }
  120. put-to-bed = {|
  121. print('You put ' + name + ' to bed.')
  122. asleep = true
  123. for i in range(3) {
  124. if asleep {
  125. passage-of-time()
  126. }
  127. if asleep {
  128. print(name + ' snores, filling the room with smoke.')
  129. }
  130. }
  131. if asleep {
  132. asleep = false
  133. print(name + ' wakes up slowly.')
  134. }
  135. }
  136. toss = {|
  137. print('You toss ' + name + ' up into the air.')
  138. print('He giggles, which singes your eyebrows.')
  139. passage-of-time()
  140. }
  141. rock = {|
  142. print('You rock ' + name + ' gently.')
  143. asleep = true
  144. print('He briefly dozes off...')
  145. passage-of-time()
  146. if asleep {
  147. asleep = false
  148. print('...but wakes when you stop.')
  149. }
  150. }
  151. hungry? = {|
  152. stuff-in-belly <= 2
  153. }
  154. poopy? = {|
  155. stuff-in-intestine >= 8
  156. }
  157. passage-of-time = {|
  158. if stuff-in-belly > 0 {
  159. -- Move food from belly to intestine
  160. stuff-in-belly = stuff-in-belly - 1
  161. stuff-in-intestine = stuff-in-intestine + 1
  162. } else { -- Our dragon is starving!
  163. if asleep {
  164. asleep = false
  165. print('He wakes up suddenly!')
  166. }
  167. print(name + ' is starving! In desperation, he ate YOU!')
  168. abort "died"
  169. }
  170. if stuff-in-intestine >= 10 {
  171. stuff-in-intestine = 0
  172. print('Whoops! ' + name + ' had an accident...')
  173. }
  174. if hungry?() {
  175. if asleep {
  176. asleep = false
  177. print('He wakes up suddenly!')
  178. }
  179. print(name + "'s stomach grumbles...")
  180. }
  181. if poopy?() {
  182. if asleep {
  183. asleep = false
  184. print('He wakes up suddenly!')
  185. }
  186. print(name + ' does the potty dance...')
  187. }
  188. }
  189. -- Export the public interface to this closure object.
  190. {
  191. feed: feed
  192. walk: walk
  193. put-to-bed: put-to-bed
  194. toss: toss
  195. rock: rock
  196. }
  197. }
  198. pet = Dragon('Norbert')
  199. pet.feed()
  200. pet.toss()
  201. pet.walk()
  202. pet.put-to-bed()
  203. pet.rock()
  204. pet.put-to-bed()
  205. pet.put-to-bed()
  206. pet.put-to-bed()
  207. pet.put-to-bed()