Re: HttpClient networking problem

2018-12-02 Thread jlhouchin
Mark this one as Solved.

I want to thank you all for pushing me to persist and for providing me with 
some information to help solve the problems.

What helped was showing me to use the timeout. Once that happened the errors 
happened in a timely manner. And then I had to discover a few places where I 
forgot the timeouts. Now it fails and I catch it and keep going.

The one thing I don't know is what is the best way to determine that the 
current connection has closed and I need to create a newHttpClient. I see in 
the docs that the HttpClient.connected var is private.

If there is a good way to determined the connection closed status, I would 
appreciate it.

Again thanks.


Re: Advent of Code 2018 megathread

2018-12-02 Thread anonanonymous
Decided to use AoC as an opportunity to learn Nim. My solutions are in this 
repo: 
[https://github.com/anonanonymous/Advent_of_Code](https://github.com/anonanonymous/Advent_of_Code)


Re: How JSON object Syntax relates to table and array syntax?

2018-12-02 Thread mp035
Ok, After much RTFM and going through the JSON modules source code I'm going to 
answer my own question, please correct me on anything I have misunderstood:

It looks to me like the argument to the JSON constructor '%*' is not actually a 
data type in the proper sense. It's an expression which is passed to a macro, 
and broken down into NimNodes for evaluation. It has the limitation that it can 
only contain compile-time constants because evaluation of the expression is 
done by the compiler (not by the runtime).


How JSON object Syntax relates to table and array syntax?

2018-12-02 Thread mp035
Hi, I'm very new to Nim, so sorry if this is a noob question.

Firstly, I'd like to thank the Nim developers, Nim really is a fantastic 
system. I discovered it after trying Golang and finding it produced a 6MB 
binary for a simple http client application. The application is designed for an 
OpenWrt device with only 8Mb of flash! Nim on the other hand produces a very 
useable 100kb https client binary in release mode. Wonderful!

Now my question is regarding the data type used to initialise JSON objects. I 
understand that the syntax to create the object is: 


var body = %*{
  "id" : "NIMTEST12345",
  "user" : "futurepoint",
  "password" : "whatever",
  "deviceClass" : 65535,
  "pollingRate" : 5,
  "event" : 0,
  "fault" : false
}


Run

And this works fine. I understand that '%*' is a function name which 
initialises the JSON object, but what type is the argument that follows? I've 
tried:


var device_details = {
  "id" : "NIMTEST12345",
  "user" : "futurepoint",
  "password" : "whatever",
  "deviceClass" : 65535,
  "pollingRate" : 5,
  "event" : 0,
  "fault" : false
}

body = %*(device_details)


Run

but I get the error **Error: type mismatch: got ((string, int)) but expected 
'(string, string)'** at the line **" deviceClass" : 65535** which implies to me 
that tables must have values of a consistent data type.

How can I create the device_details as a separate object (to be referenced by 
other parts of the application) and convert it to json only when required?

Thanks.


Cross-compiling from Linux to Windows using the -d:mingw flag

2018-12-02 Thread lqdev
Hello,

I am trying to cross-compile to Windows using the MinGW toolchain. Here's what 
I'm doing:


$ nim c -d:mingw --cpu:amd64 src/uitest.nim

Run

However, a regular Linux executable is produced. How can I compile an exe file 
for Windows? 


Re: Deprecation of

2018-12-02 Thread moerm
Also note the conceptual difference:

`for x in someIterable: foo(x)` deals with the _elements_ of someIterable, e.g. 
with the letters of a string.

`for i in 0 ..> something.len: foo(i)` is conceptionally quite different albeit 
well known from most older languages and deals with the _indices_.

More often than not the former is what is actually desired but the latter is 
used because it's what we grew up with in C.

Also note that Nim has the very valuable option of low and high for the 
"indexed loop variant" which frees you from worrying about and using `..` vs. 
`..<`.

For example `for i in someArray.low .. someArray.high: foo(i)` safely walks 
from the first element of someArray (whatever that may be) to the last element 
or more precisely yields all indices from the first to the last one.


Re: Advent of Code 2018 megathread

2018-12-02 Thread cblake
By the way, if you are trying to "right size" a set or table to streamline 
resizing costs, you can use rightSize(number of members to support). This is 
easier than trying to guess the power of two at which such a population is 
supported for both initSet and initTable. rightSize uses the same formula (its 
inverse, really) as the internal formula used to decide when doubling capacity 
is needed.


Re: How to lookup the IPV6 addr of a domain name?

2018-12-02 Thread moerm
@satoru

Note that `getHostByName` is deprecated at least on linux.

@Libman

Nim (nativesockets) _does_ provide `getAddrInfo` but as you probably saw in 
your research it's all but worthless because it's a mess. Which btw. is hardly 
Nim's fault but rather a consequence of IPv6 being a makeshift insanity and 
mess. So I can perfectly well understand that the Nim developers basically just 
threw something very close to a blank importc at us. A proper clean Nim version 
would be quite some work due to both IPv6's insanity and plenty of OS 
implementation details. And as only a few proponents really use IPv6 while 
pretty much all servers still use IPv4 it's not exactly an attractive and 
urgent looking goal to do that work.

My personal approach - and suggestion for those who absolutely want that 
functionality - would be to create a reasonably sane (well, as sane as anything 
IPv6 related can be) implementation in C, say, one simply returning a list of 
objects holding IPs, maybe with an extra field indicating IPv4 or IPv6, and to 
then create a Nim binding returning a sequence of those simple objects for 
that. 


Re: Deprecation of

2018-12-02 Thread Stefan_Salewski
It is now


for u in i ..< words.len:


Run

So no space between .. and <. The old notation with space could give wrong 
results in rare cases due to operator priority.


Deprecation of

2018-12-02 Thread herman
Apologies if this has been asked before, I am new here.

I started learning Nim yesterday, and read through the 
[tutorial|[https://nim-lang.org/docs/tut1.html]](https://nim-lang.org/docs/tut1.html\]),
 where it says:

> > Zero-indexed counting have two shortcuts ..< and ..^ to simplify counting 
> > to one less than the higher index:

But compiling the following program with <, I get a compiler deprecation 
warnings:


import tables
import strutils

proc diff_by_one(w: string, w2: string): string =
  var d: int = 0
  var pos: int = 0
  for i, ch in w.pairs:
if ch != w2[i]:
  d += 1
  pos = i
  if d != 1:
return ""
  let a = w[0 .. 

Re: CountTable.inc() causes Error: unhandled exception: index out of bounds [IndexError]

2018-12-02 Thread mratsim
Global example code can be done before the type definitions like here: 
[https://github.com/nim-lang/Nim/blob/72e15ff739cc73fbf6e3090756d3f9cb3d5af2fa/lib/pure/collections/tables.nim#L100](https://github.com/nim-lang/Nim/blob/72e15ff739cc73fbf6e3090756d3f9cb3d5af2fa/lib/pure/collections/tables.nim#L100)

Procs and methods are already below there type definitions, see here: 
[https://github.com/nim-lang/Nim/blob/72e15ff739cc73fbf6e3090756d3f9cb3d5af2fa/lib/pure/collections/tables.nim#L872](https://github.com/nim-lang/Nim/blob/72e15ff739cc73fbf6e3090756d3f9cb3d5af2fa/lib/pure/collections/tables.nim#L872)

Regarding your code you should init the countTable once and clear it in a loop. 
Otherwise you will put a lot of pressure on the GC and the memory allocator and 
memory allocation in a loop is very slow.


var cnts = CountTable[char]()
for line in f.lines:
  if line.len == 0: break
  cnts.clear()
  for c in line:
cnts.inc(c)


Run


Problem of running proc inside proc

2018-12-02 Thread geohuz
I have the following code:


proc dynaChange(): proc =
var rules: seq[string]
for rule in rules:
echo(rule)

return proc(rule: string) =
rules.add(rule)

when isMainModule:
let addRule = dynaChange()
addRule("first")
addRule("second")
discard dynaChange()


Run

The last line: discard dynaChnage() doesn't display the output in the for 
loops. I can't figure out the problem. Pls help.


Re: Advent of Code 2018 megathread

2018-12-02 Thread SolitudeSF
if i don't end up procrastinating like previous years, my solutions will be 
here 
[https://github.com/SolitudeSF/adventOfCode](https://github.com/SolitudeSF/adventOfCode)


Re: CountTable.inc() causes Error: unhandled exception: index out of bounds [IndexError]

2018-12-02 Thread jimbo1qaz
I assume my comment on Inc() is fairly noncontroversial.

Will moving the example code to the type definitions be accepted? Will moving 
the procs/methods below their corresponding type definitions be accepted?


Re: CountTable.inc() causes Error: unhandled exception: index out of bounds [IndexError]

2018-12-02 Thread miran
> Constructors don't even have a consistent language convention. See `newSeq` 
> vs `initCountTable`

See the first two lines of the big table here: 
[https://nim-lang.org/docs/nep1.html](https://nim-lang.org/docs/nep1.html)

  * `init` is used to create a **value** type `T`
  * `new` is used to create a **reference** type `P`



As for other points you raise: Nim documentation really needs lots of 
improvements. Since you already burned yourself with the poor Tables 
documentation, how about creating a PR which will fix (some of) these issues 
you mention?


Re: CountTable.inc() causes Error: unhandled exception: index out of bounds [IndexError]

2018-12-02 Thread jimbo1qaz
Thanks for the help, I'll fix my program if I get back to it.

I feel this issue is easy to hit, and near-impossible for me to resolve myself 
by looking at the docs, without forum help.

  * CountTable doesn't have a default constructor, making it trivial to 
initialize an object to an invalid state.
  * The exception is a generic IndexError and has no indication to me about 
what mistake I made. It looks like a "CountTable.inc() doesn't support inc()" 
error, not a "CountTable is null" error or a "must use initCountTable" error.
* Constructors don't even have a consistent language convention. See 
`newSeq` vs `initCountTable`.



I looked at the docs:

  * Maybe 
[https://nim-lang.github.io/Nim/tables.html#inc%2CCountTable%5BA%5D%2CA%2Cint](https://nim-lang.github.io/Nim/tables.html#inc%2CCountTable%5BA%5D%2CA%2Cint)
 should say "key" does not need to be present, and is automatically created by 
inc()?
  * Maybe the "example code" belongs at 
[https://nim-lang.github.io/Nim/tables.html#CountTable](https://nim-lang.github.io/Nim/tables.html#CountTable)
 which is accessible via #fragment and the table of contents.
  * Maybe it would be better if each type had its own heading, examples of 
usage, followed by a list of functions operating on that type? (Like other 
languages' class+method documentation?)
  * The current location of "example code" is in the middle of several pages of 
free-form prose and code blocks, and has no heading indicating it's a "usage 
example" for CountTable.