Re: Nested list comprehension
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.)
Re: Nested list comprehension
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
Re: Nested list comprehension
I'm using MSYS2/MINGW64, so can't use choosenim. :(
Re: Nested list comprehension
As I said, you must use the devel version. You can install it with a tool called choosenim.
Re: Nested list comprehension
Hi, I encountered "Error: undeclared identifier: 'collect'"
Re: Nested list comprehension
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 = "ABCDEFGHI" cols = "123456789" squares = cross(rows, cols) let unitSeq = collect(newSeq, for c in cols: cross(rows, $c)) & collect(newSeq, for r in rows: cross($r, cols)) & collect(newSeq, for rs in ["ABC", "DEF", "GHI"]: for cs in ["123", "456", "789"]: cross(rs, cs)) let units = collect newOrderedTable: for s in squares: {s: collect(newSeq, for u in unitSeq: (if s in u: u))} let peers = collect newOrderedTable: for s in squares: var v = units[s].foldl(a & b) v.delete v.find s {s: v} echo "unitSeq:"; for u in unitSeq: echo u echo "\nunits"; for k, v in units: echo k, ": ", v echo "\npeers"; for k, v in peers: echo k, ": ", v Run For some reason peers isn't what it should, but the rest is ok.
Re: Nested list comprehension
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.
Re: Nested list comprehension
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', 'G4', 'H4', 'I4'], ['A5', 'B5', 'C5', 'D5', 'E5', 'F5', 'G5', 'H5', 'I5'], ['A6', 'B6', 'C6', 'D6', 'E6', 'F6', 'G6', 'H6', 'I6'], ['A7', 'B7', 'C7', 'D7', 'E7', 'F7', 'G7', 'H7', 'I7'], ['A8', 'B8', 'C8', 'D8', 'E8', 'F8', 'G8', 'H8', 'I8'], ['A9', 'B9', 'C9', 'D9', 'E9', 'F9', 'G9', 'H9', 'I9'], ['A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9'], ['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B9'], ['C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9'], ['D1', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8', 'D9'], ['E1', 'E2', 'E3', 'E4', 'E5', 'E6', 'E7', 'E8', 'E9'], ['F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9'], ['G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7', 'G8', 'G9'], ['H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7', 'H8', 'H9'], ['I1', 'I2', 'I3', 'I4', 'I5', 'I6', 'I7', 'I8', 'I9'], ['A1', 'A2', 'A3', 'B1', 'B2', 'B3', 'C1', 'C2', 'C3'], ['A4', 'A5', 'A6', 'B4', 'B5', 'B6', 'C4', 'C5', 'C6'], ['A7', 'A8', 'A9', 'B7', 'B8', 'B9', 'C7', 'C8', 'C9'], ['D1', 'D2', 'D3', 'E1', 'E2', 'E3', 'F1', 'F2', 'F3'], ['D4', 'D5', 'D6', 'E4', 'E5', 'E6', 'F4', 'F5', 'F6'], ['D7', 'D8', 'D9', 'E7', 'E8', 'E9', 'F7', 'F8', 'F9'], ['G1', 'G2', 'G3', 'H1', 'H2', 'H3', 'I1', 'I2', 'I3'], ['G4', 'G5', 'G6', 'H4', 'H5', 'H6', 'I4', 'I5', 'I6'], ['G7', 'G8', 'G9', 'H7', 'H8', 'H9', 'I7', 'I8', 'I9']] Run
Re: Nested list comprehension
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', 'G4', 'H4', 'I4'], ['A5', 'B5', 'C5', 'D5', 'E5', 'F5', 'G5', 'H5', 'I5'], ['A6', 'B6', 'C6', 'D6', 'E6', 'F6', 'G6', 'H6', 'I6'], ['A7', 'B7', 'C7', 'D7', 'E7', 'F7', 'G7', 'H7', 'I7'], ['A8', 'B8', 'C8', 'D8', 'E8', 'F8', 'G8', 'H8', 'I8'], ['A9', 'B9', 'C9', 'D9', 'E9', 'F9', 'G9', 'H9', 'I9'], ['A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9'], ['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B9'], ['C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9'], ['D1', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8', 'D9'], ['E1', 'E2', 'E3', 'E4', 'E5', 'E6', 'E7', 'E8', 'E9'], ['F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9'], ['G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7', 'G8', 'G9'], ['H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7', 'H8', 'H9'], ['I1', 'I2', 'I3', 'I4', 'I5', 'I6', 'I7', 'I8', 'I9'], ['A1', 'A2', 'A3', 'B1', 'B2', 'B3', 'C1', 'C2', 'C3'], ['A4', 'A5', 'A6', 'B4', 'B5', 'B6', 'C4', 'C5', 'C6'], ['A7', 'A8', 'A9', 'B7', 'B8', 'B9', 'C7', 'C8', 'C9'], ['D1', 'D2', 'D3', 'E1', 'E2', 'E3', 'F1', 'F2', 'F3'], ['D4', 'D5', 'D6', 'E4', 'E5', 'E6', 'F4', 'F5', 'F6'], ['D7', 'D8', 'D9', 'E7', 'E8', 'E9', 'F7', 'F8', 'F9'], ['G1', 'G2', 'G3', 'H1', 'H2', 'H3', 'I1', 'I2', 'I3'], ['G4', 'G5', 'G6', 'H4', 'H5', 'H6', 'I4', 'I5', 'I6'], ['G7', 'G8', 'G9', 'H7', 'H8', 'H9', 'I7', 'I8', 'I9']] Run
Re: Nested list comprehension
Thanks for the info. Yes, I'd tried the sugar package but failed. Will checkout the comprehension package.
Re: Nested list comprehension
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: string): seq[string] = for ac in a: for bc in b: result.add(ac & bc) Run This is some of the syntax that confuse me.
Re: Nested list comprehension
> 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) ?
Re: Nested list 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:
Re: Nested list comprehension
> 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 comprehensions in Nim — think about how would you have written your python code without them and then try to translate _that_ version, you'll have much better time.
Re: Nested list comprehension
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 in cols: unitSeq.add cross(rows, $c) for r in rows: unitSeq.add cross(cols, $r) for rs in ["ABC", "DEF", "GHI"]: (for cs in ["123", "456", "789"]: unitSeq.add cross(rs, cs)) var units, peers: Table[string, seq[string]] for s in squares: var tmp: seq[string] for u in unitSeq: (if s in u: tmp.add u) units[s] = tmp if (let i = tmp.find s; i >= 0): tmp.del i peers[s] = tmp Run
Re: Nested list comprehension
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 in squares) peers = dict((s, set(sum(units[s],[]))-set([s])) for s in squares) Run I tried with... # Nim codes: var unitlist: seq[string] = [for c in cols: cross(rows, cast[string](c))] + [for r in rows: cross(r, cols)] + [for rs in ("ABC","DEF","GHI"): for cs in ("123","456","789"): cross(rs, cs)] var units = initTable[string](4): for s in squares: for u in unitlist: if s in u: {s, u} var peers = initTable[string](4): for s in squares: {s, toHashSet(sum(units[s],[]))-toHashSet([s]) Run But encountered lots of exceptions... :( The documentation of Seq & Tables are very poor in Nim. Not many examples to learn from. Please help!
Re: Nested list comprehension
@trtt & @miran, Thanks very much. Yes, i notice I made the mistake of the return type in the proc.
Re: Nested list comprehension
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 elements in B. for x in xs: for y in ys: result.add x Run
Re: Nested list comprehension
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 = "ABCDEFGHI" echo cross(letters, digits) Run
Nested list comprehension
Hi, I'm new to Nim-lang. I wanted to convert a Python script to Nim to improve my understanding of Nim. I have the following Python code: # Python codes: def cross(A, B): """Cross product of elements in A and elements in B.""" return [a+b for a in A for b in B] digits = '123456789' rows = 'ABCDEFGHI' cols = digits squares = cross(rows, cols) Run I tried to convert to Nim code but failed: # Nim codes: proc cross(A: string, B: string): string = ## Cross product of elements in A and elements in B. result = string: for a in A: for b in B: a let digits = "123456789" let rows = "ABCDEFGHI" let cols = digits var squares = 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.