StefanSalewski / NimProgrammingBook

Computer Programming with the Nim Programming Language -- A gentle Introduction
262 stars 19 forks source link

Debug code left in regex example #26

Open doug719 opened 1 year ago

doug719 commented 1 year ago

In part VI, process execution, parsing data files(in parallel), using regular expressions:

the code has if match(l,r,m): when true: #debug discard

Maybe this was intended to bypass the large amount of output. Maybe writing it to a file would be better.

StefanSalewski commented 1 year ago

Thanks for reporting. I think you are referring to the example

import regex
from std/strutils import repeat
const
  FileName = "csvdata.txt"
  P = """([^,]+)"""
  r = re(repeat(P & ',', 5) & P)

proc main =
  var m: RegexMatch
  for l in Filename.lines:
    if l[0] != '#': # skip first two and all other comment lines
      if match(l, r, m):
        when true: # debug
          discard
        else:
          for i in 0 .. 5:
            stdout.write(m.group(i, l))
            stdout.write(' ')
          echo ""
      else:
        assert false

main()

Yes, the statement "when true: discard" was intended to avoid all the output, because we try to compare the performance of various parsing strategies. With actual output, that would be dominated by the relatively slow output operation. Maybe I should better have defined a const named Debug, and used that const to suppress output. I think some of the other examples do that. I may adapt this example next time when I apply again some tiny grammar fixes. I think GPT-4 may find some more.

And congratulations that you have managed to read the book so far. Have you been able to follow the explanations and understand them?

Best regards,

Stefan Salewski

doug719 commented 1 year ago

Hello, Thank you for replying.  I think the explanations in the book are quite clear and have had no problem understanding them.  I appreciate that your code is easy to copy and paste, and not lose indentations.  I have been programming professionally since 1966 (scientific programming was all FORTRAN in those days), but now just program for enjoyment in languages that I like, such as nim, scheme and OCAML. You might consider expanding your section on sequtils to include additional functional functions such as foldl and foldr and how to "pipeline" functions:    data |> function1 >| function2 >| function 3

Regards,Doug Telford

On Monday, June 19, 2023 at 12:04:33 AM MDT, StefanSalewski ***@***.***> wrote:  

Thanks for reporting. I think you are referring to the example import regex from std/strutils import repeat const FileName = "csvdata.txt" P = """([^,]+)""" r = re(repeat(P & ',', 5) & P)

proc main = var m: RegexMatch for l in Filename.lines: if l[0] != '#': # skip first two and all other comment lines if match(l, r, m): when true: # debug discard else: for i in 0 .. 5: stdout.write(m.group(i, l)) stdout.write(' ') echo "" else: assert false

main()

Yes, the statement "when true: discard" was intended to avoid all the output, because we try to compare the performance of various parsing strategies. With actual output, that would be dominated by the relatively slow output operation. Maybe I should better have defined a const named Debug, and used that const to suppress output. I think some of the other examples do that. I may adapt this example next time when I apply again some tiny grammar fixes. I think GPT-4 may find some more.

And congratulations that you have managed to read the book so far. Have you been able to follow the explanations and understand them?

Best regards,

Stefan Salewski

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>