The use of view types is tied to the implementation of `split` and `parseInt` 
which is why no one has shown it. You have to rewrite them with changed type 
signatures (which `parseutils.parseInt` already has on devel, so we just make a 
shim for it):
    
    
    import std/parseutils
    
    {.experimental: "views".} # needed for openarray in iterators apparently
    
    iterator splitView(s: openarray[char], chars: set[char]): openarray[char] =
      var lastAfterLine = -1
      var lastBeforeLine = 0
      for i in 0 ..< s.len:
        let c = s[i]
        if c in chars:
          yield s.toOpenArray(lastAfterLine, lastBeforeLine)
          lastAfterLine = -1
        else:
          if lastAfterLine < 0: lastAfterLine = i
          lastBeforeLine = i
      if lastAfterLine >= 0:
        yield s.toOpenArray(lastAfterLine, lastBeforeLine)
    
    # so we don't shadow strutils:
    proc parseInt(s: string): int =
      discard parseInt(s, result)
    
    proc `$`(s: openarray[char]): string =
      result = newString(s.len)
      for i in 0 ..< s.len: result[i] = s[i]
    
    proc parseInt(s: openarray[char]): int =
      when compiles(parseInt(s, result)):
        # in the development version, parseInt is implemented for 
openarray[char]
        discard parseInt(s, result)
      else:
        parseInt($s)
    
    let csv = readFile("./nim.csv")
    
    var totalSum = 0
    
    for l in csv.splitView({'\r', '\n'}):
      if l.len > 1:
        # broken for some reason:
        #let rowSum = mapIt(l.splitView({','}), it.parseInt).sum()
        for it in l.splitView({','}):
          totalSum += it.parseInt
    
    echo "Expected total = ", 2999999 * 3000000 / 2
    echo "Actual total = ", totalSum
    
    
    Run

Reply via email to