Re: What's the most idiomatic way to call ``execCmdEx`` in parallel?

2019-07-12 Thread OldhamMade
Thanks, I did take a look at that, but from the docs it looks like 
`execProcesses` doesn't allow me to get the return value of all executions. Or 
am I misinterpreting that?


Re: D templated codeblocks

2019-07-12 Thread bpr
I'm referring to the ability of D templates beyond the ability to write 
function or struct class templates, but templates over blocks that include new 
types and variables, almost like the parameterized module system of OCaml. 
Forgive me for writing D here, I'll translate it to Future Nim soon ;-)

As with C++ D has function templates like so


void copy(T)(out T to, T from) {
to = from;
}

T sum(T)(T lhs, T rhs) {
return lhs + rhs;
}


Run

and struct/class templates like


struct Node(T) {
T v;
Node* left;
Node* right;
}


Run

but D has a general code block template, in which you can declare variables and 
new types, like


template MyTemplate(T) {
T val;

void copy(out T to, T from) {
to = from;
}

struct Node {
T v;
Node* left;
Node* right;
}
}


Run

In D the function and struct templates are special cases of the more general 
code block syntax, but that's not important. What's important to me is that it 
is like a parameterized module, so I can declare variables in there.


Re: Windows 10 fails on the test code

2019-07-12 Thread Araq
Yeah, we know, but we haven't yet got a PR to fix this. (Hint, hint!)


Re: D templated codeblocks

2019-07-12 Thread Araq
To be honest, I'm not sure what you mean? Can you elaborate? Do you refer to 
[https://dlang.org/spec/template.html#aliasparameters](https://dlang.org/spec/template.html#aliasparameters)
 ? 


Weird behaviour with generic type

2019-07-12 Thread vitreo12
Hello everyone! I am a new Nim user (and, I have to say, I am pretty excited 
about this language!) coming from Julia. I was tesing some of Nim's 
capabilities when coming to generic types and procs, when I ran into this code 
not compiling:


type
AbstractType = int or string
TestType*[T : AbstractType, Y : AbstractType] = object
a : T
b : Y

proc newTestType[T : AbstractType, Y : AbstractType](a : T, b : Y) : 
TestType[T, Y] =
return TestType[T, Y](a : a, b : b)

proc print_test_type(t : TestType) =
echo t.a
echo t.b

let
t1 = newTestType(1, 2)
t2 = newTestType("string", "here")
t3 = newTestType(10, "mixed")

echo typedesc(t1)
print_test_type(t1)

echo typedesc(t2)
print_test_type(t2)

echo typedesc(t3)
print_test_type(t3)


Run

The compiler error is this:


/home/francesco/Sources/nim/GenericTypes.nim(44, 21) template/generic 
instantiation of `newTestType` from here
/home/francesco/Sources/nim/GenericTypes.nim(35, 20) Error: cannot 
instantiate TestType
got: 
but expected: 


Run

On the other hand, if I substitute 


TestType*[T : AbstractType, Y : AbstractType]

Run

with 


TestType*[T : AbstractType, Y : int or string]

Run

, the code correctly compiles and executes, resulting in the expected result:


[system.int, system.int]
1
2
TestType[system.string, system.string]
string
here
TestType[system.int, system.string]
10
mixed


Run

Does anyone know why this is the case? 


Re: Macro to create a dictionary (table) like in python!

2019-07-12 Thread zevv
Github pull request: 
[https://github.com/nim-lang/Nim/pull/11702](https://github.com/nim-lang/Nim/pull/11702).
 


Re: Macro to create a dictionary (table) like in python!

2019-07-12 Thread cblake
These association lists, often abbreviated "alist", get a lot of play in lisp, 
but also OCaml, Haskell, etc.. 
[https://en.wikipedia.org/wiki/Association_list](https://en.wikipedia.org/wiki/Association_list)


Re: What's the most idiomatic way to call ``execCmdEx`` in parallel?

2019-07-12 Thread rayman22201
@dawkot, unfortunately your implementation has a big performance flaw.

the sleepAsync 5 creates a minimum bound of 5 ms on the execution time + the 
time it takes to run through the event loop. It's the naive solution to the 
problem.

The more correct way to accomplish this with async is to register the file 
handle for stdout of the forked process with the asyncdispatch epoll handler.

There really should be a 'asyncExecProcess` proc in the std lib. Unfortunately 
this does not exist in the std lib at the moment.

There is an excellent third party library that does have a good implementation 
of this. @cheatface's aysnc tools: 
[https://github.com/cheatfate/asynctools/blob/master/asynctools/asyncproc.nim#L122](https://github.com/cheatfate/asynctools/blob/master/asynctools/asyncproc.nim#L122)

For @OldhamMade's usecase, I'm not sure all the extra machinery of async is 
necessary. The difference between Threads vs. Async does not make much of a 
difference for this use case, and the spawn solution is much less code to get 
something that _just works(tm)_.


Re: Macro to create a dictionary (table) like in python!

2019-07-12 Thread miran
> What prevents it from being used only like this without "%", "@" or "toTable"?

Just a two comments above:

> `{}` is a sugar for array of tuples

.


Re: Emscripten/WebAssembly GC considerations?

2019-07-12 Thread gemath
Due to the changes in emscripten you mentioned, my own WASM example code now 
compiles and runs fine with the following simple nim.cfg:


@if emscripten:
   cc = clang
   clang.exe = "emcc"
   clang.linkerexe = "emcc"
   cpu = i386
@end


Run

and this command line:

`nim c -d:emscripten -d:release -d:danger -o:.html .nim`

For ASM.js output,


-t:"-s WASM=0" -l:"-s WASM=0"


Run

is inserted into the command line. I'm running Nim 0.20 and emscripten 1.38.38 
on Linux 64bit, so your problem might be specific to Windows or to your code.


Re: What's the most idiomatic way to call ``execCmdEx`` in parallel?

2019-07-12 Thread dawkot
It would be something like this: 


import std / [osproc, streams, asyncdispatch]

proc asyncProcess(cmd: string, args: seq[string]): Future[(int, string)] 
{.async.} =
  var p = startProcess(cmd, args=args)
  while p.running: await sleepAsync 5
  return (p.peekExitCode, readAll p.outputStream)

let a = asyncProcess("nim", @["-v"])
let b = asyncProcess("nim", @["-v"])

for (code, output) in waitFor all(a, b):
  echo ">> ", code, "\n\n", output


Run


Re: What's the most idiomatic way to call ``execCmdEx`` in parallel?

2019-07-12 Thread Stefan_Salewski
> I'm not particularly bothered whether it's threads or async, I just want to 
> execute the command in parallel rather than sequentially.

Of course we know that async mean "non blocking", but not really parallel, see

[https://stackoverflow.com/questions/6133574/how-to-articulate-the-difference-between-asynchronous-and-parallel-programming](https://stackoverflow.com/questions/6133574/how-to-articulate-the-difference-between-asynchronous-and-parallel-programming)


Re: What's the most idiomatic way to call ``execCmdEx`` in parallel?

2019-07-12 Thread zevv
osproc execProcesses?

[https://nim-lang.github.io/Nim/osproc.html#execProcesses%2CopenArray%5Bstring%5D%2Cproc%28int%29%2Cproc%28int%2CProcess%29](https://nim-lang.github.io/Nim/osproc.html#execProcesses%2CopenArray%5Bstring%5D%2Cproc%28int%29%2Cproc%28int%2CProcess%29)


Re: What's the most idiomatic way to call ``execCmdEx`` in parallel?

2019-07-12 Thread OldhamMade
In a way, yes. I'm not particularly bothered whether it's threads or async, I 
just want to execute the command in parallel rather than sequentially.


Re: What's the most idiomatic way to call ``execCmdEx`` in parallel?

2019-07-12 Thread OldhamMade
Thanks, I'll take a look at `asyncdispatch`. If you have time for a quick 
example, it would be most helpful!