NaN tagging in Nim?
Are there any existing implementation of NaN tagging in Nim? (I'm about to do it myself - or at least experiment with to see if there are any performance benefits - but having sth for reference wouldn't be bad at all :))
Re: Differences between simple assignment, shallowCopy and deepCopy
Thanks a lot. Very thorough explanation!
Binary resulting much larger with --opt:size?
I've just noticed sth very weird. I have 2 different tasks in my _nimble_ file. One builds the project with \--opt:size and one with \--opt:speed. The thing is the resulting binary with the first option is over 900KB, while with \--opt:speed it get down to 600-sth(!). How is that possible? Nim version: 1.0.3 [MacOSX: amd64] - Compiled at 2019-11-09 (git hash: 7ea60f78b5bd90bd34c5b15e1a80d23fd41c36a8) command options for 'mini' build: nim c --gcc.options.speed="-O4 -Ofast -flto -march=native -fno-strict-aliasing -ffast-math -ldl" --gcc.options.linker="-flto -ldl" --clang.options.speed="-O4 -Ofast -flto -march=native -fno-strict-aliasing -ffast-math -ldl" --clang.options.linker="-flto -ldl" -d:release -d:mini -d:danger --passL:parser.a --threads:on --hints:off --opt:speed --gc:regions --path:src -o:arturo -f --nimcache:_cache --embedsrc --checks:off --overflowChecks:on src/main.nim Run command options for 'full' (speed) build: nim c --gcc.options.speed="-O4 -Ofast -flto -march=native -fno-strict-aliasing -ffast-math -ldl" --gcc.options.linker="-flto -ldl" --clang.options.speed="-O4 -Ofast -flto -march=native -fno-strict-aliasing -ffast-math -ldl" --clang.options.linker="-flto -ldl" -d:release -d:danger --passL:parser.a --threads:on --hints:off --opt:speed --gc:regions --path:src -o:arturo -f --nimcache:_cache --embedsrc --checks:off --overflowChecks:on src/main.nim Run Any ideas?
Re: Web applications and pattern match
Good to hear! I couldn't find the source for the new version of the live demo though. In the markdown document it's still the old code as far as I can tell. Yes, please just ask!
A simple web page with utf-8 html form
I wrote a very simple web application just to learn how to do the job. Basically, the web page present a few lines of the Iliad, and has a visitor book. In the previous version, the visitor book worked with ASCII characters only. However, I noticed that many people wanted to test the page with accented names, Russian, Chinese, etc. Therefore, I changed the application, in order to allow people to sign the visitor book using many different scripts. Besides Adiliya Ravgatovna Kotovskaya, I added Valentin Glushko and Sergey Korolev with their names in Cyrillic, Kalidasa in Sanskrit, and Qian Xuesen in Chinese. The person who is writing the tutorial is Vernon Sipple. He will add the changes to the tutorial in due time. The main change is due to Vindaar. In the mean time, please, visit the page again: [http://medicina.tips/nim/nimweb.n](http://medicina.tips/nim/nimweb.n)
Re: Web applications and pattern match
Hi, Vindaar. I will use the solution you pointed out in the tutorial. By the way, if you visit the demo page, you will see that I already replaced the inicial version of the program with another one, which is based on the scanf. I also used the decode.Url procedure in order to test the utf-8 encoding. I still have a few questions, where I will need your help again.
Re: How to avoid recursive module dependency ?
@Stefan_Salewski, Thanks. @juancarlospaco Let me try.
Re: How to avoid recursive module dependency ?
Thanks for the reply
Re: Jester memory usage keep rising using sqlite
Thank you for your answer. I think that's because Nim does not free memory to OS. It's still ok for me that it's not memory leak
Inject into closures
Hi all, I have submitted issue [#12625](https://github.com/nim-lang/Nim/issues/12625) on GitHub on generating func-s (closures) with mapIt, where the code crashes nimvm or behaviors wrongly in runtime. Further checking indicates that the root cause is that, {.inject.} variables (it in this case) are [not injected into closures](https://github.com/nim-lang/Nim/issues/12625#issuecomment-553211144), but represented by a global variable. I repost it here, and hope that it can be resolved in a fundamental/systematic way. A quick fix for mapIt(func ...) can be map((it) => func ...), :)
Re: Is this a compiler bug or I took mistake?
Oh I see. Thank you.
Re: Array vs tuple for procs returning multiple values of same type
Not sure if this helps, but here an example: The Complex type (at the Standard Math/complex pure library) used to be a tuple, and was upgraded to a generic object. There are two possible representations of a complex number: rectangular and polar. The library and most functions are based on the Rectangular representation; however, there is a conversion to the Polar representation whose output is still a tuple: proc polar[T](z: Complex[T]): tuple[r, phi: T] proc rect[T](r, phi: T): Complex[T] Run I believe that a generic Polar type should have been defined for consistency, to allow: proc polar[T](z: Complex[T]): Polar[T] proc rect[T](p: Polar[T]): Complex[T] Run In such a way, Polar and Complex types can be used the same way.
Re: Compilation error after upgrade to 1.0.2 from 0.19
According to your error message, it seems tokens[ITEM_NAME] is Option[system.string] type and you are trying to add it to seq[string] type result variable. You might need to read this document and learn how to get a value from an Option type. [https://nim-lang.org/docs/options.html](https://nim-lang.org/docs/options.html)
Compilation error after upgrade to 1.0.2 from 0.19
The following code used to compile in nim 0.19: result = newSeq[string]() ... var tokens = li.match(re"\s*(\S+)\s*(?:>\s*(\S.*\S))?\s*").get.captures.toSeq() # Add to the list of list values result.add(tokens[ITEM_NAME]) Run Now, I get the following error: G:\src\nim\vim256\opts.nim(281, 19) Error: type mismatch: got but expected one of: proc add[T](x: var seq[T]; y: openArray[T]) first type mismatch at position: 2 required type for y: openArray[T] but expression 'tokens[0]' is of type: Option[system.string] proc add[T](x: var seq[T]; y: T) first type mismatch at position: 2 required type for y: T but expression 'tokens[0]' is of type: Option[system.string] 9 other mismatching symbols have been suppressed; compile with --showAllMismatches:on to see them Run As I have "been away" from nim for a relatively long period, I would appreciate some hint as how to remedy this...
Re: Can I "prune" directories with walkDirRect?
I guess the link to the Python 2 version of the library was only by accident. If some new functionality in Nim should be modeled after Python, refer to the documentation for Python 3. For most older libraries, there shouldn't be a big difference, but for newer libraries there may be, and even older modules might be improved in Python 3. So, here's the link: [https://docs.python.org/3/library/os.html#os.walk](https://docs.python.org/3/library/os.html#os.walk) . Note that I used `/3/` in the URL, so you'll get the documentation for the most recent Python 3 version. If you select a specific version (e. g. 3.8) from the drop-down menu at the top of the page, you'll get the documenation as of this version.
Re: How to avoid recursive module dependency ?
Put types on a dedicated file, `include` it.
euwren – a high-level wrapper for the Wren scripting language with a user-friendly DSL
Recently, I have been working on euwren, a high level wrapper for [Wren](http://wren.io/). The idea behind euwren is to be _the king of Wren wrappers_ , one that uses a user-friendly DSL and that makes wrapping Nim procs a piece of cake. All you have to do is list the things you need in the VM, and voila! The VM is ready to execute some Nim code. The wrapper is highly WIP, but the most essential things are ready to be used. You can find the wrapper [here](https://github.com/liquid600pgm/euwren). Feel free to suggest any features you'd like to see, or any bugs you find. I'll be happy to take care of them.
How to avoid recursive module dependency ?
Hi all, I have a recursive module dependency. How to solve this ? Say, i have two modules like this. #module_A.nim type a_Type* = ref object aVar* : b_Type Run And in another file, #module_B.nim type b_Type* = ref object bVar* : a_Type Run How can i avoide recursive module dependency ?
Re: How to avoid recursive module dependency ?
When you have types that depend on each other, you must place them under one `type` section—there's no way of avoiding that.
Re: How to avoid recursive module dependency ?
See also [https://forum.nim-lang.org/t/4745](https://forum.nim-lang.org/t/4745)
Re: Complex vs Rational implementations in the Standard Library
Congrats for 1.0 milestone. I'm glad to see that the Complex type is not a tuple any longer but an object and allows generics like the Rational type. That makes more sense now. Great job.
Re: Nim based Github Actions
I created GitHub Actions too. And I published it. [https://github.com/jiro4989/setup-nim-action](https://github.com/jiro4989/setup-nim-action) [https://github.com/marketplace/actions/setup-nim-environment](https://github.com/marketplace/actions/setup-nim-environment) But my actions is very simple (no caching). I will improve my actions with your cache stuff. Thank your knowledge.
Re: difference between ; and , as proc parameter separator
See the first 3 examples in [https://nim-lang.github.io/Nim/manual#procedures](https://nim-lang.github.io/Nim/manual#procedures). `;` is used to explicitly break the type propagation. In most use cases though (when not using `using`), there's not much distinction between `;` and `,`.
Re: Problems with Emacs mode for Nim
Be that as it may, the fact remains that nim-mode is written in elisp. ;) I don't even think these changes are hard at all, but I don't know my way around how to find the code responsible for those indentations without studying all of nim-mode first.
Re: ``Table.take`` should be ``Table.pop`` -- discuss
Ok. Just to close the loop on this thread I believe this accommodates all expressed views: [https://github.com/nim-lang/Nim/pull/12678](https://github.com/nim-lang/Nim/pull/12678) (except maybe one week pre-release implies backward compatibility forever view or the if one-table has a proc, all tables should have the proc view, but people didn't seem to comment on/care much about that aspect of this question).
Re: difference between ; and , as proc parameter separator
when you're listing multiple parameters under one type like this proc test(a, b: int) Run if you're gonna use semicolon, the a parameter wont be an int. in current nim thats just useful when you're using the using statement [https://nim-lang.github.io/Nim/manual.html#statements-and-expressions-using-statement](https://nim-lang.github.io/Nim/manual.html#statements-and-expressions-using-statement) using a: string proc test(a, b: int) # a is int proc test(a; b: int) # a is string Run
Re: Problems with Emacs mode for Nim
As far as I know, Vindaar, it is possible to program Emacs in any language through the Emacs Dynamic Mode. I believe that even in Nim. It is also possible to write extensions in neovim for any language, although the feature works better in the case of dynamic languages, such as Common Lisp, Haskell and python.
Re: the "type" of the curly-bracket structure
What Nim calls the Table Constructor as dom96 has correctly pointed out, the lisp world has long called "association lists", often abbreviated "alist". Since it is a macro you are writing, you could check that the kind of the NimNode is `nnkTableConstr` and if not emit a useful compile-time message for client code, as in something approximately like: import macros macro `@@@`(x: untyped): untyped = if x.kind != nnkTableConstr: error "triple-@ expects a {(,),(,)..} table constructor" var a = @@@{"fromStruct": "blah", "sub": {"joe": 10}} #var b = @@@[1,2,3] #Errors out with above message. Run
Re: Cross Platform Python Package
They have a wheel per platform and pip/conda select the correct one on install.
Re: Can I "prune" directories with walkDirRect?
The python os.walk is exceptionally convenient and supports such a use case - the iterator returns 3 components: "path", "dirs" and "files"; the user has to enumerate "files" (or dirs) themselves, and join themm to the "path" for the list of files, but can also ignore dirs or modify it - the iterator will only recurse into those still listed into dirs when re-called, so: if you ignore dirs, you get a standard recursion; if you empty it out, you get no recursion down from this path; and if you filter it, you get selective recursion. It has a few more bells and whistles that cover just about all use cases I've encountered: [https://docs.python.org/2/library/os.html?highlight=walk#os.walk](https://docs.python.org/2/library/os.html?highlight=walk#os.walk) Worth adding to standard library, I think.
Re: Accessing the terminating null byte in a string
It's only there for no-copy interop with C libraries. If you want to access it you need to cast with `cast[ptr UncheckedArray[char]](s[0].unsafeAddr)`
Re: difference between ; and , as proc parameter separator
None.
difference between ; and , as proc parameter separator
> What's the underlying difference between this signature: proc max(a: int; b: > int): int = and this proc max(a: int, b: int): int =?
Accessing the terminating null byte in a string
As per "Nim in Action", strings are lists of char`s terminated by `'0' (page 29). In the code below, how can I access that null byte? let s = "Abcd" echo s[s.len()] == '\0' # Error: unhandled exception: index 4 not in 0 .. 3 [IndexError] Run
Re: Web applications and pattern match
So originally I wanted to write a up a nice example to do the replacements via the scanf macro: [https://nim-lang.github.io/Nim/strscans.html](https://nim-lang.github.io/Nim/strscans.html) by defining tuples of strings to match against and their replacements, but I hit a dead end, because an element of a const tuple doesn't count as a static string for the pattern. Also scanf turned out to be more problematic than I thought, because the $* term does not like to match any string until the end. But since your book is (at least partly) about Nim macros and writing macros is fun, I built the following even longer version of your code, haha. It also includes a custom matcher that matches anything until the end of the string. # File: web.nim import strutils, os, strscans, macros let input = open("rage.md") let form = """ """ echo "Content-type: text/html\n\n" echo """ """ echo "" proc rest(input: string; match: var string, start: int): int = ## matches until the end of the string match = input[start .. input.high] # result is either 1 (string is empty) or the number of found chars result = max(1, input.len - start) macro match(args, line: typed): untyped = ## match the `args` via `scanf` in `line`. `args` must be a `[]` of ## `(scanf string matcher, replacement string)` tuples, where the latter ## has to include a single `$#` to indicate the position of the replacement. ## The order of the `args` is important, since an if statement is built. let argImpl = args.getImpl expectKind argImpl, nnkBracket result = newStmtList() let matched = genSym(nskVar, "matched") result.add quote do: var `matched`: string var ifStmt = nnkIfStmt.newTree() for el in argImpl: expectKind el, nnkTupleConstr let toMatch = el[0] let toReplace = el[1] let ifBody = nnkStmtList.newTree(nnkCall.newTree(ident"echo", nnkCall.newTree(ident"%", toReplace, matched)), nnkAsgn.newTree(matched, newLit(""))) let ifCond = nnkCall.newTree(ident"scanf", line, toMatch, matched) ifStmt.add nnkElifBranch.newTree(ifCond, ifBody) result.add ifStmt echo result.repr const h1title = ("# ${rest}", "$#") const h2title = ("## ${rest}", "$#") const elseLine = ("${rest}", "$#") const replacements = [h1title, h2title, elseLine] for line in input.lines: match(replacements, line) # produces: # var matched: string # if scanf("# ${rest}", line, matched): # echo h1title[1] % matched # if scanf("## ${rest}", line, matched): # echo h2title[1] % matched # if scanf("${rest}", line, matched): # echo elseLine[1] % matched echo form let qs = getEnv("QUERY_STRING", "none").split({'+'}).join(" ") if qs != "none" and qs.len > 0: let output = open("visitors.txt", fmAppend) write(output, qs&"\n") output.close let inputVisitors= open("visitors.txt") for line in inputVisitors.lines: match(replacements, line) inputVisitors.close echo "" input.close Run This is totally not practicle I'd say and one's better off writing something by hand or using the excellent [https://github.com/zevv/npeg](https://github.com/zevv/npeg) by @zevv. Still fun though. And if someone wants to improve on this... Finally, to just remove a prefix of a string, you may just use removePrefix from strutils: [https://nim-lang.github.io/Nim/strutils.html#removePrefix%2Cstring%2Cstring](https://nim-lang.github.io/Nim/strutils.html#removePrefix%2Cstring%2Cstring) Note that it only works inplace on string. You could use the new outplace though: [https://github.com/nim-lang/Nim/pull/12599](https://github.com/nim-lang/Nim/pull/12599)