ソースを参照

- Remove named list and unary operators `min`, `max`, and `minmax`
- Update doc on `Array` methods
- Change way of iterating with `for` loop

tcheukueppo 2 年 前
コミット
32cb95b670
2 ファイル変更52 行追加42 行削除
  1. 25 22
      docs/maat_dev_ref/maat.md
  2. 27 20
      docs/maat_dev_ref/objects/Array.md

+ 25 - 22
docs/maat_dev_ref/maat.md

@@ -37,8 +37,6 @@ inspired from the lovely `Perl`, `Raku` and `Lua` programming languages.
 ## Named unary operators
 
 - `defined`: (b) check if a varible is `nil` and return true otherwise
-- `chomp`: (b)
-- `chop`: (b) strip 
 - `sleep`: (b) call sleep() syscall
 - `return`: (b) return from a function
 - `exit`: exit program with given exit code
@@ -51,9 +49,7 @@ inspired from the lovely `Perl`, `Raku` and `Lua` programming languages.
 - `printfln`: (b) formatted string + a trailing new line and return to a file descriptor
 - `sprintf`: (b) sprintf, return formatted string
 - `sprintfln`: (b) sprintf + a trailing new line
-- `min`: (b) yield min from a list
-- `max`: (b) yield max from a list
-- `minmax`: (b) yield min and max, return them in a list
+- `join`: (b) 
 - `die`: (b) program dies with a message on `STDERR`
 - `warn`: (b) warn with a message on `STDERR`
 
@@ -94,6 +90,8 @@ inspired from the lovely `Perl`, `Raku` and `Lua` programming languages.
 
 - `?:`: tenary operator
 
+- `minmax`: 
+
 ## List of all operators from highest precedence to lowest
 
 - left        terms and list operators (leftward)
@@ -365,7 +363,7 @@ do { false } or die "failed"
 run a block asyncronously
 
 ```
-awork {
+work {
     4.sleep
     say "done"
 }
@@ -386,7 +384,7 @@ abide p
 
 Conditional `if` statement, note that paranthesis are optional.
 
-`if ... { ... } [elsif ... { ... } ...] [else { ... }]`
+`if ... { ... } [elsif ... { ... }] [else { ... }]`
 
 ```
 if true { say "it is true" }
@@ -425,22 +423,36 @@ else          { say "never here" }
 
 6. `for`
 
+`for` iterator either iterate over a comma-separated value or an Array object
+
+
+Iterating over an Array object
+
 ```
+-- three iterations
 for "a", r/regex/, [2, 4] { .say }
 
+var ar = a<one two three four five>
+
+-- only one iteration
+-- output: ["one", "two", "three", "four", "five"]
+for ar, { .say }
+
+-- ar.len + 1 iterations
+for ar…, 2 { .say }
+
 -- output: 3 3 5 4 4
-for ar -> i { i.len.say }
+for ar -> i { say i.len }
 
-ar = <one two three four five>
 -- "ar" is now [3, 3, 5, 4, 4]
 for ar -> j {
     j = j.len
 }
 
--- set a custom value when we are running out of elements
--- (3, 3) (5, 4) (4, none)
+-- set a custom default value when we are running out of elements
+-- output: (3, 3) (5, 4) (4, none)
 for ar -> i, j = "none" {
-    say "(#i, #j)"
+    print "(#i, #j) "
 }
 
 .say for ar
@@ -616,16 +628,7 @@ conditional construct to avoid the execution of a statement.
 ```
 var h = h{one 1 two 2 three 3}
 
-for h.each_kv { say "#_ => #__" }
-
-for h.each_kv {
-    (k,v)
-    once say 'only once!' if v == 1
-    printfln "%s => %d", k, v
-}
-
--- for syntatic suger
-for h.each_kv -> k, v {
+h.each_kv {(k,v)
     once say 'only once!' if v == 1
     printfln "%s => %d", k, v
 }

+ 27 - 20
docs/maat_dev_ref/objects/Array.md

@@ -27,8 +27,7 @@ var c = Array.new(2, 3, "four", a<5 6>)
 
 ### Note
 
-Let `a` be a variable containing an object of type `Array`, `a` has the following
-methods:
+Let `a` be a variable containing an object of type `Array`, `a` supports the following methods:
 
 - `a.len -> Num`: Return the number of elements in `a`
 - `a.of -> ` Tell us `of` what type are the elements of `a`
@@ -37,31 +36,39 @@ methods:
 - `a.minstr -> Any`: Return the minimum element in `a` based on stringwise comparison
 - `a.max -> Any`: Return the maximum element in `a` based on numeric comparison
 - `a.maxstr -> Any`: Return the maximum element in `a` based on stringwise comparison
-- `a.end -> Any`: Return the index of the last element in `a`
-- `a.minmax -> Array`: Return an `Array` of two elements, first and second is the max and min in `a`
+- `a.minmax -> Array`: Return an `Array` of two elements which are the max and min in `a` respectively.
+- `a.minmaxstr -> Array`: The stringwise version of `a.minmax`
 - `a.minmax_op(Array | Set | MSet | Bag | MBag b) -> Array`: Return an `Array` of two elements, first and second are the max and min in `a` and `b` resepctively
 - `a.minmax_op_str(Array | Set | MSet | Bag | MBag b) -> Array`: Stringwise version of `a.minmax_op`
-- `a.minmaxstr -> Array`: The stringwise version of `a.minmax`
+- `a.compact -> Array`: Remove all `nil` values in `a`
+- `a.uniq([ Fun f | Expr ]) -> Array`: Return a new Array object without duplicate elements
+- `a.first(Fun f | Expr)`: Return the first element for which its evalution be `f` or `Expr` returns a true value
+- `a.head(Str | Num n = 1) -> Any | Array`: Return the first `n` elements of `a` if `n >= 1` or the first `n - 1` elements if `n <= 0`. Fail is `a.is_empty`
+- `a.tail(Str | Num n = 1) -> Any | Array`: Same as `a.head` except that it  performs on the last elements of `a`
+- `a.shuf(str | Num n = 1) -> Array`: Shuffle elements in `a` `n` times and return `a`
+- `a.end -> Any`: Return the index of the last element in `a`
 - `a.keys -> Array`: Return an Array of indexes of `a`
-- `a.kv -> Map`: Return a new `Map` object elements of `a`, abort if `a.len` is odd
-- `a.ikv -> Map`: Return in a new object the `Map` version of `a` with indexes as keys
+- `a.hash -> Map`: Return a new `Map` object with elements from `a`, abort if `a.len` is odd
+- `a.ihash -> Map`: Return in a new object the `Map` version of `a` with indexes as keys
 - `a.rev -> Array`: Return a new array which contains reversed elements of `a`
-- `a.join([ Str | Num sep ]) -> Str`: Stringyfy elements in `a` and interleave them with the separator `sep` or empty string if not specified
-- `a.pop([ Num | Str n ]) -> Num | Array`: `n` defaults do `1` if omitted. At the end, pop and return `n` elements from `a`, as a `Num` if n is 1 else as an `Array`
+- `a.join([ Str | Num sep = '' ]) -> Str`: Stringyfy elements in `a` and interleave them with the separator `sep`
+- `a.pop([ Num | Str n = 1 ]) -> Num | Array`: At the end, pop and return `n` elements from `a`, as a `Num` if n is 1 else as an `Array`
 - `a.push(Any x [, Any y, ... ]) -> Array`: At the end, push into `a` elements passed as arguments to `push`
 - `a.append(Any x [, Any y, ... ]) -> Array`: Same as `push` except that it flattens arrays to append individual elements
-- `a.shift([ Num | Str n ])`: Same as `a.pop` but does it at the front
-- `a.unshift([ Num | Str n ])`: Same as `a.push` but does it at the front
-- `a.prepend() -> Array`: Same as `a.append` but does it at the front
-- `a.del(Num | Str n)`: Delete element at index `n`, fail if index does not exist in that array
-- `a.splice([ Num | Str start ] [, Num | Str end ] [, Array | Bag | MBag | Set | MSet b ]) -> Array`: 
-- `a.flat() -> Array`: It flattens the Array object so that all the elements found in nested array end up direct elements of `a`
-- `a.map(Fun f | Exp) -> Array`: Run function `f` or evaluate expression `Exp` for each elements in `a` and collect their return values into an array
-- `a.lmap(Fun f | Exp) -> Array`: Same as `a.map` but `.flat` the result return it
-- `a.grep(Fun f | Exp) -> Array`: Returns elements in `a` for which it evaluation by `f` or `Exp` return a true value
-- `a.last_by(Fun f | Exp) -> Any`:
-- `a.first_by(Fun f | Exp) -> Any`:
+- `a.shift([ Num | Str n ])`: Same as what `a.pop` does but does it at the front
+- `a.unshift([ Num | Str n = 1 ])`: Same as what `a.push` does but does it at the front
+- `a.prepend() -> Array`: Same as what `a.append` does but does it at the front
+- `a.del([ Num | Str n ])`: Delete element at index `n` or deleta, fail if index does not exist in `a`
+- `a.splice([ Num | Str s = 0 ] [, Num | Str l =  ] [, Array | Bag | MBag | Set | MSet b ]) -> Array`: Remove and return `l` elements from `a`, start at `s`, replace them by `b` if any
+- `a.flat -> Array`: It flattens the Array object so that all the elements found in nested array end up direct elements of `a`
+- `a.map(Fun f | Expr) -> Array`: Run function `f` or evaluate expression `Expr` for each elements in `a` and collect their return values into an array
+- `a.lmap(Fun f | Expr) -> Array`: Same as `a.map` but `.flat` the result before returning it
+- `a.grep(Fun f | Expr) -> Array`: Returns elements in `a` for which their evaluations by `f` or `Expr` return a true value
+- `a.each(Fun f | Expr) -> Array`:
+- `a.each_kv(Fun f | Expr) -> Array`:
+- `a.each_ikv(Fun f | Expr)`:
 - `a.cmp(Array b) -> Num`: 
 - `a.print -> Bool`:
 - `a.say`:
 - `a.dump`:
+