sample_3.pi 2.9 KB

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