I don't see "collect" used in that comprehension module at all. Also,
**comprehension** is slightly broken with nim-1.0.0. I haven't tried nim-devel
yet. (I can only use official releases at work.)
You can always follow our github instructions to produce a Nim devel version.
First get Nim from github:
git clone
[https://github.com/nim-lang/Nim.git](https://github.com/nim-lang/Nim.git)
cd Nim
build_all.bat
I'm using MSYS2/MINGW64, so can't use choosenim. :(
As I said, you must use the devel version. You can install it with a tool
called choosenim.
Hi, I encountered "Error: undeclared identifier: 'collect'"
I generally only put brackets where neccessary.
You can write this using devel version of nim (you can use choosenim)
import tables, sugar, sets, sequtils
proc cross(xs, ys: string): seq[string] =
for x in xs: (for y in ys: result.add x & y)
let
rows =
It would be nice if when something is deprecated in the standard library there
was a link to the associated discussion/decision summary (if any). I found the
deprecated list comprehension type a while ago and was curious about why it was
deprecated.
Expected results...
# Python output:
unitlist = [['A1', 'B1', 'C1', 'D1', 'E1', 'F1', 'G1', 'H1', 'I1'],
['A2', 'B2', 'C2', 'D2', 'E2', 'F2', 'G2', 'H2', 'I2'],
['A3', 'B3', 'C3', 'D3', 'E3', 'F3', 'G3', 'H3', 'I3'], ['A4', 'B4', 'C4',
'D4', 'E4', 'F4', '
Expected results...
# Python output:
unitlist = [['A1', 'B1', 'C1', 'D1', 'E1', 'F1', 'G1', 'H1', 'I1'],
['A2', 'B2', 'C2', 'D2', 'E2', 'F2', 'G2', 'H2', 'I2'],
['A3', 'B3', 'C3', 'D3', 'E3', 'F3', 'G3', 'H3', 'I3'], ['A4', 'B4', 'C4',
'D4', 'E4', 'F4', '
Thanks for the info. Yes, I'd tried the sugar package but failed. Will checkout
the comprehension package.
Thanks!
Just a clarification, why your codes have the brackets around the y loop, and
not around the result.add?
cross(xs, ys: string): seq[string] =
for x in xs: (for y in ys: result.add x & y)
Run
While @trtt has it around the result.add?
cross(a, b
> how about you first get familiar with the correct Nim syntax for them? link
Wait, you are linking against the deprecated lc syntax.
I believe the closest non-deprecated solution is
[https://github.com/alehander92/comprehension](https://github.com/alehander92/comprehension)
?
> The documentation of Seq & Tables are very poor in Nim. Not many examples to
> learn from.
If something has a solid documentation and lots of examples, then it is
`tables` module. :rolleyes:
> I tried with...
>
> [invalid syntax]
If you really want to force list comprehensions, how about you first get
familiar with the correct Nim syntax for them?
[link](https://nim-lang.org/docs/sugar.html#\[\].m%2CListComprehension%2Cuntyped%2Cuntyped)
But my advice would be: don't use list comp
This should work:
import tables, strutils
proc cross(xs, ys: string): seq[string] =
for x in xs: (for y in ys: result.add x & y)
let
rows = "ABCDEFGHI"
cols = "123456789"
squares = cross(rows, cols)
var unitSeq: seq[string]
for c
Some more codes...
# Python codes:
unitlist = ([cross(rows, c) for c in cols] +
[cross(r, cols) for r in rows] +
[cross(rs, cs) for rs in ('ABC','DEF','GHI') for cs in
('123','456','789')])
units = dict((s, [u for u in unitlist if s in u])
for s i
@trtt & @miran, Thanks very much. Yes, i notice I made the mistake of the
return type in the proc.
There are few problems with your Nim version, e.g. returning `string` instead
of `seq[string]` (cause this is what the Python version does - it returns a
list).
Here is a correct Nim version:
proc cross(xs, ys: string): seq[string] =
## Cross product of elements in A and ele
That's not list comprehension in nim, you just iterated over two strings and
then joined the elements without using them.
proc cross(a, b: string): seq[string] =
for ac in a:
for bc in b:
result.add(ac & bc)
let digits = "123456789"
let letters =
quares = cross(rows, cols)
Run
I hit " Error: expression 'a & b' is of type 'string' and has to be discarded"
Please help to explain how to convert the nested list comprehension. Thanks in
advance.
20 matches
Mail list logo