Re: Proposal to start a Nim-Scientific Community

2019-09-30 Thread mratsim
I do, in Arraymancer: 
[https://github.com/mratsim/Arraymancer](https://github.com/mratsim/Arraymancer)/

For NLP there was some wrappers here: 
[https://github.com/Nim-NLP](https://github.com/Nim-NLP) with a focus on NLP on 
the Chinese language.

For FSM for NLP, I've came across 
[BlingFire](https://github.com/microsoft/BlingFire) from Microsoft research but 
I guess the most flexible tokenizer is 
[sentencepiece](https://github.com/google/sentencepiece) by Google which does 
unsupervised training and does not assume anything about the language 
(whitespaces), you can just give it things to read.


Re: Getting a strange error with file streams

2019-09-30 Thread Stefan_Salewski
Compiler message is very clear. There is only proc setPosition(s: Stream; pos: 
int) which takes an non optional int as second parameter. So call 
file.setPosition() is invalid. I have no idea what you wants to do exactly, 
maybe you assume that there is a default parameter to setPosition()? There is 
none in this case, so maybe use file.setPosition(0). See 
[https://nim-lang.org/docs/lib.html](https://nim-lang.org/docs/lib.html)


Re: Front page example

2019-09-30 Thread Stefan_Salewski
Also see 
[https://github.com/nim-lang/website/issues/162](https://github.com/nim-lang/website/issues/162)


Re: Hello, Nim!

2019-09-30 Thread Pixeye
thaaank you good sir : )


Re: Hello, Nim!

2019-09-30 Thread Pixeye
thaaank you good sir : )


Re: Front page example

2019-09-30 Thread miran
Every number in `[10, 20, 30, 40, 50, 60]` is even. That's on purpose.

This iterator returns elements at odd _positions_ , i.e. index 1, 3, 5.


Front page example

2019-09-30 Thread Nimster
This code on the front page as written returns the EVEN numbers from the 
function oddElements. Either i should be set to 0, or the passed array needs a 
0 in the front in order to return ODD elements.

# Thanks to Nim's 'iterator' and 'yield' constructs, # iterators are as easy to 
write as ordinary # functions. They are compiled to inline loops.

iterator oddElements[Idx, T](a: array[Idx, T]): T =
var i = 1

while i < a.len:
yield a[i] i += 2
for x in oddElements([10, 20, 30, 40, 50, 60]):
echo x


Getting a strange error with file streams

2019-09-30 Thread dotheda
First off, not even sure if the code will really work once I fix this, but I'm 
sure I can figure out the rest;


proc get_file_data(file: FileStream, offset: int, size: int): seq[char] =
var test = newSeq[char]()
file.setPosition()

for i in offset .. (size - 1):
var x: char
file.setPosition(i)
discard file.readData(x.addr, 1)
result.add(x)

test


Run

returns the error 


main.nim(32, 9) Error: type mismatch: got 
but expected one of:
proc setPosition(s: Stream; pos: int)

expression: setPosition(file)


Run

I'm not sure what type I'm supposed to be using? I've tried Stream as well. 
Maybe there's a more elegant way to do this as well? Not sure.


Re: Proposal to start a Nim-Scientific Community

2019-09-30 Thread minierolls
Would like to contribute here too! I am currently working on a (un)weighted 
FSA/FSM library (which can be easily used to implement HMMs), which might be 
useful for any NLP/Speech folks. Highly interested in keeping up with the 
progress in the community; is anyone working on a generic ndarray/matrix 
computation library? 


Re: 1.0.0 is here

2019-09-30 Thread tedlavarias
That's what brought me here! Nim's announcement of going 1.0.0 showed up on my 
Google News feed so I took a look... And lo' and behold, this is exactly what 
I've always been hoping for! A language that combines the elegance of Python, 
with the safety of Pascal/Ada, and the power/speed of C! I thought it was too 
good to be true... When browsing through some sample Nim code, I was still 
hoping I would see := as the assignment operator though... I hope I can find 
something I can contribute to! :)


Re: Karax/Jester simple CRUD example?

2019-09-30 Thread juancarlospaco
I recommend you start learning Jester first, then you learn/add stuff on 
Frontend. Both projects have examples on their own repos, theres infinite 
possibilities on how to combine them. 🙂


Re: How to tie procedure to type?

2019-09-30 Thread mratsim
I tried 4 different ways, 2 fails, 2 works.

Using a proc as a generic (failure): 


type
  BinaryTree[V; compare: proc(a, b: V): bool] = object
value: V

proc insert[V; compare](tree: BinaryTree[V, compare], value: V): bool =
  # Error: attempting to call routine: 'compare'
  # found 'insert.compare [declared in 
/home/beta/Programming/Nim/Arraymancer/build/bintree.nim(5, 16)]' of kind 'type'
  compare(value, tree.value)

var a = BinaryTree[int, proc(a, b: int): bool = a < b](value: 10)

echo insert(a, 20)


Run

Using a proc as a static (failure): 


type
  BinaryTree[V; compare: static proc(a, b: V): bool] = object
# Error: cannot generate VM code for proc (a, b: V): bool
value: V

proc insert[V; compare](tree: BinaryTree[V, compare], value: V): bool =
  compare(value, tree.value)

var a = BinaryTree[int, proc(a, b: int): bool = a < b](value: 10)

echo insert(a, 20)


Run

Using a compile-time switch (success): 


type
  CmpKind = enum
GreaterThan
LowerThan
  
  BinaryTree[V; cmp: static CmpKind] = object
value: V

proc insert[V; cmp](tree: BinaryTree[V, cmp], value: V): bool =
  when cmp == GreaterThan:
value > tree.value
  elif cmp == LowerThan:
value < tree.value

var a = BinaryTree[int, LowerThan](value: 10)

echo insert(a, 20)


Run

Using a compile-time dispatch table (success): 


import macros

type
  CmpKind = enum
GreaterThan
LowerThan
  
  BinaryTree[V; cmp: static CmpKind] = object
value: V

let funcTable {.compileTime.} = [
GreaterThan: ident">",
LowerThan: ident"<"
  ]

macro dispatch(a: typed, cmp: static CmpKind, b: typed): untyped =
  result = newCall(funcTable[cmp], a, b)

proc insert[V; cmp](tree: BinaryTree[V, cmp], value: V): bool =
  dispatch(value, cmp, tree.value)

var a = BinaryTree[int, LowerThan](value: 10)

echo insert(a, 20)


Run


Re: Proposal to start a Nim-Scientific Community

2019-09-30 Thread mratsim
I think we can replace iterator chaining by objects that represent the 
transformations.

Then it can be applied lazily like Dask (aka build a compute graph) or applied 
like D-ranges, a bit like @timotheecour 
[PoC](https://github.com/timotheecour/vitanim/blob/82b73565bf24a984658974790c8ada32632f5a6a/drange/src/drange/drange_algorithms.nim).
 AFAIK the new C++20 ranges work like this as well. 


Re: Hello, Nim!

2019-09-30 Thread mratsim
What you are missing is strformat:


import strformat

proc Print*()=
for pe in elements:
echo &" Time elapsed {pe.name} for {pe.t1-pe.t0} *"
index = 0
elements.setLen(0)


Run


Re: [RFC] Why use Nim?

2019-09-30 Thread rustomax
@sky_khan, thank you, this is educational. @Araq, nice list! There are some 
packages here I haven't come across of before, like gintro or fltk.


Re: Persistent data structures

2019-09-30 Thread e
You can also take a look at 
[https://github.com/dcurrie/nim-bbtree](https://github.com/dcurrie/nim-bbtree) 
, which implements a persistent Bounded Balance Tree library for Nim. 


Re: [RFC] Why use Nim?

2019-09-30 Thread Araq
There is

  * [https://github.com/filcuc/nimqml](https://github.com/filcuc/nimqml) (Qt 
for Nim, not sure about its limitations)
  * [https://github.com/nim-lang/ui](https://github.com/nim-lang/ui)
  * [https://github.com/khchen/winim](https://github.com/khchen/winim)
  * [https://github.com/PMunch/wxnim](https://github.com/PMunch/wxnim)
  * [https://github.com/nim-lang/iup](https://github.com/nim-lang/iup)
  * [https://github.com/Skrylar/nfltk](https://github.com/Skrylar/nfltk)
  * 
[https://github.com/trustable-code/NiGui](https://github.com/trustable-code/NiGui)
  * [https://github.com/nim-lang/gtk2](https://github.com/nim-lang/gtk2)
  * 
[https://github.com/stefansalewski/gintro](https://github.com/stefansalewski/gintro)



And probably more that I missed (my apologies!). I guess settling for a 
"standard" solution would be nice, but how to do that without offending 
anybody... :-)


Hello, Nim!

2019-09-30 Thread Pixeye
First, gratz nim team on releasing v.1.0 I'm just sharing my...first 
impressions :) I'm a C# game developer that wants to learn something new and 
fresh, it's difficult to change mindset sometimes though.

I decided to write something very stupid like time profiler "hello world" and I 
thought it will be EASY PIZY but...I ended up getting SIGSEGV: Illegal storage 
access. (Attempt to read from nil?)

I like to try to solve my problems myself and I realized that it must be 
something stupid about my code 


var
elements = newSeq[ProfileElement](2)
index: int


Run

It took me some time to realize that newSeq created 2 nulled objects and I 
needed to use 


newSeqOfCap


Run

It's something very unfamiliar for me and I still didn't decide whether I like 
it or not. The thing that made me sad was string formatting. A lot of useless 
comma stuff in the string


echo " Time elapsed ",$(pe.name)," for ", $(pe.t1-pe.t0),"*"


Run

It's something I miss from C# where I can write like


  $" Time elapsed {pe.name} for {pe.t1-pe.t0} *"


Run

But maybe I don't know something about formatting in Nim. In the end, the code 
looks something like this XD

I think I'll hang around and try to write something more interesting and 
complex to decide if Nim is great for game dev :)


type
ProfileElement = object
name: string
t0, t1: float

var
elements = newSeqOfCap[ProfileElement](2)
index: int

proc newProfileElement*(arg: string): ProfileElement =
result = ProfileElement(
name: arg,
t0: cpuTime()
)

proc  Start*(arg:string)=
var el = newProfileElement(arg)
el.name = arg
el.t0 = cpuTime()
elements.add(el)


proc  End*()=
var el = elements[index]
el.t1 = cpuTime()
index += 1

proc Print*()=
for pe in elements:
echo " Time elapsed ",$(pe.name)," for ", $(pe.t1-pe.t0),"*"
index = 0
elements.setLen(0)


profile.Start("Test1")
profile.End()

profile.Start("Test2")
profile.End()

profile.Start("Test3")
profile.End()

profile.Print()


Run


Re: [RFC] Why use Nim?

2019-09-30 Thread sky_khan
If you use only OS native widgets you'll have multiple-platform library, not 
cross-platform. You'll have to deal with every single detail for each widget on 
each OS you support. If a widget is not exists on one of your target OSes or 
behaves completely different, tough luck. Thats why Lazarus is still behind on 
Mac front.


Re: Karax/Jester simple CRUD example?

2019-09-30 Thread r3c
Thanks Huan, but I was looking for the most simpe CRUD example with 
Jester/Karax and all I found is this: 
[https://github.com/emekoi/joker](https://github.com/emekoi/joker) Which is 
useless, because its rendering static strings on the FE.

Too bad, the frameworks are there, but there is lack of practical examples, I 
can imagine how demotivating is this for new users...


Compile-time matrix sizes

2019-09-30 Thread japplegame
How to define matrix type with compile time sizes? C++ code: 


template
struct Matrix {
T data[M][N];
};

Matrix m;


Run

How to write something like this in Nim?


Re: Nim for Beginners Video Series

2019-09-30 Thread Kiloneie
#7 is live ! LINK: [https://youtu.be/Ystn3SG2yeU](https://youtu.be/Ystn3SG2yeU)

Is the terminal now clearly visible from phones ? My phone is rather big so i 
am unsure.


Re: Persistent data structures

2019-09-30 Thread rayman22201
First place to check for libraries in general is 
[https://nimble.directory](https://nimble.directory) (Nims version of npm)

We do have this: 
[https://github.com/PMunch/nim-persistent-vector](https://github.com/PMunch/nim-persistent-vector)
 by @PMunch

Immutable data structures are possible in Nim, but I would not call it easy. 
Such structures are very difficult to optimize. Immutable structures are 
expensive!

Immutable data structures and that style of functional programming is not 
really the "strong point" of Nim. Nim is a much more low level language, where 
we care about memory and performance. It's the same reason you don't have 
immutable data structures by default in a language like Rust.

I don't want to put you off completely. Nim has some very powerful functional 
programming libraries. This one for example, is one of my favorites: 
[https://github.com/zero-functional/zero-functional](https://github.com/zero-functional/zero-functional)

In regards to translators from other languages to Nim, the biggest one is 
Py2Nim. This was used by Status to convert a complex crypto codebase to Nim 
very effectively: 
[https://github.com/metacraft-labs/py2nim](https://github.com/metacraft-labs/py2nim)

We also have nimterop, which isn't a full translator, but converts C / C++ 
header files to Nim for a better interop experience with those languages. 
[https://github.com/nimterop/nimterop](https://github.com/nimterop/nimterop)


Re: [RFC] Why use Nim?

2019-09-30 Thread rustomax
Neither. What I am looking for is an OS-native executable.


Re: [Maybe a new feature] Hook free function pointer.

2019-09-30 Thread rayman22201
We already have this in Nim.

We have ptr in Nim. This is a raw C pointer. The GC does not track it. ref is a 
GC tracked pointer.

you can use gc_ref and gc_unref to convert between the two freely: 
[https://nim-lang.org/docs/system.html#GC_ref%2Cref.T](https://nim-lang.org/docs/system.html#GC_ref%2Cref.T)

Here is an example of working with the C FFI using cstrings from the manual: 
[https://nim-lang.org/docs/manual.html#types-cstring-type](https://nim-lang.org/docs/manual.html#types-cstring-type)

It works similarly for other types.


Re: [RFC] Why use Nim?

2019-09-30 Thread vonH
Is this application a compile to exe with a web frontend, or a compile to 
Javascript with a web frontend?


Re: Proposal to start a Nim-Scientific Community

2019-09-30 Thread cdome
Hi, +1 to discuss scientific Nim in this forum until community is going to grow 
big enough. I am making a number of scientific calculations with Nim but from 
stochastic differential equations and Monte Carlo simulations for finance and 
insurance angle rather than neural networks and data mining.

DataTable package was my pilot project to get more familiar with Nim. IMO, to 
finish it woulr require a couple of improvements in Nim itself, namely: 
chaining of iterators and static[T] type improvements.


Re: Persistent data structures

2019-09-30 Thread ShalokShalom
So is there a popular translator from another language to Nim?


Re: Persistent data structures

2019-09-30 Thread dawkot
> Why do you think its easy?

Well, it should be just a matter of translating whatever code is out there to 
nim.


Re: Persistent data structures

2019-09-30 Thread ShalokShalom
Yes, I think so. I am wondering that it is not yet implemented, if so. So far 
as I am informed are the different kinds of implementation different in terms 
of memory usage and so.

Why do you think its easy?


Re: Karax/Jester simple CRUD example?

2019-09-30 Thread juancarlospaco
  * Get data from Database to a Nim object type, `Row` usually.
  * Stringify Nim object type adding some HTML optionally, `$` usually.
  * Send over the string with Jester `resp "" & data & ""`.



Basically is kinda like that.

Having said that, if you want a more complete round solution you can check [Nim 
Web Creator](https://nimwc.org/login) you can [learn more 
here](https://dev.to/juancarlospaco/self-firejailing-web-framework-h5l). It 
builds on top of Jester. Check the [Nimble](http://nimble.directory) is >1000 
useful modules. 🙂


Re: Persistent data structures

2019-09-30 Thread r3c
You mean something like 
[immer.js](https://immerjs.github.io/immer/docs/introduction) ?

I think not, but it would be very easy to create library like this


Persistent data structures

2019-09-30 Thread ShalokShalom
Persistent data structures 
 are a fundamental 
idea of functional programming. With that, it is possible to create new 
instances of constants without altering them, which is obviously beneficial for 
multi-threaded programming.

Is this supported in Nim?

I have looked into the available documentation, have not found anything yet. 


[Maybe a new feature] Hook free function pointer.

2019-09-30 Thread Lachu
Nim, as far as I known, uses GC, but can use C headers to generate bindings. C 
and C++ doesn't use GC, so there could be some weird situations, when C/C++ 
library free an memory and our program store pointer to this memory somewhere.

Few years ago I had similar problem and solve it. I use free wrapper, iterate 
through list stores real memory pointers and replace pointer in this list to 0, 
where it was matched. When GC ran, it iterate on this list and free elements 
with NULL pointer.

Solution for Nim could been similar. Just add new pointer type. It will be 
wrapper of normal pointer. To create my pointer type, programmer might specify 
free function and it will be injected or something at runtime (program start). 
When program tries to assign normal pointer to my pointer type, we iterate on 
wrappers list and check it exist currently. If not, we wrap it and create a new 
list element. When assigning my pointer type var to variable of the same type, 
we copy address to a wrapper.

When program uses my pointer type var, we check there exist NULL value. If it's 
true we will throw an exception.


Re: Great tutorials needed

2019-09-30 Thread ShalokShalom
[https://narimiran.github.io/nim-basics](https://narimiran.github.io/nim-basics)/
 reads awesome ^-^


Re: Newbie experience with the documentation

2019-09-30 Thread ShalokShalom
Yeah, thats great.

I suggest a beginners guide.

What you think about it?


Re: Newbie experience with the documentation

2019-09-30 Thread ShalokShalom
Yeah, that is great.

I suggest a beginners guide.

What do you think about it?


Re: Newbie experience with the documentation

2019-09-30 Thread Araq
Every document uses the same search and the search focusses on the APIs. Not 
ideal for your case but also no evil forces behind the curtain.


Re: Newbie experience with the documentation

2019-09-30 Thread ShalokShalom
The manual is not on top of the list of the documentation, as I said.

Despite the fact the documentation about _type_ is quite sparse, I think it is 
important to provide the very fundamentals of the language on a prominent spot.

Opposed to hidden within a wall of unrelated content, also the search function 
in said manual does not provide any hint for this section, so zero chance to 
find the documentation for the literally **first keyword used in an example** 
on your own homepage.


Re: Proposal to start a Nim-Scientific Community

2019-09-30 Thread bobd
I'm working on stats/data science code (e.g. a Nim interface for R) and will 
open source everything when it's in a usable state (probably early next year). 
But in the meantime it would be useful to have somewhere to coordinate with 
other people working in this area - primarily to avoid re-inventing the wheel. 
For example, I've seen that user cdome is working on a DataTable - something 
that's on my own to-do list (this is as good a time as any to ping you cdome 
for more info!). MeWe is one possibility I guess.


Example for using websocket with protobuf?

2019-09-30 Thread geohuz
I'm trying to use the protobuf with websocket, but I'm stuck on very simple 
case like send and receive message, my code is sth like below:


const protoSpec = """
syntax = "proto3";
package DS;

enum TOPIC {
  UNKNOWN = 0;
  PARSER = 1;
  CONNECTION = 2;
  AUTH = 3;
  EVENT = 4;
  RECORD = 5;
  RPC = 6;
  PRESENCE = 7;
  MONITORING = 8;
  CLUSTER = 9;
  LOCK = 10;
  STATE_REGISTRY = 11;
  ERROR = 100;
}

enum CONNECTION_ACTION {
  UNKNOWN = 0;
  
  ERROR = 1;
  PING = 2;
  PONG = 3;
  ACCEPT = 4;
  CHALLENGE = 5;
  REJECT = 6;
  REDIRECT = 7;
  CLOSING = 8;
  CLOSED = 9;
  
  AUTHENTICATION_TIMEOUT = 100;
  INVALID_MESSAGE = 101;
}

message Msg {
  TOPIC topic = 1;
  CONNECTION_ACTION action = 2;
  string url = 3;
  string protocolVersion = 5;
}
"""

parseProto(protoSpec)
var msg = new DS_Msg
msg.topic = DS_TOPIC.CONNECTION
msg.action = DS_CONNECTION_ACTION.CHALLENGE
msg.url = "ws://localhost:6020"
msg.protocolVersion = "0.1a"

var stream = newStringStream()
stream.write msg

let ws = waitFor newAsyncWebsocketClient("localhost", Port(6020), path="/")
echo "Connected!"
echo "Sending a challenge"
discard ws.sendBinary(stream.data) # is this correct?

proc ping() {.async.} =
  while true:
await sleepAsync(6000)
echo "ping"
await ws.sendPing()

proc read() {.async.} =
  while true:
let (opcode, data) = await ws.readData()
echo "(opcode: ", opcode, ", data: ", data, ")"

asyncCheck read()
asyncCheck ping()
runForever()


Run

My question are:

  1. how to serialize the protocol encoded payload and send through websocket?
  2. how to read and parse the protocol encoded payload from peer?




Re: Nim source size

2019-09-30 Thread federico3
The Debian (and therefore Ubuntu) package recommends gcc and build-essentials 
and uses only 31MB. There is also a nim-doc package that contains 
documentation. You can use it to build a Docker image with everything you need.


How to tie procedure to type?

2019-09-30 Thread japplegame
In D language we can tie function to a template by alias: 


struct BinaryTree(T, alias cmp_fun) {
alias V = T
alias compare = com_fun;
}

void main() {
alias Tree = BinaryTree!(int, (a, b) => a < b);
}


Run

It is no need to store the comparator in the tree object, also it is no need to 
pass the comparison function to other functions. Since it is possible to 
extract the comparison function from the type: 


void insert(T)(T tree, T.V value) {
...
if(T.compare(value, node.value)) {...}
}


Run

The same nim code failed to compile: 


type
  BinaryTree[V, compare] = object
value: V

proc insert(tree: BinaryTree, value: BinaryTree.V): bool =
  BinaryTree.compare(value, tree.value) # Error: a type conversion takes 
exactly one argument

var a = BinaryTree[int, proc(a, b: int): bool = a < b](value: 10)

echo insert(a, 20)


Run


Re: [RFC] Why use Nim?

2019-09-30 Thread rustomax
> Writing a good GUI framework that can do gpu accelerated rendering and call 
> to native APIs is a huge task

I am out of my depth here, so please help me understand this better. Why do I 
need to worry about GPU acceleration to render a button or a checkbox in Nim? 
If a Nim wrapper library is calling let's say a native OS primitive for such 
interface elements, won't the OS take care of GPU acceleration and such for me. 
From Nim's perspective, there should be no need to meddle in low level details. 
Or am I thinking about this wrong?

Just to be clear, I am not talking about creating a crazy complex interface 
here. It's more along the lines of something similar to this:


Karax/Jester simple CRUD example?

2019-09-30 Thread r3c
In other frameworks like Django, you can read from the DB into array/list, pass 
the array onto view and display it trough HTML Template. So you can do this 
without single line of JS code.

Is it possible something similar with Jester? How do I pass the array from the 
BE to FE?


Re: Is it a bug?

2019-09-30 Thread Araq
@mratsim I gave it a try but the root problem is that C++ compilers do not 
support "named fields in const unions" (yet?), a C99 addition. So you can only 
initialize the first field of a union and nothing else.


Re: Is it a bug?

2019-09-30 Thread mratsim
Can't we do the other way around, start supporting case objects properly across 
compile-time to runtime boundary?

That would be very useful for both 
[nimbus](https://github.com/status-im/nimbus/blob/78714295dab4ead5e56c0048c46ae07d8bbed91a/nimbus/vm/interpreter/gas_costs.nim#L360-L361),
 as a core component is a let and instead could be inline as a const, and Laser 
where I defined my own AST to be used at runtime (with LLVM) and compiletime 
(with Nim macros) and not being able to cross the boundary would be a 
restriction. 


Re: Proposal to start a Nim-Scientific Community

2019-09-30 Thread sschwarzer
I'm currently not using Nim for scientific computing, but I'm quite interested 
in what's going on in this area and follow related discussions in the forum.


Re: Cross compilation linux -> win + wine issue with file reading

2019-09-30 Thread sschwarzer
Have you tried reading the file and printing the start of its contents - at 
least up to and a bit beyond the first null byte as known from the hex editor - 
as hex strings to stdout? That way you could at least check if the problem 
already occurs when reading.


Re: Is it a bug?

2019-09-30 Thread Araq
Fair enough, consider it done.


Re: Is it a bug?

2019-09-30 Thread japplegame
Then why don't you start the deprecation process?


Re: Library for linear algebra

2019-09-30 Thread andrea
Hi Royi, as I have written in the README, 
[linear-algebra](https://github.com/unicredit/linear-algebra) is now 
discontinued in favor of its sequel [neo](https://github.com/unicredit/neo). 
But be sure to also check [ArrayMancer](https://github.com/mratsim/Arraymancer) 
which is much more advanced and optimized (but has a different API based on 
tensors)