sample_3.pi 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/env ruby
  2. # x is a global variable which can be accessed from other namespaces
  3. # ex: if we were in the namespace ONE::TWO then access 'a' with ONE::TWO::W
  4. # it is mandatory for globals to be uppercased
  5. global W = 2
  6. # fibonacci sequence with ret value cached
  7. func fibonacci(n) is cached {
  8. n < 2 ? n : __FUNC__(n - 1) * __FUNC__(n - 2)
  9. }
  10. # forever loop
  11. forever {
  12. "Hello World".say
  13. }
  14. # async forever loop
  15. async forever {
  16. ["Hello", "World"].each{ .uc.shuffle + _ }
  17. }
  18. # func fibonacci asyncronously
  19. async fibonacci(10)
  20. # Working with blocks
  21. LABEL: {
  22. say "block one"
  23. # recurse to the current block
  24. _BLOCK_ if "one".shuffle(3) = "one"
  25. # recurse to the current block too
  26. _LABEL_ if "one".shuffle(3) = "one"
  27. {
  28. say "block two"
  29. # recurse to LABEL
  30. _LABEL_ if "two".shuffle(4) = "two"
  31. # recurse to the current block
  32. _BLOCK_
  33. }
  34. }
  35. ## Numbers
  36. 3.times{ ... } # ...: unimplemented
  37. ## Hash
  38. # declare a hash and store it in a lexically scope variable
  39. var bhash = { one = 2, two = 1 } # or
  40. var ahash = Hash.new(
  41. 2 = two,
  42. 3 = "this three",
  43. one = 1,
  44. ) # can replace '->' with ','
  45. # declaring a code/block
  46. var code = {
  47. let(k, v) = (_[0], _[2])
  48. say "#k, #{v isa Num ? v + 1 : v.uc }"
  49. }
  50. code(k, v) foreach k,v in ahash
  51. ahash.each_kv{ a.say; b.say }
  52. ahash.each_kv -> a, b {
  53. a.say
  54. b.say
  55. }
  56. # _ is a topic variable
  57. hash.keys{ (_ + .lc) }.each{ .say }
  58. hash.keys -> a { (a + a.lc) }
  59. ahash{one}.say # accessing elements
  60. ahash{one, 2, 3}.each{ ... } # returns an array
  61. ahash{qw(one 2 3)} = ["One", 2, "ThReE"] # change values
  62. var key = "Hello"
  63. say ahash{key} if ahash.exists(key)
  64. del key # delete lexical var 'key'
  65. ## Arrays
  66. var array = ["one", 2, 3] # or
  67. var barray = Array.new("one", 2, 3)
  68. array.say
  69. array.each{ ... }.grep{ ... }
  70. var r = (3..4).map{ .say } # returns an array of values ranging from 3 to 4
  71. # access values
  72. barray[0].say
  73. array[2..4].each({...})
  74. # lvalue
  75. array[0..1] = qw(i j)
  76. 'a'..'z'.each{ ahash.exists(_) && .say }
  77. ## Complex data structures
  78. var wtf = {
  79. one = [ { two = 3, three = 4 }, [qw(a b c d)] ],
  80. two = do { ... },
  81. three = True ? [ { one = 1, two = 2 } ] : do { ... },
  82. }
  83. var b = [wtf, True]
  84. ## Working with Files
  85. const file = "/path/to/file"
  86. <file>.each_line{ .uc.say } # <a> open file "a" for reading if exists and return the File object
  87. <>.each_line{ ... } # read from standard input
  88. # classes
  89. class Person { ... }
  90. class Student is Person {
  91. has class is rw
  92. has notes is ro
  93. # multiple dispatch
  94. method get_nodes(v) {
  95. self.notes.each{_ > v}
  96. }
  97. }
  98. # flow constructs
  99. var k = qw(one two three)
  100. for k -> a { a.uc.say }
  101. for k -> a is rw {
  102. a.shuffle
  103. }
  104. for k { .uc.say }
  105. var b = {
  106. one = 1,
  107. two = 2,
  108. three = 4,
  109. }
  110. for b -> k, v { (a + ':' + b).say }