Re: unique index
@mashigan & @miran Ty guys, you both point me to tables and i also thought that tables would solve my problem. I thought that tableref would reference the content like record number. But it reference the whole table. The point is that i want to prevent to walk through the whole table to see if a value exists. But i think that there is no other option. But now i am thinking/talking about it, i suddenly have a idea what would solve this. Because i know what the key's are before compilation. Thanks guys.
Re: Error: module names need to be unique per Nimble package;
thank very match. I think, that correct path is touch src/moje/a/a.nimble touch src/moje/b/b.nimble It works.
Re: Is there any way to create template with await?
Yes, you can do this: template withDb(body: untyped) = let dbFut = open(...) yield dbFut let db = dbFut.read() defer: close(db) block: body
Re: Error: module names need to be unique per Nimble package;
The compiler uses a .nimble file in a similar way as Python uses the __init__.py files. To fix your issue you can just: touch src/moje/a.nimble touch src/moje/b.nimble Should work then.
Re: unique index
Maybe I'm misunderstanding the question, but to me this looks like a case where you would want to use [tables](https://nim-lang.org/docs/tables.html) instead of seq.
Re: unique index
But maybe wrapped to `TableRef` is better I guess instead of seq if you need to have unique key
Re: unique index
implement proc `==` and `contains` for you specialized type import sequtils type DataBuffer* = tuple key : string mytype : string value : string proc `==`(a, b: DataBuffer): bool = a.key == b.key proc contains[T: DataBuffer](d: T, dbuf: seq[T]): bool = for data in dbuf: if d == data: return true false # change proc to be different to work with generic add proc dataAdd[T: DataBuffer](data: var seq[T], newdata: T): bool {.discardable.} = if newdata in data: data[data.find(newdata)] = newdata false else: data.add newdata true var mydata = newSeq[DataBuffer]() mydata.dataAdd(("a","int","4")) mydata.dataAdd(("a","int","5")) echo mydata # if expect only the last add to be visible ([a, int, 5]) With boolean return, you can know whether it replaced old value or add new to collections. You can just change the dataAdd to return DataBuffer so it will return old value that's be replaced or return a newly added value.
unique index
Hi all, I have a problem finding a solution for a problem (or perhaps i am looking the wrong direction). I want to check if a key is already set and if that is the case, overwrite it with the new value. Off course there is the possibility to walk through the whole sequence to see if a key exists, but is there a smarter way? I only have this code : type DataBuffer* = tuple key : string mytype : string value : string var mydata = newSeq[DataBuffer]() mydata.add(("a","int","4")) mydata.add(("a","int","5")) echo mydata # if expect only the last add to be visible ([a, int, 5])
Re: How to cross-compile a Nim executable for Android
@alexsad, take a look at [https://github.com/yglukhov/android](https://github.com/yglukhov/android)
Re: Arbitrary Precision Integer Math Operators
OK, so core Nim doesn't have an arbitrary precision math capability. Is it something planned for in the roadmap to 1.0?
Re: How to cross-compile a Nim executable for Android
thanks a lot mashingan! It is working! Now I am going to implement JNI to call java classes(some SDK has been already developed in Java) for manage android application.
Error: module names need to be unique per Nimble package;
If I have two modules with the same name in differrent directory, I get compile error **Error: module names need to be unique per Nimble package;** tree . ├── moje.nimble ├── src │ ├── moje │ │ ├── a │ │ │ └── x.nim │ │ └── b │ │ └── x.nim │ ├── moje.nim │ └── nimcache │ ... Why it is? thanks
Re: Compiling nim/ui in linux
Araq I checked again to see if that issue exist but now I am not able to replicate it. It happend when I was setting up my workspace in a new linux machine. Cant say for sure wt happened but it does work now.
Re: How to call a macro from a template with a constructed identifier argument?
@mratsim: The code needs two changes to work: import macros macro defthing(name: untyped): untyped = # used this instead of parseStmt result = quote do: let `name` = 1000 # Now things get really weird: added a dummy argument and things work as intended.. template calldefthing(name, t: untyped) = defthing `name Extra` calldefthing(x, y) echo $xExtra
Re: Arbitrary Precision Integer Math Operators
At Status we require that for cryptographic work and we are developing and supporting the following: * nim-ttmath: [https://github.com/status-im/nim-ttmath](https://github.com/status-im/nim-ttmath). This wraps the [ttmath C++ bignum library](https://www.ttmath.org/). This requires the [following patches](https://github.com/nim-lang/Nim/pull/7333) to `static[int]`. * Mpint: [https://github.com/status-im/mpint](https://github.com/status-im/mpint). WIP, don't use it. You can also check my own number theory repo: [https://github.com/numforge/number-theory](https://github.com/numforge/number-theory) which is just a reorganization of code I've used for Project Euler ([my repo here](https://github.com/mratsim/nim-projecteuler)). Of note it will provide a template that replace all operations in a scope by modulo operation: import number_theory modulo(42): let x = 10 + 50 let y = x ^ 10 echo y Currently it does not work though, you get stack overflow because the +/-/mul/div within addmod, submod, etc are also replaced ... Edit @lsrcd: Python is probably using libmpdec too: [http://www.bytereef.org/mpdecimal](http://www.bytereef.org/mpdecimal)/
Re: How to call a macro from a template with a constructed identifier argument?
Normally to do identifier construction of variable you need to use `{.inject.}` However untyped macro will capture everything and you will have to deal with the inject pragma in your macro. Instead forget about the template and do identifier construction directly in it. import macros static: echo "Target macro: " dumpASTgen(): let xExtra = 1000 static: echo "#" macro defthing(name: untyped): untyped = echo name.treerepr result = newStmtList() result.add nnkLetSection.newTree( nnkIdentDefs.newTree( newIdentNode($name & "Extra"), newEmptyNode(), newLit(1000) ) ) defthing awesome echo "xExtra is: " & $awesomeExtra Now building on your example, if you want to match normal Nim syntax and macro calls. Here are 3 ways to do that from within macros and templates: import macros macro defthing(name: untyped): untyped = echo name.treerepr result = newStmtList() result.add nnkLetSection.newTree( nnkIdentDefs.newTree( newIdentNode($name & "Extra"), newEmptyNode(), newLit(1000) ) ) # 1. Extra stuff in macro v1 template extraThing(): untyped = echo "wow extra from template with normal Nim syntax" result.add getAST(extraThing()) # 2. Extra stuff in macro v2" result.add quote do: echo "yet more thing to do in macro with normal Nim syntax" template yetyetAnother(name: untyped): untyped = # 3. Extra stuff in template defthing(name) echo xExtra * 1337 yetyetAnother(x)
Re: Arbitrary Precision Integer Math Operators
To add to Stephan's answer, I have tried both packages when solving puzzles from "Euler project": * "bigints" is pure Nim and has the best API in my opinion, but it is about twice slower than "bignum" and has some issues (see comments in source); it is still a work in progress. * "bignum" uses the "gmp" wrapper and frequently uses types as "clong" in its API which I don't like. But it works fine with good performance. Curiously, I have found Python (using "pypy" interpreter) to be more performing when dealing with long integers than Nim with "bignum" (for instance when doing Rabbin-Miller primality tests). So, it seems there is still room for some improvement.
Re: Passing data prom one thread to another
It is not Nim specific problem, you know, it is in general GUI "problem". In my practice the best way to send some data (and when I say "data" I mean "more than DWORD value") to UI is to save that data in some temporary container and use "message to ui" just to say "Hi, here is some data for you" and when appropriate handler received and processed this message it can pull original data from container.
Re: Arbitrary Precision Integer Math Operators
Are the nimble packages bignum and bigints not good enough for you? I think one is a wrapper, the other is pure Nim from smart Mr. Felsing.
Arbitrary Precision Integer Math Operators
I have Ruby/Crystal code that uses arbitrary precision integer numbers (really BIG numbers). Looking at Nim's math libraries they don't appear to be arbitrary precision. [https://nim-lang.org/docs/lib.html#pure-libraries-math-libraries](https://nim-lang.org/docs/lib.html#pure-libraries-math-libraries) I need at least the following operators: gcd(a,b) modinv(a,b) -> (a^-1) base b -> a*(a^-1) mod b = 1 modpow(a,e,b) -> a^e mod b intsqrt(a) -> a ^(1/2) introotn(a,n) -> a^(1/n) All the algorithms are fairly simple (a few lines of code), but does Nim use arbitrary math lib?
Passing data prom one thread to another
Passing data prom one thread to another Hi, I have created a process that's runing a thread that open some windows form, now i want that other running threads from the same process will be able to pass data to this windows form or even close it or reopen it. The Windows form GUI is always in use of the end users so i cant lock. what is the best way to implement it in c# ? i would like a short example, thanks.
Re: How to call a macro from a template with a constructed identifier argument?
Yes, that compiles, but the macro produces the code let x = 1000 instead of the code let xExtra = 1000 I'm trying to get the identifier to be constructed before passing it into the macro, but I'm not sure if that's possible.