src/bookofnim/deepdives/sugar

Source   Edit  

sugar deep dive

bookmark

TLDR

  • the sweetest nim syntax
  • the fusion pkg provides additional sugar, but its modules are dispersed through other files
  • working with data structures (e.g. sorting) generally requires std/algorithm

links

TODOs

  • std/wrapnils: optional chaining
  • macro capture(locals: varargstyped; body: untyped): untyped
    • capture local variables for use in a closure
  • macro dump(x: untyped): untyped
  • macro dumpToString(x: untyped): string
  • macro dup[T](arg: T; calls: varargsuntyped): T
    • echo for debugging expressions, prints the expression textual representation
  • std/algorithm
    • then review all the sort procs for each datatype (they all depend on algo)

do blocks and proc fn signatures

  • do blocks can be considered an alias for block:
  • proc expressions can use do notation when passed as a parameter to a proc
  • can also be used to pass multiple blocks to a macro
  • i.e.
    • do with paranthesis is an anonymous proc
    • do without paranthesis is just a block of code

sugar

sugar procs

  • -> for type defs in proc signatures
  • => for lambdas
expects a proc

Lets

myLambda = proc (s: string): auto {.noSideEffect.} = result = "hello " & s
=> macro: anonymous procs, same restraints as -> Source   Edit  

Consts

myHashSet = (data: [(0, 0), (0, 0), (0, 0), (-5492552493630111101, 4), (0, 0),
                    (0, 0), (6628840058955693702, 5), (0, 0), (0, 0), (0, 0),
                    (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
                    (-8667131158406858863, 3), (0, 0), (0, 0),
                    (6130242188011939732, 2), (0, 0), (0, 0), (0, 0), (0, 0),
                    (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
                    (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
                    (510521829891512613, 6), (0, 0), (0, 0), (0, 0), (0, 0),
                    (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
                    (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
                    (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
                    (8641844181895329213, 1), (0, 0), (0, 0), (0, 0), (0, 0),
                    (0, 0), (7204881626893065923, 9), (0, 0), (0, 0),
                    (-7855962892973491642, 7), (0, 0), (0, 0), (0, 0), (0, 0),
                    (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
                    (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
                    (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
                    (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
                    (-6375197177782184730, 10), (0, 0), (0, 0), (0, 0), (0, 0),
                    (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
                    (0, 0), (0, 0), (0, 0), (0, 0), (5112299225816896374, 8),
                    (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
                    (0, 0), (0, 0)], counter: 10)
Source   Edit  
myRange = (a: 1, b: 10)
Source   Edit  
mySeq = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Source   Edit  
myTable = (data: [(0, 0, 0), (0, 0, 0), (0, 0, 0),
                  (-5492552493630111101, 4, 8), (7204881626893065923, 9, 18),
                  (0, 0, 0), (6628840058955693702, 5, 10),
                  (-7855962892973491642, 7, 14), (0, 0, 0), (0, 0, 0),
                  (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0),
                  (0, 0, 0), (0, 0, 0), (-8667131158406858863, 3, 6),
                  (0, 0, 0), (0, 0, 0), (6130242188011939732, 2, 4), (0, 0, 0),
                  (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0),
                  (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0),
                  (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0),
                  (510521829891512613, 6, 12), (-6375197177782184730, 10, 20),
                  (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0),
                  (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0),
                  (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0),
                  (5112299225816896374, 8, 16), (0, 0, 0), (0, 0, 0),
                  (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0),
                  (8641844181895329213, 1, 2), (0, 0, 0), (0, 0, 0)],
           counter: 10)
Source   Edit  

Procs

proc cmpCustom[T](x, y: T): int
return 0, -1 or 1 for custom sorts requires == and < operators defined for type T Source   Edit  
proc echoThis(a: proc (x: string): string): void {....raises: [Exception],
    tags: [RootEffect], forbids: [].}
Source   Edit  
proc myProc(s: string): string {....raises: [], tags: [], forbids: [].}
Source   Edit  
proc runFn(x: string; fn: (string) -> string): string {....raises: [Exception],
    tags: [RootEffect], forbids: [].}
-> type declarations; doesnt support semicolons blah -> blah | blah {.somePragma.} -> blah Source   Edit