fibonacci.pi 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/env perl
  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. fun fibonacci(n) is cached {
  8. n < 2 ? n : _F_(n - 1) * _F_(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. # fun 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. let bhash = {one: 2, two: 1} # or
  40. let ahash = Hash.new(
  41. 2 -> two,
  42. 3 -> "this three",
  43. one -> 1,
  44. ) # can replace '->' with ','
  45. # declaring a code/block
  46. let 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. # _ is a topic variable
  53. hash.keys{ (_ + .lc).say }
  54. ahash{one}.say # accessing elements
  55. ahash{one, 2, 3}.each{ ... } # returns an array
  56. ahash{one, 2, 3} = ["One", 2, "ThReE"] # change values
  57. let key = "Hello"
  58. say ahash{key} if ahash.exists(key)
  59. del key # delete lexical var 'key'
  60. ## Arrays
  61. let array = ["one", 2, 3] # or
  62. let barray = Array.new("one", 2, 3)
  63. array.say
  64. array.each{ ... }.grep{ ... }
  65. let r = 3..4 # returns an array of values ranging from 3 to 4
  66. r.each { .say }
  67. # access values
  68. barray[0].say
  69. array[2..4].each({...})
  70. # lvalue
  71. array[0..1] = qw(i j)
  72. 'a'..'z'.each{ ahash.exists(_) && .say }
  73. ## Complex data structures
  74. let wtf = {
  75. one -> [ { two -> 3, three -> 4 }, [qw(a b c d)] ],
  76. two -> do { ... },
  77. three -> True ? [ { one -> 1, two -> 2 } ] : do { ... },
  78. }
  79. let b = [wtf, True]
  80. ## Working with Files
  81. const file = "/path/to/file"
  82. <file>.each_line{ .uc.say } # <a> open file "a" for reading if exists and return the File object
  83. <>.each_line{ ... } # read from standard input
  84. # flow constructs
  85. while (True) { ... }
  86. foreach v in array { ... }
  87. # classes
  88. class Person { ... }
  89. class Student << Person {
  90. has class is rw
  91. has notes is ro
  92. # multiple dispatch
  93. multi method get_nodes(v) {
  94. self.notes.each{ _ > v}
  95. }
  96. multi method get_nodes(v, k) {
  97. self.notes.each{x < _ < k}
  98. }
  99. }