mighty

The mighty programming language, compiler and tools (WIP)
Log | Files | Refs

notes.md (1491B)


      1 
      2 @(expr)
      3 -------
      4 
      5 Evaluate `expr` once per element, with . bound to the current element.
      6 
      7 ```
      8 Examples:
      9 
     10 int[] doubled(int[] xs) =
     11     xs | @(. * 2)
     12 
     13 str[] labels(Reading[] rows) =
     14     rows | @("high" if .value > 100.0 else "normal")
     15 ```
     16 
     17 Masking or filtering
     18 ---------------------
     19 
     20 Masking/filtering works as you would expect at first, then evolves into something more like APL.
     21 
     22 It's best to call `[]` the Selector.
     23 
     24 ```
     25 xs[i]       // scalar index
     26 xs[is]      // gather by int[] indices
     27 xs[mask]    // compress by bool[] mask
     28 ```
     29 
     30 `xs[3]` returns the 4th element
     31 `xs[[0 2]]` returns the first and 3rd element
     32 `xs[0..-1]` returns the range from the 1st, to the one before last
     33 `xs[[true, false, true]]` returns the mask against the collection, in this case 1st and 3rd.
     34 
     35 
     36 Selectors can also be using expression to filter collections
     37 
     38 `integer_list[. > 0]` returns values higher than 0
     39 `rows[.valid]` within a struct, invoke valid(element) on each element
     40 
     41 Implied self
     42 ------------
     43 
     44 `.` is `self` within the context, it's the current subject.
     45 
     46 Inside a free function it's implied the first argument.
     47 
     48 `float len(Vec2) = math.sqrt(.x ^ 2 + .y ^ 2)`
     49 
     50 is equivalent to
     51 
     52 `float len(Vec2 v) = math.sqrt(v.x ^ 2 + v.y ^2)`
     53 
     54 inside a method of a struct, it's implied to this struct subject
     55 
     56 ```
     57 struct Vec2 ::
     58   float x, y
     59   float len() = math.sqrt(.x ^ 2 + .y ^2)
     60 ```
     61 
     62 Inside a selector, map, or other collection expr, `.` refers to the current element.
     63 
     64 `rows[valid]`
     65 
     66 
     67 
     68 
     69 
     70 
     71 
     72 
     73 
     74 
     75 
     76 
     77 
     78 
     79 
     80 
     81 
     82 
     83 
     84 
     85 
     86 
     87 
     88 
     89 
     90