Re: Python transpiler

2020-07-14 Thread lscrd
I have translated several programs from Python to Nim. Some of them where quite simple and the translation was easy… for a human who knows what the program is intended for. Other were a bit more complicated, but as these are programs I wrote, there is little usage of dynamic features. In any ca

Re: Why Seq search is faster than Table search

2020-07-06 Thread lscrd
This is strange. I get the long execution time in release mode without changing a line in the code. Why is the code not optimized in my case? To get the same behavior, I have to use `-d:danger` instead of `-d:release`. May be it depends on some options of the C compiler. But, at least, we have

Re: Why Seq search is faster than Table search

2020-07-06 Thread lscrd
I changed the count for 1_000_000 to 100_000 and compiled your test with stable and devel in release mode and my results are totally different. With version 1.2.4: List: (seconds: 3, nanosecond: 71413798) Table: (seconds: 0, nanosecond: 496792) Run So, I retried w

Re: First look

2020-06-19 Thread lscrd
Which compiler did you use? Your program compiles correctly with 1.2.2 and #devel on a Linux platform. Note that your use of _echo_ is incorrect. As it is written, you write a tuple which contains the string "\nProcessor count " and an int. To get what you expect, you have to write:

Re: Visual Studio Code plugin

2020-06-18 Thread lscrd
Yes, you are right. But as Nim syntax doesn’t change a lot, this is understandable. Nevertheless, there are several issues which should have been solved (easier said than done of course). The alternative plugin seems currently more active. So, this may be a better choice in the future. But I th

Re: Visual Studio Code plugin

2020-06-18 Thread lscrd
Personally, I use the original plugin as I was not aware that an alternative one exists. This plugin seems in fact quite recent (first commit by Gary M on 20 April) and, logically, there is some activity on it. Besides, we can’t say that the original plugin is abandoned as the last commit was o

Re: Help understanding simple string pointer indexing example

2020-04-23 Thread lscrd
I didn’t use `create` but reading the documentation it is clear that the parameter `size`, whose default value is 1, is the number of elements to allocate. That is, for a string, 8 bytes for a pointer and, under the hood, 8 bytes for the length, 8 bytes for the capacity and 0 bytes for the actua

Re: Help understanding simple string pointer indexing example

2020-04-23 Thread lscrd
When you allocate the string using `create`, it is initialized with zeroes. So its capacity and its length are null as if it was assigned `""`. Then, if you assign globally the string, it works, but not if you assign each element individually. But this works: # test.nim proc m

Re: Help understanding proc()

2020-04-16 Thread lscrd
Sorry, but your second example is no more valid than the first one. In both cases, as @Hlaaftana said, you need to declare the proc before referencing it.

Re: Error: got proc, but expected proc {.closure.}

2020-04-15 Thread lscrd
You need to provide an anonymous proc. This works: import sugar proc calc(a: int, b: int): (int) -> int = let diff = a - b result = proc (c: int): int = result = c - diff let diff_proc = calc(5, 6) echo diff_proc(7) Run

Re: Destructor not called for ref object

2020-04-05 Thread lscrd
As far as I know, the default has not changed in 1.2. It’s better as some programs will not work with `--gc:arc` without changes (if you have created cycles). I suppose the default is likely to change with 2.0.

Re: Destructor not called for ref object

2020-04-05 Thread lscrd
Note that I have been able to get the right result by compiling your example with `--gc:arc`.

Re: Custom exceptions

2020-04-04 Thread lscrd
OK, thanks. My question was for a catchable error but, indeed, maybe I could use two exceptions in this case, one catchable, the other not catchable.

Custom exceptions

2020-04-04 Thread lscrd
Hi, Until now, the normal way to create a new exception type was by inheriting from Exception. At least, it was the recommended way in the manual (and is still). In the development version and in the new 1.2 version, this is considered bad style and you get a warning if you inherit from Excepti

Re: How does one get a mutable iterator?

2020-03-07 Thread lscrd
To make datum a variable you need to use mitems: import options type T = ref object data: seq[Option[T]] proc p(t: var T) = for datum in t.data.mitems: datum = none(T) Run

Re: Why does the this code work?

2020-02-19 Thread lscrd
I have tried to declare a `varargs[string | seq[string]]` but the compiler rejects it. It seems that for `varargs`, more strict rules apply to avoid some nasty problems. But there may exist other problematic cases as Araq said. Adding – in a future version – an explicit unpacking as he suggests

Re: Why does the this code work?

2020-02-18 Thread lscrd
As regards implicit dereferencing, the compiler, when looking for the field "a" has to detect that "p" is a reference or a pointer, check if the referenced type is an object or a tuple and that it contains a field "a" and if this is OK generate the code for dereferencing. I don’t see that as sty

Re: Why does the this code work?

2020-02-18 Thread lscrd
There is no reason to use a special syntax to tell the compiler that expansion is needed. Thanks to static typing, the compiler is able to see if the array or sequence must be transmitted as a whole to the procedure or must be expanded. In Python, you need a special syntax. Given a function _def

Re: How to export custom exception types?

2019-12-09 Thread lscrd
Exporting the type is not enough. You need to mark the fields you want to export with an * too. So, here, put an * after "extraData": type myModuleException* = ref object of Exception extraData* : uint32 Run

Re: Advent of Nim 2019 megathread

2019-12-01 Thread lscrd
As last year, I will use Nim and only Nim. [https://github.com/lscrd/AdventOfCode2019](https://github.com/lscrd/AdventOfCode2019)

Re: 1.0.0 is here

2019-09-23 Thread lscrd
This is a great day for Nim. Many thanks to the development team.

Re: What do you think about the programming language NIM?

2019-07-31 Thread lscrd
Thanks for your explanation.

Re: What do you think about the programming language NIM?

2019-07-31 Thread lscrd
This is Interesting. Thank you. So the documentation is wrong when it says: “Neither inline nor closure iterators can be recursive”. Here: [https://nim-lang.org/docs/manual.html#iterators-and-the-for-statement-first-class-iterators](https://nim-lang.org/docs/manual.html#iterators-and-the-for-sta

Re: What do you think about the programming language NIM?

2019-07-31 Thread lscrd
> Note that Nim iterators correspond to Python generators… I would like, but they are less powerful. Generators can be recursive in Python while iterators cannot. And in Python it’s easy to use a generator without a _for_ loop thanks to the _next_ function. But, despite these limitations, itera

Re: A few questions about procs

2019-06-16 Thread lscrd
You are right, I was too restrictive regarding static typing. Provided the code in the loop is compatible with each field type, it works.

Re: A few questions about procs

2019-06-16 Thread lscrd
You can iterate on an _openarray_ , of course. proc p(x: openarray[int]) = for val in x: echo val p([1, 2, 3]) p(@[4, 5, 6]) Run

Re: Natural is not positive

2019-06-15 Thread lscrd
It depends on authors. I said that French people generally consider 0 as being neither positive nor negative. For instance, a positive temperature is above 0°C. For the french mathematician group Nicolas Bourbaki, which is a well known reference, zero is both positive and negative. But that not

Re: Natural is not positive

2019-06-13 Thread lscrd
Sorry, I’m French and for me positive means strictly positive (as negative means strictly negative), but to avoid any ambiguity, I always use « strictement positif » for numbers > 0 and « positif ou nul » for numbers ≥ 0. Some authors (Bourbaki for instance) considers that zero is both a positiv

Re: rant about shr change

2019-05-30 Thread lscrd
Yes, there is indeed a change in the documentation between 0.19.6 and 0.19.9. Personally, for signed integers I would find more consistent to use an arithmetic shift to the right. With a logical shift, _-2 shr 1_ is equal to 9223372036854775807 on a 64 bits machine which may seem odd. We would r

Re: rant about shr change

2019-05-30 Thread lscrd
I have not been able to reproduce this behavior. On my Manjaro Linux with the last development version (built with _choosenim_ ) _shr_ is a logical right shift, not an arithmetic one. This is also the case with the last stable version. And there is already an arithmetic right shift in the syste

Re: Understanding performance compared to Numpy

2019-05-10 Thread lscrd
Very interesting explanation. As regards Python floats, they are native floats (64 bits long in IEEE 754 format), not arbitrary precision floats. Only integers are in arbitrary precision. So, I think that the rounding errors are also present in the Python program.

Re: type mismath in simple math

2019-04-22 Thread lscrd
To add to _mratsim_ answer, you can also write: import math echo 2.0^2 * 2.0 Run and even: import math echo 2.0^2 * 2 Run In the latter code, the compiler convert 2 to 2.0 as it knows that a float is expected. So, there is

Re: gintro demo with two columns in a listview / gtktreeview and sortable

2019-03-13 Thread lscrd
I have used "gintro" with a TreeView. There are some things that do not work the way I want (for instance the impossibility in Gtk3 to use alternate colors for rows if the theme doesn’t provide it), so I still use my previous version in gtk2. But here is how I proceed using _gintro_. To allocat

Re: How to immutably initialize parent object with private fields?

2019-02-15 Thread lscrd
Yes, you are right :-(. I forgot your requirement. I fear there is no simple solution in this case, as you need to create the object with the right type (to get the right size) and, at this moment, you don’t have access to the private field. In fact, I’m sure that my first solution with a cast

Re: How to immutably initialize parent object with private fields?

2019-02-14 Thread lscrd
In this direction, to make it work, you have to do a cast: proc newElementA(): ElementA = cast[ElementA](newElement("A")) Run Another way consists to create a proc _initElement_ in _lib_ : proc initElement*(elem: Element, id: string) = elem.id

Re: Convert float to its corresponding bytes?

2019-01-26 Thread lscrd
Sorry, I don’t see any aliasing in the code presented by `e`. The float value is copied into an array of uint8 and there is no sharing of memory. It would be different is one had used pointers, of course. But, with unions, you have an aliasing as two fields refer to the same address in the unio

Re: Nim Advocacy & Promotion Strategies

2019-01-26 Thread lscrd
No, without `global` it works and even this way: def f(): print(x) x = 4 f() Run But, the following doesn’t work and you have indeed to specify `global` (quite different from Nim here as the “equivalent” would be `var x = 2 * x + 1` which compiles

Re: Nim Advocacy & Promotion Strategies

2019-01-25 Thread lscrd
> There is no var keyword that you have to use when first create a variable. My > "implicit var" proposal was for Nim to behave the same way for b = 2 as for > var b = 2. You'd still get b type-inferred as int with static checking like > in Crystal (or like with b := 2 in Go). But there are cas

Re: Nim Advocacy & Promotion Strategies

2019-01-20 Thread lscrd
I don’t understand when you are saying that `b = 2` means `let b = 2` in Python. Python has neither an equivalent of `var`, nor an equivalent of `let`. When you write `b = 2`, you simply bind the object `2` to the name `b`. And that doesn't prevent you to write afterwards `b += 1`, so it certain

Re: Nim and Project Euler

2019-01-12 Thread lscrd
I mostly agree with you and I am also rather optimistic for the future of Nim. The community is tiny, but composed of truly motivated Nim users. My remark about the small number of Nim users in Project Euler was there only to give true numbers, and yes, Nim is certainly a marginal language here.

Re: Nim and Project Euler

2019-01-12 Thread lscrd
Yes, I can understand that you don’t appreciate Project Euler tasks. They may seem less fun that, for example, those of Advent of Code. Most of them have a strong mathematical nature and not everybody likes to deal with prime numbers and totient function. So, you have to like mathematics or, mor

Re: Nim and Project Euler

2019-01-11 Thread lscrd
> Any ideas how to make it more popular? :-) In the Project Euler community, this is not easy. Members are mostly mathematicians and either they use main languages such as C, C++, Java, Python, or they use languages such as Haskell (6863 users!) or more logically Mathematica or Matlab. For thos

Nim and Project Euler

2019-01-11 Thread lscrd
Hi all, In another thread, someone ( _mratsim_ ) has spoken of a lot of users using Nim for Project Euler. Maybe members of this “project” have done a lot of noise to make illusion, but I want to give some more precise data regarding Nim and Project Euler. For those who do not know about this

Re: Nim Advocacy & Promotion Strategies

2019-01-11 Thread lscrd
For Python users, the syntax of Nim is not disturbing whereas this may be an obstacle for those using another language. But, this is a trap. Despite their syntax similarities, Python and Nim are very different languages and those who are not aware of the constraints of a statically typed and non

Re: Advent of Code 2018 megathread

2018-12-16 Thread lscrd
I encountered the same difficulties. The fact that the problem provides several test cases shows that the creators are aware of the difficulties. There are several traps to fall into. And, for part 2, there are also performance issues. For now, this problem has been the most challenging. Day 16

Re: Cannot prove initialization, again.

2018-12-13 Thread lscrd
It seems that I was not in a great shape yesterday. There are some errors in my message. Indeed, a set of _range[ '0'..'9']_ will occupy 58 bits rounded to 64, not 9 bits rounded to 16. I will have to declare _Digit_ as a _range[0..9]_ to use only 10 bits (not 9!) rounded to 16 and, in this cas

Re: Cannot prove initialization, again.

2018-12-12 Thread lscrd
Thanks, it works! But, as this only hides the warning and doesn’t suppress the check , I hope that, despite what is said in the message, this warning will never become a compile time error.

Cannot prove initialization, again.

2018-12-12 Thread lscrd
Hi, When trying to solve some project Euler problem, I declared something like that: import tables const N = 16 type Digit = range['0'..'9'] var counters: array[N, TableCount[Digit]] var exclusion: array[N, set[Digit]] . . . Run and got the usual wa

Re: [help] indirectly imported types not working?

2018-12-09 Thread lscrd
Marking the type as exported is not sufficient. You have to mark the field pos as exported (and also for the field dir I guess): import nico/vec type GameObject* = ref object of RootObj pos*: Vec2i Player* = ref object of GameObject dir*: i

Re: Deprecation of "round" with two arguments

2018-11-23 Thread lscrd
Yes, I agree that this function is not generally what is needed. Most of the time, we want a string and "format" is what should be used. I didn’t wanted to discuss the decision, I was just curious to know if there exists situations where it actually gives a wrong result. Now, in my case, this i

Deprecation of "round" with two arguments

2018-11-23 Thread lscrd
Hello all, When compiling (with development version of Nim) a module which uses the "round" function from the "math" module, more precisely the "round" function with two arguments (the second one being the number of positions after decimal point), I got a deprecation warning. The recommended wa

Re: Should we get rid of style insensitivity?

2018-11-23 Thread lscrd
Thanks for the precision. But is it really style insensitivity? That’s what I thought until I understood that it is only a consequence of ignoring the underscores in identifiers as underscores are ignored in numbers. So Nim style insensitivity may be seen only a consequence of Nim case insensit

Re: Should we get rid of style insensitivity?

2018-11-22 Thread lscrd
> Stating that the same group of people dislike style insensitivity, GC, etc is > a bold claim. That’s not what I meant. I suppose I should have been more precise :-). What I mean is that some people will find any reason to reject a language they don’t like. And why do they not like this langua

Re: Should we get rid of style insensitivity?

2018-11-22 Thread lscrd
I think that changing Nim to get more users is exactly what should not be done. A language should not be adapted to suit the opinion of a majority of potential users. Rather users have to learn other ways to work. For me, many of those who definitively rejected Nim for its case insensivity will

Re: Should we get rid of style insensitivity?

2018-11-21 Thread lscrd
For me, this idea of voting is strange and even makes me uncomfortable. Does the creator of the language agree with this procedure? If this is not the case, we are following a wrong path here. Moreover, are we going to vote for each presumably controversial topic, for instance the syntactically

Re: How does one convert from string to integer using an arbitrary radix?

2018-10-21 Thread lscrd
The message is misleading. "result" is the one at line 325 in system.nim, i.e. the HSlice which is returned by the proc. When instanciating the slice (".."), the fields "a" and "b" of "result" (a slice) should be initialized with 0. But "b" is of type "range[8..10] and cannot be initialized wit

Re: How does one declare byte array constants?

2018-10-19 Thread lscrd
In any case, you have to indicate at least that the first value is a byte. For instance: const LOOKUP_TABLE: array[4, byte] = [1'u8, 2, 3, 4] Run or more elegant const LOOKUP_TABLE: array[4, byte] = [byte 1, 2, 3, 4] Run

Re: Some OOP problems

2018-09-24 Thread lscrd
I have already encountered this error. As far as I remember, it occurs when you use "var" in a proc whereas the type is a "ref object". If you change this in your example, the error disappears. type Animal* = ref object of RootObj age*: int proc init(x: Animal

Re: How to call a proc of Base class from a derived class ?

2018-09-18 Thread lscrd
Here is your example translated to Nim using a method for "cry". type Animal* = ref object of RootObj weightOfAnimal*: int method cry(x: Animal) {.base.} = echo "Animal is crying..." type Dog* = ref object of Animal name*: string

Re: string literals should not allocate; should allocate in ROM an extra `\0`

2018-07-20 Thread lscrd
The copy doesn’t worry me if the behavior is consistent. But, there is something I don’t understand. In this program proc p() = let x = "abc" Run there is a string copy (and an allocation) for "x". But in this one proc p() = var a = "abc"

Re: Arbitrary Precision Integer Math Operators

2018-03-22 Thread lscrd
To add to Stephan's answer, I have tried both packages when solving puzzles from "Euler project": * "bigints" is pure Nim and has the best API in my opinion, but it is about twice slower than "bignum" and has some issues (see comments in source); it is still a work in progress. * "bignum" u

Re: Wrong copy of sequences?

2018-03-20 Thread lscrd
Yes, I understand. Indeed, it seemed to me it was an issue quite difficult to solve. But, it is something very unlikely to happen in real programs and, so far, nobody has encountered these problems. I have built these examples when I have suspected that something may be wrong in some cases and t

Re: Wrong copy of sequences?

2018-03-18 Thread lscrd
Yes, I think the last example is the most annoying one as, to solve it, we have to do a copy which is just something we don't want to do for obvious performance reasons. I have tried to change the parameter type from sequence to openarray with same result. And with an array instead of a sequence

Re: Wrong copy of sequences?

2018-03-17 Thread lscrd
Yes, I know it works with **var**. I have once written a statement let b = a in a program, **a** being a sequence, with a comment # No copy. Reading this some months later, I was not sure that there is actually no copy (and I thought that, in fact, a copy is needed). So I done some checks and

Re: Wrong copy of sequences?

2018-03-17 Thread lscrd
I use 0.18.0, so the results may differ, of course. When running in the browser, I get the same results as with version 0.18.0. Maybe the compiler does some optimization but it cannot consider that _a_ and _b_ are not used in another place: they are used by _echo_. Assigning with **var** works,

Wrong copy of sequences?

2018-03-17 Thread lscrd
I would like to discuss a problem I have encountered and for which I have submitted a report on the bug tracker with a different version using _newSeqOfCap_. Here is a simple program: proc p() = var a = @[0, 1, 2] let b = a a.add(3) echo a # @[0, 1, 2, 3]

Re: Big integer litterals

2018-03-08 Thread lscrd
You have not convinced me either. I have shown that on 32 bits platform, when a variable is initialized without explicit type, the current rule will cause problems as the variable size will vary according to the initialization value. I think it's really a bigger problem that which seems a proble

Re: Big integer litterals

2018-03-07 Thread lscrd
@StasB > Not sure what you mean. Can you state what you think the general rule is? The general rule is written in the manual: "Literals without a type suffix are of the type **int** , unless the literal contains a dot or **E|e** in which case it is of type **float**. " This is the reason why I

Re: Big integer litterals

2018-03-07 Thread lscrd
I don't see why 10_000_000_000 could not be an **int** on a 64 bits platform and produce an error on a 32 bits platform. As there is always the possibility to write 10_000_000_000 'i64, this is not a restriction. Furthermore, it would make things consistent as const c = 10 * 1_000_000_000 would

Re: Big integer litterals

2018-03-06 Thread lscrd
Thank you all for your answers. Is seems that my first replies are still blocked . @Stefan_Salewski I didn't remember this paragraph, but indeed the compiler does what is written in the manual. However, in "Numerical constants" is is said that "Literals without a type suffix are of the type **

Re: Unable to reply?

2018-03-06 Thread lscrd
Seems OK, now

Re: Unable to reply?

2018-03-06 Thread lscrd
Thank you. I will wait until my messages appear in the thread.

Unable to reply?

2018-03-06 Thread lscrd
Hi. I submitted my first post some time ago without any problem, but my replies do not appear in the thread. To reply, I click on "reply", type my message, click on "preview" (to check if anything is OK) then on "submit". The window closes, but that's all. I have tried three times, first with th

Big integer litterals

2018-03-03 Thread lscrd
Hi. This is my first post on this forum, but I have used Nim for some time now (in particular, the Gtk3 bindings). Compiling one of my old programs with the new 0.18 version, I have encountered an error which can be sum up to a single statement: var x: int = 10_000_000_000 Th