nim-lang

reading files from tar.gz archive in Nim

自作多情 提交于 2019-12-10 14:24:54
问题 Looking for a way to read in a file from a tar.gz archive using the Nim programming language (version 0.11.2). Say I have an archive /my/path/to/archive.tar.gz and a file in that archive my/path/to/archive/file.txt My goal is to be able to read the contents of the file line by line in Nim. In Python I can do this with the tarfile module. In Nim there are the libzip and zlib modules, but the documentation is minimal and there are no examples. There's also the zipfiles module, but I'm not sure

create a reference to an array in Nim

对着背影说爱祢 提交于 2019-12-07 01:47:06
问题 var b: array[5, int] type ArrRef = ref array[5, int] var c : ArrRef echo repr(c) # nil c = addr b # doesn't compile, says type is Array constructor, expected reference In Nim, how can I pass references to arrays instead of passing by value? See the above code for what I have so far. 回答1: In Nim ref s are on the heap and have to be allocated with new . You can't just use a stack array as a ref because that would be unsafe: When the array disappears from the stack, the ref points to some wrong

Nim inter-thread message passing: How to avoid a global TChannel?

半腔热情 提交于 2019-12-06 21:44:19
问题 I have the following simple example of an inter-thread communication problem: I want to run arbitrary "anytime" algorithms in a background thread. An anytime algorithm performs some computation of result type T incrementally, i.e., it sporadically produces newer, more precise results. In Nim parlance, they are probably best represented by an iterator. In the main thread, I now want to wrap such iterators each in its own thread, with the possibility to query the threads for things like "is

How do I parse a string at compile time in Nimrod?

你。 提交于 2019-12-06 03:11:01
问题 Going through the second part of Nimrod's tutorial I've reached the part were macros are explained. The documentation says they run at compile time, so I thought I could do some parsing of strings to create myself a domain specific language. However, there are no examples of how to do this, the debug macro example doesn't display how one deals with a string parameter. I want to convert code like: instantiate(""" height,f,132.4 weight,f,75.0 age,i,25 """) …into something which by hand I would

Nim inter-thread message passing: How to avoid a global TChannel?

◇◆丶佛笑我妖孽 提交于 2019-12-05 02:47:40
I have the following simple example of an inter-thread communication problem: I want to run arbitrary "anytime" algorithms in a background thread. An anytime algorithm performs some computation of result type T incrementally, i.e., it sporadically produces newer, more precise results. In Nim parlance, they are probably best represented by an iterator. In the main thread, I now want to wrap such iterators each in its own thread, with the possibility to query the threads for things like "is there a new value available" or "what is the current computation result". Since I'm not familiar with Nim

How do I parse a string at compile time in Nimrod?

风流意气都作罢 提交于 2019-12-04 07:34:43
Going through the second part of Nimrod's tutorial I've reached the part were macros are explained. The documentation says they run at compile time, so I thought I could do some parsing of strings to create myself a domain specific language. However, there are no examples of how to do this, the debug macro example doesn't display how one deals with a string parameter. I want to convert code like: instantiate(""" height,f,132.4 weight,f,75.0 age,i,25 """) …into something which by hand I would write like: var height: float = 132.4 var weight: float = 75.0 var age: int = 25 Obviously this example

How to return an exit code from the main block in Nim?

只愿长相守 提交于 2019-12-04 03:33:26
问题 In Nim, to write code that executes as a sort of main function, you do (similar to the is main checks in Python): when isMainModule: echo ("Hello, Nim!") However, for the life of me, I can't figure out how to return an error code. Traditionally there has always been an option to make main functions return int , but since this is not actually in a proc , it doesn't seem like you can return ; the only thing I've figured out how to do is to raise an exception. Surely there is a way to just

How can I create a new primitive type using C++11 style strong typedefs?

瘦欲@ 提交于 2019-11-29 01:23:00
I'm trying to emulate in C++ a distinct type from the Nim programming language . The following example won't compile in Nim because the compiler catches the variables e and d having different types ( Error: type mismatch: got (Euros, float) ) despite both being a float at the binary level: type Euros = distinct float when isMainModule: var e = Euros(12.34) d = 23.3 echo (e + d) One way to do this in C++ would be to write a wrapper class for floats. But this doesn't work well with APIs which export the type because the size won't be the same as float. Or even if a class' size matches the

typed vs untyped vs expr vs stmt in templates and macros

↘锁芯ラ 提交于 2019-11-28 23:56:06
I've been lately using templates and macros, but i have to say i have barely found information about these important types. This is my superficial understanding: typed/expr is something that must exists previously, but you can use .immediate. to overcome them. untyped/stmt is something that doesn't to be defined previously/one or more statements. This is a very vague notion of the types. I'd like to have a better explanation of them, including which types should be used as return. The goal of these different parameter types is to give you several increasing levels of precision in specifying

How can I create a new primitive type using C++11 style strong typedefs?

喜夏-厌秋 提交于 2019-11-27 21:37:57
问题 I'm trying to emulate in C++ a distinct type from the Nim programming language. The following example won't compile in Nim because the compiler catches the variables e and d having different types ( Error: type mismatch: got (Euros, float) ) despite both being a float at the binary level: type Euros = distinct float when isMainModule: var e = Euros(12.34) d = 23.3 echo (e + d) One way to do this in C++ would be to write a wrapper class for floats. But this doesn't work well with APIs which