@kayle, possibly reversing `seq`'s is not implemented in any of the current
standard libraries because it is so easy to do writing it yourself.
> You have, map, filter, reduce, etc. but not reverse?
These are a little trickier to implement that a simple reversal:
I recently needed to reverse a
@MrZoraman, Although there was some discussion on this forum in the past about
making Nim a little safer as to controlling mutation, I don't think that it was
@Araq who instigated the suggestions but rather others such as myself who
thought it might be a good option to have so as to compete
Several months back I remember reading a thread on here about Araq planning on
adding a borrow checker. Is that still the plan? I don't really want Nim to
have a borrow checker so I'm asking about this before I decide to invest more
time into learning/using Nim.
Thanks!
Yes, I've read them and I changed my code to use reverse() as two separate
lines...
I was commenting, albeit not clearly, on the fact that it's a loss from the
method that allows function chaining (in terms of style), which I think it neat.
I was also mentioning that someone should look at
I'm assuming you're talking about:
[https://rosettacode.org/wiki/Reverse_words_in_a_string#Nim](https://rosettacode.org/wiki/Reverse_words_in_a_string#Nim)
Look closely at that code and the 2 links I send.
You mentioned you can't do
list.reverse().join()
Run
BUT!
Hmm, thank you.
And it's not queuable either, I can't do `list.reverse().join()` like I could
with the rosetta reverse sample.
I'd recommend updating the rosetta code example to use the _algorithm_ lib if
that's the better way to do it.
{.compileTime.} will not do the work. In generated C code, value is copied into
a just before a is used.
Greetings people,
it's been a tick.
I've been toying with nim 1.0 and I was baffled to find out that I couldn't
simply reverse a seq. It's such a common operation in wildly available in other
languages, what gives?
You have, map, filter, reduce, etc. but not reverse? I had to ask.
cheers,
It's available in algorithm not in sequtils (I think I tripped over it too
once):
[https://nim-lang.org/docs/algorithm.html#reverse%2CopenArray%5BT%5D%2CNatural%2CNatural](https://nim-lang.org/docs/algorithm.html#reverse%2CopenArray%5BT%5D%2CNatural%2CNatural)
I think without an example code snippet that reproduces the problem theres no
way to know.
Explained in
[https://nim-lang.github.io/Nim/manual.html#templates-hygiene-in-templates](https://nim-lang.github.io/Nim/manual.html#templates-hygiene-in-templates)
Any variables declared in a template annotated with {.inject.} will be
accessible in the scope where the template is called,
IMO, the most benefit from DCE (Dead code elimination) that it allows to import
modules without worrying too much on other modules overhead.
For example if you imported sequtils and used only one proc from this module,
you will get only one extra proc extra (and its dependencies if any) in
Currently I use the [unittest module](https://nim-lang.org/docs/unittest.html)
from the standard library and `nimble test` to test [my
code](https://hg.sr.ht/~sschwarzer/vppdiff/browse/default/tests/test_cleanxmi.nim).
I like unittest's DSL. The tests look very clean.
What I miss is some
Code that is not used is removed from the final binary.
For example
proc noOneCalledMe() =
echo "Hello?"
echo 1+1
Run
The noOneCalledMe proc will not exist in the final binary created by Nim
because it's not used at all. In C or C++ this is usually
genericReset and genericResetAux.
See issues:
*
[https://github.com/nim-lang/Nim/issues/8745](https://github.com/nim-lang/Nim/issues/8745)
*
[https://github.com/nim-lang/Nim/issues/10132](https://github.com/nim-lang/Nim/issues/10132)
Note that one of the main source of generic reset,
Thank you. That works. Could you explain to me what was wrong with my code? It
seems that the important part is the
var matches: array[1, string]
Run
Why is
var matches: seq[string]
Run
wrong? I thought that's a list of strings that can be
Hi all, What is dead code elimination in Nim ?
In the past few days I've been examining a lot the nimcache files trying to
figure out what the final code looks like and what optimizations I could do to
my original code.
For example, I discovered things that I had not known and fixed them, mainly by
spotting crucial points were
Seems like you could just elaborate as needed:
proc double[T: SomeSignedInt](x: T): T =
when (T is int) or (T is int64) or (T is int32):
2 * x
else:
x
echo "int double(1): ", double(1)
echo "int64 double(1): ", double(1'i64)
echo
see for
[https://github.com/nim-lang/Nim/issues/12552](https://github.com/nim-lang/Nim/issues/12552)
for how
proc xyz[T : int](x : T)
Run
behaves
In the last days I created the requested macro for
[https://forum.nim-lang.org/t/5418](https://forum.nim-lang.org/t/5418)
[https://github.com/StefanSalewski/gintro/blob/master/gintro/gimpl.nim#L308](https://github.com/StefanSalewski/gintro/blob/master/gintro/gimpl.nim#L308)
which printed out
That works for your example.
It does not work if I want to treat int's and uints differently: (nb:
SomeSignedInt and SomeUnsignedInt are non-overlapping)
proc double[T: SomeUnsignedInt](x : T) : T = x
proc double[T: SomeSignedInt](x : T) : T = 2 * x
echo double(1) #
this is interesting,I was expecting that T was lifted to integer. Instead is
just validated it can be used as integer but the type stays as passed from
parameter.
proc xyz[T : int](x : T) =
echo "type of param is ", type(x)
echo "type of generic is ", T
echo
You could make some specialized types or exploit some of the combined types
that Nim already defines. For example:
proc double[T: SomeFloat](x : T) : T = x
proc double[T: SomeInteger](x : T) : T = 2 * x
echo double(1.0)
echo double(1)
echo double(1'i64)
proc xyz[T : int](x : T) = echo x
xyz(1'i8)
xyz(1'i32)
Run
generates two distinct procedures for int8 and int32 in the c code. How does
that happen if int is not a generic type, which would be treated as a type
class according to the spec?
"more specific" refers to `ref T` vs `T`, it doesn't refer to type generic type
constraints in the form `[T: int]` (which is super weird anyway, `int` is not a
typeclass).
The program doesn't compile for me:
temp2.nim(13, 7) Error: ambiguous call; both temp2.test_1(n: T: int or uint)
[declared in C:Usersrumpfprojectsnimtemp2.nim(7, 6)] and temp2.test_1(n: T)
[declared in C:Usersrumpfprojectsnimtemp2.nim(5, 6)] match for: (int
literal(10))
Given that the template in line 7 is more specific than the template in line 5,
is the compile time error the expected result?
I wonder whether integers work in Nim as most people would expect.
Is anybody surprised by the output of the program below?
const c_1bn = 1_000_000_000
let i_4bn = 4 * c_1bn
proc test_1[T](n : T) = echo n, "A"
proc test_1[T : int|uint](n : T) = echo n, "B"
Thanks
This still leaves the question: how can I avoid implementing each int procedure
for int64 also,
i.e. how can I ensure that the integer version of double is called for i
without uncommenting the int64 version?
converter lenientInt64toInt*(x: int64): int =
when
If your string and the extracted results fit in your memory you can use the
splitWhiteSpace iterator:
[https://nim-lang.org/docs/strutils.html#splitWhitespace.i,string,int](https://nim-lang.org/docs/strutils.html#splitWhitespace.i,string,int)
Otherwise write a simple parser using streams and
And second version, with run_hook as pragma
import tables, macros
var hooks {.compileTime.}: Table[string, NimNode]
macro registerHook(name: untyped{ident}, body: untyped): untyped =
result = newStmtList()
result.add newProc(
name = name,
Version 1, exactly what you asked.
import tables, macros
var hooks {.compileTime.}: Table[string, NimNode]
macro registerHook(name: untyped{ident}, body: untyped): untyped =
result = newStmtList()
result.add newProc(
name = name,
body =
> The system module has readLine but not readWord
There are dozen of ways to do it and dozen of examples -- some are optimized
for performance, some more for easy usage.
For example for lines of text, there is split iterator which can split the line
into words.
Miram already recommends
Thank you all for the help.
If I understand correctly, you have presented various parsers to convert a
character string into numbers.
Then, my next task is to read the text file word by word.
My example above is a short string of blank-separated numbers. In reality,
however, my file contains
Hi there,
I want to write macros that takes code written in one place and inserts it
elsewhere. In use this would look something like:
registerHook post_foo:
echo “foo done. x=“, x
...
proc foo =
let x = 10
runHooks post_foo
Run
My
I use dirty templates a lot but only when I need to capture 5~10 variables in
the environment.
It's very easy to refactor and forget that the dirty template was capturing
something, it happen to me with result. In your case the transformation is easy:
proc Core_Not*[F,X,V](f: F,
I have a proc which I want to directly include in my code, that's why I decided
templates is the way to go.
For example, in my caller proc I have:
case someVar
of 0: result = doSth()
...
Run
(obviously doSth() also returns a value)
Now, to make
I didn't want to introduce more flags initially but this approach simplifies
the real world use case of allowing local deps mode projects to be used
traditionally without having to jump through hoops. It's rare enough that the
extra typing is tolerable.
> For this python packaging scheme to work the c files generated by nim and
> nimpy need to compile on all the target platforms, and the list of source
> files to compile must to be the same on all the target platforms.
Well you need to find some other scheme that works then as the list of
40 matches
Mail list logo