Nim Days Progress

2020-03-05 Thread aredirect
I added two more chapters on wit.ai client and creating a CacheTable. I hope 
you guys find it useful :) Now Nim days reached 20 chapters in total. thank you 
everyone for your kind responses and valuable input 


Re: Documenting one liner

2020-03-05 Thread dawkot

proc myProc = ##[my comment]## discard

myProc()


Run

.


Re: How can I declare a constant ptr function argument?

2020-03-05 Thread mratsim
Use a distinct ptr UncheckedArray[T] and overload `[]` like I do here: 
[https://github.com/numforge/laser/blob/d1e6ae6106564bfb350d4e566261df97dbb578b3/laser/tensor/datatypes.nim#L104-L105](https://github.com/numforge/laser/blob/d1e6ae6106564bfb350d4e566261df97dbb578b3/laser/tensor/datatypes.nim#L104-L105)


Why does `setCommand` exist?

2020-03-05 Thread dawkot
As far as I can tell 


setCommand "js"


Run

is equivalent to 


switch "d", "js"


Run

Is it?


Re: Documenting one liner

2020-03-05 Thread mantielero
@treeform, it seems that you are right. I had to split it to get it working. 


Re: Documenting one liner

2020-03-05 Thread treeform
Well its not 1 line any more? I would just make it 3 lines :) 


proc createMap(this: VS):ptr VSMap =
  ## Creates a new property map. It must be deallocated later with 
freeMap().
  this.vsapi.createMap()


Run


Re: Table with generic type

2020-03-05 Thread kerry
That works and without any complaints from my editor (vs code). Thank you! This 
gets me through the roadblock that I am currently facing.

I am a little dissatisfied with this solution however but it may be due to a 
lack of understanding of hashing or static typing.

I am wanting to create a library with lots of procedures for the Node object 
which a user can easilyy associate with any data type. Ideally, I would like 
for a user to only have to define their data type and then be able to use any 
procedure that I have defined for the Node object, some of which store nodes in 
a table.

Needing to create procs for hashing each data object is overhead that I had not 
anticipated and I expect that potential users (biologists like myself) will be 
unfamiliar with the concept as I was before now.

Maybe the solution is to use Templates or Macros to create those procs? I still 
haven't delved into that part of the Nim language yet.

Or perhaps there is a more flexible way to permit hashing with a generic type?


Re: Documenting one liner

2020-03-05 Thread juancarlospaco

proc createMap(this:VS):ptr VSMap = this.vsapi.createMap()  ## \
  ## Creates a new property map. It must be deallocated later with 
freeMap().

Run

:) 


Documenting one liner

2020-03-05 Thread mantielero
What is the right way to document a one liner proc?

I tried:


proc createMap(this:VS):ptr VSMap = this.vsapi.createMap()
   ## Creates a new property map. It must be deallocated later with 
freeMap(). 

Run


Re: proc header: 'x' is of type which cannot be captured as it would violate memory safety

2020-03-05 Thread cdome
There is RFC that should allow to capture lent T/var T safely in future. But 
for now you need to use other types like ref T or ptr T


Re: FFI questions - VapourSynth

2020-03-05 Thread mantielero
Good progress. It looks like the wrapper works fine now:


$ ./ex01
ptr 0x557ede9361a8 --> [versionString = 0x7f0e3ea18860"VapourSynth Video 
Processing Library\10"
"Copyright (c) 2012-2019 Fredrik Mellbin\10"
"Core R48\10"
"API R3.6\10"
"Options: -\10"
"",
core = 48,
api = 196614,
numThreads = 8,
maxFramebufferSize = 4294967296,
usedFramebufferSize = 0]

Still some work to do. 


Re: Exporting the tostring procedure along with a type

2020-03-05 Thread abhijat0
Thanks, exporting seems to be a good way to do this, did not know about that.


Re: Arraymancer and --gc:arc

2020-03-05 Thread Clonk
That's seriously awesome, thanks everyone !

@mratsim What I meant was the equivalent of Numpy's interpn / 
RegularGridInterpolator and interp3 in Matlab / Octave with typically Linear / 
Nearest Neighbour / Cubic methods. 


Re: How can I declare a constant ptr function argument?

2020-03-05 Thread trtt
Overriding = might be enough but I get this: Error: signature for '=' must be 
proc[T: object](x: var T; y: T) for proc `=`*(a: var Xp, b: Xp {.error: 
"Copying is not allowed!".}


Re: How can I declare a constant ptr function argument?

2020-03-05 Thread trtt
But if I have something like Xp = distinct ptr X then how can I access the 
fields of X through an Xp?


Re: proc header: 'x' is of type which cannot be captured as it would violate memory safety

2020-03-05 Thread trtt
I can't, I just tried to mark the variable as var.


Re: proc header: 'x' is of type which cannot be captured as it would violate memory safety

2020-03-05 Thread Hlaaftana
Can you give code please?


Re: Arraymancer and --gc:arc

2020-03-05 Thread mratsim
Interpolation?

For FFT I was sure there was a package but maybe I misremembered.

Edit: there was one 
[https://github.com/mratsim/Arraymancer/issues/394#issue-512574030](https://github.com/mratsim/Arraymancer/issues/394#issue-512574030),
 forgot to link the forum post though :P


Re: Exporting the tostring procedure along with a type

2020-03-05 Thread Hlaaftana
I think "tying procedures to objects" is a planned compiler behaviour for the 
future, but I think the accepted behaviour currently is doing `import story` in 
this case. If you come across this situation where you specifically need 
`utils` but not `story` more than once, you can do this:


# utils.nim
import story
export story.`$`

proc parseStory*(filename: string): Story =
  ...


Run


Re: How can I declare a constant ptr function argument?

2020-03-05 Thread mratsim
Use a distinct type


type MyReadOnlyPtr[T] = distinct ptr T

proc `==`(a: var MyReadOnlyPtr, b: MyReadOnlyPtr){.error: "Copying is 
verboten".}


Run

For deep immutability, you can get some inspiration from what I describe here 
in the deep immutability section: 
[https://github.com/mratsim/Arraymancer/pull/420](https://github.com/mratsim/Arraymancer/pull/420)


Re: Arraymancer and --gc:arc

2020-03-05 Thread Vindaar
As far as I'm aware these two things aren't possible with Arraymancer at the 
moment. But using normal a seq[T] there's ways to do both:

For interpolation:

  * numericalnim by @hugogranstrom: 
[https://github.com/HugoGranstrom/numericalnim#natural-cubic-splines](https://github.com/HugoGranstrom/numericalnim#natural-cubic-splines)
  * to an extent seqmath by @jlp765 (and me to an extent, but not on 
interpolation): 
[https://github.com/Vindaar/seqmath/blob/master/src/seqmath/smath.nim#L639](https://github.com/Vindaar/seqmath/blob/master/src/seqmath/smath.nim#L639)
 and below



And for FFT the only way I'm aware of at the moment is via the C library kiss 
FFT: 
[https://github.com/m13253/nim-kissfft](https://github.com/m13253/nim-kissfft)

I wrote down a couple of notes (mainly for myself and @kaushalmodi) about using 
kiss FFT from Nim a couple of days ago: 
[https://gist.github.com/Vindaar/fc158afbc75627260aed90264398e473](https://gist.github.com/Vindaar/fc158afbc75627260aed90264398e473)


Exporting the tostring procedure along with a type

2020-03-05 Thread abhijat0
I have a program with the following components:

  * a main.nim file, a utils.nim file and story.nim
  * story.nim defines a type Story and also its tostring procedure. A proc in 
utils returns the story object. main calls the proc to get the story and prints 
it.
  * I need to import story in utils but not in main, as due to type inference I 
do not need to actually specify story type anywhere in main.



If I echo my story type in main, the tostring used is the default one for a 
type, and not the one I defined.

If I import story in main, then my tostring is used.

main.nim 


import utils

let story = parseStory(...)
echo(story) # this prints the default object string. if I import story in 
this file, then the custom tostring is used.


Run

utils.nim 


import story

proc parseStory*(filename: string): Story =
  ...


Run

story.nim 


type
  Story* = object
...

proc `$`*(s: Story): string =
  


Run

Is there a way to print story with the custom representation I defined without 
importing it in main, by binding it with the type somehow?


Re: Arraymancer and --gc:arc

2020-03-05 Thread xflywind
I write some naive fft function use Arraymancer.But I don't have time to 
improve.(i'm writting web framework now)

[https://github.com/xflywind/scim/blob/master/src/scim/fft.nim](https://github.com/xflywind/scim/blob/master/src/scim/fft.nim)


Re: HELP: async httpclient running out of response

2020-03-05 Thread enthus1ast
Code looks good to me.

you can drive your async loop with:


runForever()


Run

which boils down to:


while true:
  poll()


Run

or you can


waitFor() # drives an async loop and also returns value.


Run

Then inside an async proc:


await # blocks and returns a value


Run

or


asyncCheck # does not block but does not return a value.


Run

So in your case,

you could loop over futures and call await on your future.

Or you could loop over futures and call


asyncCheck


Run

then in another loop, check if all futures are finished.


Re: How can I declare a constant ptr function argument?

2020-03-05 Thread trtt
What I want is to "protect" the variable - I want a read-only resource without 
reference counting and without copying.


Re: Arraymancer and --gc:arc

2020-03-05 Thread Clonk
Good to hear !

Also, do you support interpolation and FFT on Tensor in Arraymancer ?


Re: How can I declare a constant ptr function argument?

2020-03-05 Thread mratsim
addresses are always relative dynamic context (the binary reserved memory, the 
system allocator state), so no it is not. Unless you meant something like C 
`void my_proc(const * const int){...}`?


Re: Arraymancer and --gc:arc

2020-03-05 Thread mratsim
Reported there: 
[https://github.com/mratsim/Arraymancer/issues/423](https://github.com/mratsim/Arraymancer/issues/423)
 thanks.

I'm currently significantly changing the Arraymancer internals (backed by raw 
memory buffers instead of sequences) so this might disappear by itself.


Re: parallel: bounds checker

2020-03-05 Thread daef
for completeness sake, a working version of the first example (helping the 
proof engine a bit)

still gonna give weave a try I guess


import strutils
import threadpool
{.experimental: "parallel".}

proc main =
  let
w = 16
h = 8
  var s = "a".repeat(w*h)
  parallel:
## not yet
#for y in 0..

How can I declare a constant ptr function argument?

2020-03-05 Thread trtt
Is it possible?


Re: Arraymancer and --gc:arc

2020-03-05 Thread juancarlospaco
Report the Bug maybe. 


Re: HELP: async httpclient running out of response

2020-03-05 Thread slime496
@enthus1ast I have changed my code to this, and it seems not be stucked 
anymore.(Need more test after.) 


proc checkUrlAsyncWithTimeOut*(url: string, saveName: string,
proxy: Proxy, timeOut:int=3000): Future[UrlCheckResult] {.async.} =
var client = newAsyncHttpClient(proxy = proxy, maxRedirects = 1)
let future = client.get(url)
let onTimeGet = await future.withTimeout(timeOut)
if not onTimeGet:
return UrlCheckResult(url: url, error: "Async timeOut [GET]")
else:
let bodyFuture = future.read().body()
let onTimeBody = await bodyFuture.withTimeout(timeOut)
if not onTimeBody:
return UrlCheckResult(url: url, error: "Async timeOut [BODY]")
else:
let body = await bodyFuture
return UrlCheckResult(url: url, saveName: saveName,
data: body)
client.close()


Run

But since I add the line "client.close()" in the end of the proc, it will get 
some error like below:

> Exception message: Connection was closed before full request has been made
> 
> Exception type: [ProtocolError]

\---

btw I am wonderring whether the "all" proc is the only way to begin a async 
event. Is there any other way to write async code in Nim.


Arraymancer and --gc:arc

2020-03-05 Thread Clonk
Hello,

I am experimenting with arraymancer and when compiling with nimble install 
--gc:arc while using arraymancer I encounters weird behaviour such as :

  * code blocking on Matrix by scalar :code:* operation
  * SIGSEGV: Illegal storage access. (Attempt to read from nil?) when doing 
Matrix by Matrix :code:*. operation



I am using the devel nim :


Nim Compiler Version 1.1.1 [Linux: amd64]
Compiled at 2020-03-05
Copyright (c) 2006-2019 by Andreas Rumpf

I installed arraymancer with nimble install arraymancer@#head

For reference here is the code that reproduce :


import arraymancer

proc doubleMe(data : Tensor[float]): Tensor[float]=
  result = data*2

proc squareMe(data : Tensor[float]): Tensor[float]=
  result = data*.data

let A : seq[float] = @[0.11, 0.12, 0.13, 0.14]
echo A.type
echo A
var B : Tensor[float] = A.toTensor
echo B
# Blocking
var C = doubleMe(B)
echo C
#Segfault
var D = squareMe(C)
echo D


Run

If I remove the \--gc:arc flag it behaves normally.

Is this "normal" behaviour (meaning arraymancer does not support arc) or am I 
miss something obvious ? 


Re: parallel: bounds checker

2020-03-05 Thread daef
damn you globals! I feel like I should have smelled that...

thanks!


Re: Why not introduce the concept of Decimal in nim ???

2020-03-05 Thread JPLRouge
I worked a lot IBM38 / AS400 / OS400 over 40 years RPG / ILE / C I think I'm 
too old (and my health requires it) I worked for a Nim port in github in the 
AS400 (DDS) way, I hesitated between starting from the source code "C" Mike 
Cowlishaw / IBM, finally for the moment I relied on the "mpdecimal" process and 
[https://github.com/status-im/nim-decimal](https://github.com/status-im/nim-decimal),
 reworked

But the ideal would be to resume that of "Mike Cowlishaw / IBM" opensource of 
GNU to date it is not something that moves, on the other hand the repercussions 
in NIM for example for JSON and all that is related to the type of var ...

The interest is in the drift calculations due to very large numbers, impalpable 
but how significant ... As well as compatibility with professional SQL

Sorry for my english (shabby)


Re: parallel: bounds checker

2020-03-05 Thread Araq
Fwiw this works fine :P


import threadpool
{.experimental: "parallel".}

proc main =
  var s = newSeq[char](3)
  parallel:
for i in 0..s.high:
  s[i] = spawn chr(i)
  
  echo s

main()


Run


Re: proc header: 'x' is of type which cannot be captured as it would violate memory safety

2020-03-05 Thread trtt
But I'm using --gc:arc to avoid unnecessary overhead.


Re: parallel: bounds checker

2020-03-05 Thread daef
thanks, I'll give weave a try :)


Re: proc header: 'x' is of type which cannot be captured as it would violate memory safety

2020-03-05 Thread enthus1ast
i don't know much about arc but this is a common error on async. I would try to 
change the type to a "ref T"


proc header: 'x' is of type which cannot be captured as it would violate memory safety

2020-03-05 Thread trtt
I'm trying to avoid the copy of an object but the compiler doesn't let me. I'm 
using --gc:arc on #devel.

...nim-#devel/lib/core/macros.nim(557, 1) Error: 'x' is of type  which 
cannot be captured as it would violate memory safety, declared here: ...

lent only works with return types and var wouldn't be good anyway because I 
need it to be a constant.


Re: HELP: async httpclient running out of response

2020-03-05 Thread slime496
Thank you. I will have a try along with your suggestion once arriving my house. 
And I will give some feedbacks whatever it is succeeded or failed.


Re: HELP: async httpclient running out of response

2020-03-05 Thread enthus1ast
You should close the HttClient after each request to avoid running out of file 
descriptors. Then try to make the requests with 'withTimeout' from the 
asyncdispatch module. This way you can react when a request timesout. If you 
still having troubles let me know. Later I can help you better (i'm on mobile 
atm)