Re: Stream IO errors with -d:release flag

2020-05-28 Thread seves
Thank you for your answers. I appreciate that very much. Its a bit funny but i 
don't use c in this context.

The problem I have is that it didn't work with the flag -d: release. I have now 
tried something out and found out the following:

When I create the stream from the previously opened file, these errors occur in 
the release build, but when I create a new stream from filepath, the program 
works fine

broken: 


stream = newFileStream(FILE)
...
stream.setPosition(blabla)


Run

working solutiuon: 


stream = newFileStream(filename)
...
stream.setPosition(blabla)


Run

I'm not sure, maybe I still have a worm somewhere, but when I open the stream 
from the path it seems to work. It is still strange that it does not work with 
the stream from the FILE


Re: VSCode debug: strings and booleans are ugly

2020-05-28 Thread Yardanico
Sometimes you really want an interactive debugger though


Re: Faster and Safer Raytracing in Nim

2020-05-28 Thread mratsim
> How about to use counter based random number generater like philox? 
> [https://www.thesalmons.org/john/random123](https://www.thesalmons.org/john/random123)/

This paper is mentioned in JAX (another Google Deep Learning framework), which 
was mentioned to me here 
[https://github.com/mratsim/weave/issues/147#issuecomment-631864783](https://github.com/mratsim/weave/issues/147#issuecomment-631864783)

[https://github.com/google/jax/blob/master/design_notes/prng.md](https://github.com/google/jax/blob/master/design_notes/prng.md)

This is what I'm doing with my `pair` function + reseed. JAX focused more on 
the splittable RNG part so i may have reinvented the counter RNG :P.

> Probability of that happening can be very small, but it looks like 
> Birthday_problem. You need to use PRNG that have much longer period than 
> number of random numbers actually used to avoid random number stream overlap.

The birthday paradox takes the square root of your probability, i.e. if you 
have 2^-256, the birthday paradox changes that to 2^-128. I use 2^256 period in 
my RNGs due to this, Weave is also using 2^256 for task scheduling.

Thank you for your suggestions, it's nice to see people interested in RNG 
quality in the community :) 


Re: Doc generation fails

2020-05-28 Thread kaushalmodi
This issue is now fixed on devel, but there's another regression with `nim doc` 
\+ `runnableExamples`: 
[https://github.com/nim-lang/Nim/issues/14485](https://github.com/nim-lang/Nim/issues/14485)
 .


Git mirror for nimble packages https://reg.nim.moe/

2020-05-28 Thread lmariscal
Hi,

I shared this on IRC a month ago, but wanted to share it here too after it has 
been running non-stop for a month.

**What is it?**

This is mostly a cron job that clones all the Git and Mercurial repositories 
and offers them through a modified package registry. What I'm trying to 
accomplish here is two things. First, create a mirror of all the repos since 
previous experience has shown me that repos can be easily taken down either by 
the authors or Github itself (Github hasn't taken any Nim related repos to the 
extend of my knowledge). Second, I don't think of myself as being proficient 
with Mercurial and don't want to mix my packages with other version control 
systems, the fix being to transform the Mercurial repos to Git repos via HgGit. 
An example of a transformed Mercurial repo can be found at 
[https://cgit.nim.moe/strfmt](https://cgit.nim.moe/strfmt) You can find the 
packages registry at 
[https://reg.nim.moe/packages.json](https://reg.nim.moe/packages.json) and an 
overview of the cloned repos served by cgit at 
[https://cgit.nim.moe/](https://cgit.nim.moe/). The packages list is updated 
every 10 minutes and all the packages are fetched and if needed pulled from 
source every 30 minutes. The url of all the packages is modified to target the 
ones at [https://git.nim.moe/](https://git.nim.moe/). (Why git.nim.moe and not 
cgit.nim.moe? Because git.nim.moe uses the git http backend while cgit.nim.moe 
uses a cgit http with similar functionality but with not enough to be fully 
used by nimble)

For a little bit of context, I'm the maintainer/creator/BDFL of 
[https://github.com/nimgl/nimgl](https://github.com/nimgl/nimgl) and one of the 
packages that inspired me was glfw by ephja (I haven't for a while used it 
since I made my own bindings) but one day it simply got deleted after the mass 
migration to Gitlab. That's what inspired me to do this thing.

**Why should I use this?**

You shouldn't. The main reason it exists is so packages don't die out after 
simply being deleted. The only realistic reason is if you find an active 
Mercurial package which you would like to use without having to interface with 
Mercurial. If a package you needed was deleted I would recommend to make a 
clone for yourself from the mirror repo instead of directly referencing it from 
nim.moe.

**I still want to use it**

Well, either if for a reason previously stated or because you don't want to 
trust Github but you do me (I don't know why this would happen). You can add it 
by following the [Nimble 
readme](https://github.com/nim-lang/nimble/blob/master/readme.markdown#user-content-configuration).
 And simply add: 


[PackageList]
name = "official"
url = "https://reg.nim.moe/packages.json;


Run

My biggest concern is that I'm a college student earning exactly $0 dollars a 
year (By no means a legal tax statement). So all I can do is a $5 droplet in 
digitalocean and the yearly expenses of the domain. Currently all the packages' 
data is sitting at a 5.4Gb (21.86% of the droplet's disk) with still room to 
grow, the other part being that if enough people use it the little baby droplet 
won't be able to support the load.

**I was using X package but it disappeared, do you have it?**

This is a project (Baby project? It's little.) that started a month ago, so all 
the projects that were deleted before this date (20/04/2020) were not able to 
be cloned. You can find a list of such projects at 
[https://reg.nim.moe/not_found.txt](https://reg.nim.moe/not_found.txt).

**Please** if you have a fork/clone/zip/png of one of this packages let me know 
either by DM or a comment here so it can be uploaded even if it isn't the 
latest version.

**I like the idea, but I trust no one. Not even me (Yourself)**

You can easily create your own instance, I don't have step by step guide on how 
to create it but all you need is a git http backend and the cron jobs located 
at [https://github.com/nim-moe/cron](https://github.com/nim-moe/cron). Also 
Mercurial and the HgGit extension to be able to push the repo from hg to git. 
Cgit is not necessary since it is only used to give an overview of what 
packages have been updated and when was the last update loaded into the system.

**The future of the project** a.k.a. **I like the domain**

I really like the domain, but this was a mini project made in a few hours 
mostly to try and ignite a conversation on the topics of Nim's package manager 
and what we as a community will do to prevent packages from being lost into the 
void. If any of you have any better ideas or want to use the domain name to 
start a project related to this issues please feel more than free to contact 
me. And I would even be happy to contribute some code.


Re: Faster and Safer Raytracing in Nim

2020-05-28 Thread demotomohiro
How about to use counter based random number generater like philox? 
[https://www.thesalmons.org/john/random123](https://www.thesalmons.org/john/random123)/

It works like a hash function. When you generate a random number, you make a 
unique number and pass it the function. You can create unique numbers using 
pixel coordinate, item index, loop counter, line number, etc.

Pros:

  * deterministic as long as input numbers are deterministic.
  * Stateless. You can use existing variables to generate random numbers and it 
can reduce memory usage per thread.
  * Each random number generations can be executed in parallel as it doesn't 
need PRNG state update. Increase instruction-level parallelism.



Cons:

  * It require more instructions to generate random number from non-random 
numbers.



> I have found a parallel RNG scheme that allows reproducible multithreaded 
> result for those who wants to do parallel reproducible Monte-Carlo 
> simulations:

Each RNGs generate different RNG streams but I think 2 RNG streams can be 
overlapped. All RNGs are seeded with unique random number in your code, but the 
state of one RNG right after being seeded can be same to the state of other RNG 
after several status update. (In that case, former RNG generate random number 
stream that is same to the one from later RNG after several state update) 
Probability of that happening can be very small, but it looks like 
[Birthday_problem](https://en.wikipedia.org/wiki/Birthday_problem). You need to 
use PRNG that have much longer period than number of random numbers actually 
used to avoid random number stream overlap. 


Re: Creating a seq or openarray on unmanaged memory

2020-05-28 Thread Araq
> But Go's slices are memory-safe and zero-copy and can be stored, for example.

They are technically memory-safe but prone to aliasing bugs anyway.


Re: Best Nim translation of C {void*, size_t} struct

2020-05-28 Thread Araq
It's supposed to work, yes and should be documented in the manual.


Re: Creating a seq or openarray on unmanaged memory

2020-05-28 Thread snej
> Only Rust has memory safe zero-copy collection that can be stored in a type 
> (their slice type).

Not exactly. Only Rust has this 
_[plus](https://forum.nim-lang.org/postActivity.xml#plus) compile-time safety 
checks and zero runtime overhead. But Go's slices are memory-safe and zero-copy 
and can be stored, for example. It accomplishes this by having the slice type 
contain a hidden reference to the object that owns the memory; thus the GC 
ensures that a slice can't outlive its backing store.

That would be pretty easy to support in Nim. I could easily make such an object 
myself, it just wouldn't work the same as an array/seq/string, as I said above. 
But thanks for the pointers [sic] to the "it" templates and zero-functional; 
I'll check those out.


Re: Best Nim translation of C {void*, size_t} struct

2020-05-28 Thread snej
Just to make sure you're answering the exact question I asked :) — if I declare 
a C proc that takes an `openarray`, when Nim calls that function does it pass 
that parameter as a pointer followed by an int? For example:


proc set_bytes(bytes: openarray[byte]) {.importc: "set_bytes".}

let data: seq[byte] = @[1, 2, 3, 4]
set_bytes(foo, data)


Run

Will this work going forward? (I could try it out myself, but that only means 
it works in Nim 1.2, not that it's expected/supported...)


Re: Is there a command line one-line to start an HTTP server using Nim?

2020-05-28 Thread bung
make it depends on libzip optional sounds nice, user may not need full features.


Re: Nim Cheatsheet PDF (English+Spanish+Latex)

2020-05-28 Thread juancarlospaco
That was the idea, use it as template for more cheatsheets or other stuff. :)


Re: Nim Cheatsheet PDF (English+Spanish+Latex)

2020-05-28 Thread snej
Nice! I made a copy that's black-and-white, better suited for laser-printing: 
[https://www.overleaf.com/read/gytjmgdzmcdt](https://www.overleaf.com/read/gytjmgdzmcdt)


Re: Nim Cheatsheet PDF (English+Spanish+Latex)

2020-05-28 Thread Araq
Iteration order that depends on runtime values is a design bug.


Re: Is there a command line one-line to start an HTTP server using Nim?

2020-05-28 Thread kaushalmodi
After 
[https://github.com/enthus1ast/nimAsyncHttpTools/pull/2](https://github.com/enthus1ast/nimAsyncHttpTools/pull/2),
 `simplehttpserver` compiles and runs well on my machine (RHEL 6.8).


Re: Is there a command line one-line to start an HTTP server using Nim?

2020-05-28 Thread kaushalmodi
@bung You do not have an Issues section on your fork. I wanted to ask you if 
the dependency on libzip can be made optional? (See [my message 
above](https://forum.nim-lang.org/t/6386#39399) ). The original `nimhttpd` 
builds and runs fine on my machine (RHEL 6.8), but trying to run your fork 
fails as I don't have the needed `libzip`.


Re: Nim Cheatsheet PDF (English+Spanish+Latex)

2020-05-28 Thread juancarlospaco
Fixed.

Maybe we need that overload (???) ;P 


Re: VSCode debug: strings and booleans are ugly

2020-05-28 Thread juancarlospaco
`assercho` is `assert` and `echo` combined, with extra Debug, it shows the Nim 
code line and the C code line, works with bool and string.

[https://github.com/juancarlospaco/nim-contra](https://github.com/juancarlospaco/nim-contra)


Re: Is there a command line one-line to start an HTTP server using Nim?

2020-05-28 Thread kaushalmodi
Awesome! That is the winner!

@Araq It would be now cool to bake that into nim :)


Re: Is there a command line one-line to start an HTTP server using Nim?

2020-05-28 Thread kaushalmodi
OK, it builds find after uninstalling `zip` and doing `nimble install 
zip@#head`.

So the correct fix is:

  1. Cut a new release of the `zip` nimble package
  2. Cut a new release of your `finder` package with updated zip version in its 
dependency
  3. Finally update your `static_server` package with updated finder version in 
its dependency




Re: Is there a command line one-line to start an HTTP server using Nim?

2020-05-28 Thread kaushalmodi
ok. .now it compiles, but doesn't run:

> could not load: libzip(|2).so(|.4|.2|.1|.0)

I'll have to figure this out later. I am using an admin managed OS, a very old 
OS: RHEL 6.8 (Linux).


Re: Is there a command line one-line to start an HTTP server using Nim?

2020-05-28 Thread kaushalmodi
I tried your repo as well. I cloned it and did `nimble build`, but it failed 
with this: [http://ix.io/2nAa/text](http://ix.io/2nAa/text)

It looks like the `psutil` dependency needs to be updated for a backward 
incompatible change that happened in nim 1.0.0. Now we have `isDigit` taking 
only `char` arg, not `string`.

I'll open a PR on `psutil` to fix this.


Re: Is there a command line one-line to start an HTTP server using Nim?

2020-05-28 Thread jackhftang
In case you just need the original nimhttpd like `python3 -m http.server`, run 
`nimble install nimhttpd` to install the binary and then run `nimhttpd` every 
time you want to serve static files. 


Re: Is there a command line one-line to start an HTTP server using Nim?

2020-05-28 Thread kaushalmodi
The aim of my post was if there's a way that Just Works for kicking off an 
adhoc server, like `python3 -m http.server` does, **and** then have that baked 
into nim as well.


Re: Is there a command line one-line to start an HTTP server using Nim?

2020-05-28 Thread kaushalmodi
Hello, I cloned your repo and did `nimble build`. It failed with this error: 
[http://ix.io/2nA6/text](http://ix.io/2nA6/text)

I am using the latest nim devel.


Re: Change Nim colour on GitHub

2020-05-28 Thread JPLRouge
yellow is difficult to read on a white background


Re: How to bypass a runtime error ?

2020-05-28 Thread fredg
I have tried it, in many ways, with no luck :(

Thanks for trying.

++


Re: incorrect set-to-int conversion

2020-05-28 Thread Araq
> Is this a bug, or intentional? If the latter, is there a cleaner workaround 
> like a pragma?

It's intentional but it's not part of the spec and we could change it.

The workaround is to add an enum field that is mapped to 0. 


Re: VSCode debug: strings and booleans are ugly

2020-05-28 Thread aviator
Oh, bummer. LLDB supports a Python API for pretty printing as well, but I don't 
think anyone has made a script for Nim yet.


Re: How to bypass a runtime error ?

2020-05-28 Thread mratsim
`try` / `except` or `try`/`finally`

[https://nim-lang.org/docs/tut2.html#exceptions-try-statement](https://nim-lang.org/docs/tut2.html#exceptions-try-statement)


from strutils import parseInt

# read the first two lines of a text file that should contain numbers
# and tries to add them
var
  f: File
if open(f, "numbers.txt"):
  try:
let a = readLine(f)
let b = readLine(f)
echo "sum: ", parseInt(a) + parseInt(b)
  except OverflowError:
echo "overflow!"
  except ValueError:
echo "could not convert string to integer"
  except IOError:
echo "IO error!"
  except:
echo "Unknown exception!"
# reraise the unknown exception:
raise
  finally:
close(f)


Run


Re: Stream IO errors with -d:release flag

2020-05-28 Thread mratsim
@Araq, how do you know it's a callback?

When you create a proc to be used in C (callback):

  * Don't use exceptions
  * Don't use string or sequences
  * Deactivate stacktraces
  * Use `int32` or `cint` for `int` use `uint` or `csize_t` for `size_t`
  * tag `{.cdecl, gcsafe.}` your proc to detect at compile-time the issue



Also one thing I've noticed is that Nim `endOfFile()` is quitting to early 
compared to `c_eof` from the `system/ansi_c` module (see my h264 encoder test 
case 
[https://github.com/mratsim/trace-of-radiance/blob/99f7d85d/trace_of_radiance/io/h264.nim#L278](https://github.com/mratsim/trace-of-radiance/blob/99f7d85d/trace_of_radiance/io/h264.nim#L278))

Example to pass a Nim callback to C code for MP4 muxing:

[https://github.com/mratsim/trace-of-radiance/blob/99f7d85d/trace_of_radiance/io/mp4.nim#L115-L126](https://github.com/mratsim/trace-of-radiance/blob/99f7d85d/trace_of_radiance/io/mp4.nim#L115-L126)



proc MP4E_open(
   sequential_mod_flag: cint,
   enable_fragmentation: cint,
   token: pointer,
   write_callback: proc(
 offset: int64,
 buffer: pointer,
 size: csize_t,
 token: pointer
   ): cint{.cdecl, gcsafe.}
 ): ptr MP4E_mux_t {.importc, header: "minimp4.h".}
  ## The C code


{.push stackTrace: off.}
proc writeToFile(
   offset: int64,
   buffer: pointer,
   size: csize_t,
   token: pointer
 ): cint {.cdecl, gcsafe.} =
  ## The callback
  let file = cast[File](token)
  file.setFilePos(offset)
  let bytesWritten = file.writeBuffer(buffer, size)
  return cint(bytesWritten.csize_t != size)
{.pop.}


Run


Re: Stream IO errors with -d:release flag

2020-05-28 Thread Araq
It's likely that your code is buggy. Use `setupForeignThreadGC()` in callbacks 
that are called from C and run in a different thread.


Re: Change Nim colour on GitHub

2020-05-28 Thread Araq
Fwiw orange is my favorite color.


How to bypass a runtime error ?

2020-05-28 Thread fredg
Hello,

I am trying to build a list of mirrors and append the date when the mirror has 
been refreshed. (For doing that I take the last-modified header of a file)

The program built without errors. The program begin to parse and print but, it 
ends if it meet an error during the parse.

Is there a way to "bypass" the crap mirror and go to the next one ?

Here is the error output : 


https://mirror.labkom.id/pub/OpenBSD
2020-05-27 15:45
2020-05-27 20:08
...
...
https://mirrors.nav.ro/pub/OpenBSD
2020-05-27 15:45
2020-05-26 20:01
/home/f/code/mirrosnap/mirrosnap.nim(55) mirrosnap
/home/f/code/mirrosnap/mirrosnap.nim(44) makeDalist
/usr/local/lib/nim/pure/httpclient.nim(254) lastModified
/usr/local/lib/nim/pure/times.nim(2436) parse
/usr/local/lib/nim/pure/times.nim(2412) parse
/usr/local/lib/nim/pure/times.nim(1827) raiseParseException
Error: unhandled exception: Failed to parse '' with format 'ddd, dd MMM 
 HH:mm:ss 'GMT''. Parsing ended but there was still patterns remaining 
[TimeParseError]


Run

Here is the code : 


import httpClient
import strutils
import re
import tables
#import osproc
import times

var
  client = newHttpClient(timeout = 2)
  mirloc = initTable[string, string]() # mirrors table
  basemod = initTable[string, string]() # table : url + base freshness
  pkgmod = initTable[string, string]() # table : url + packages freshness
  kbase: string
  kpkg: string
  basem: Response
  pkgm: Response

let
  httpslist: string = 
"https://cvsweb.openbsd.org/cgi-bin/cvsweb/~checkout~/www/httpslist;
  #(myArchnl, errarch) = execCmdEx("uname -m")
  #myArch = myArchnl.replace(re"\n")

proc mirrorsDico(link: string) =
  let
contents = client.getContent(link)
mirrors = contents.splitLines()
  for m in mirrors:
if m =~ re"(^http.*(BSD|bsd))(\s{2,})(\w.*$)":
  # mirror url --- location
  mirloc[matches[0]] = matches[3]

mirrorsDico(httpslist)

proc makeDalist(arch: string) =
  let
base = "/snapshots/" & $arch & "/SHA256"
pkg = "/snapshots/packages/" & $arch & "/SHA256"
  for k, v in mirloc:
kbase = k & base
kpkg = k & pkg
basem = client.get(kbase)
pkgm = client.get(kpkg)
let
  bma = basem.lastModified()
  pma = pkgm.lastModified()
  bm = bma.format("-MM-dd HH:mm")
  pm = pma.format("-MM-dd HH:mm")
#basemod[k] = $bm
#pkgmod[k] = $pm
# TODO find a way to handle error and bypass it
echo k
echo bm
echo pm

makeDalist("amd64")

#for mir, bf in basemod:
#  echo(mir, " | ", bf)


Run

Don't hesitate to criticize, this is my first try with nim ;)

Regards.

fredg.


Re: VSCode debug: strings and booleans are ugly

2020-05-28 Thread Kosteg
Unfortunately this won't work - GDB doesn't work on Mac since 10.14 Mojave


Re: Nim Cheatsheet PDF (English+Spanish+Latex)

2020-05-28 Thread SolitudeSF
thats not correct 


for i in 9..4: echo i # 9, 8, 7, 6, 5, 4


Run


Re: Is there a command line one-line to start an HTTP server using Nim?

2020-05-28 Thread bung
[https://github.com/bung87/nimhttpd](https://github.com/bung87/nimhttpd) you 
can try my fork version httpd support serve file through file system, memory 
zip, file zip. 


Re: Is there a command line one-line to start an HTTP server using Nim?

2020-05-28 Thread enthus1ast
i've build one, but it needs some love (especially the part where ip addresses 
are queried from the os) 
[https://github.com/enthus1ast/nimAsyncHttpTools/blob/master/src/simplehttpserver.nim](https://github.com/enthus1ast/nimAsyncHttpTools/blob/master/src/simplehttpserver.nim)