List Comprehension equivalent
I have a bit of code: string[] returnValue; foreach(string key, string[] value; groups) { returnValue ~= value.sort!debianPackageNumberComparator()[0..$-1].array; } return returnValue; which does seem to do the thing required, but this sort of code in Python, Rust, even C++ (but not Go), would be seen as for too "changing state", not declarative enough. With Python you use list comprehensions (which are lovely things), in Rust you can do flat_map and collect. I am fighting in D starting from: return groups.byPair() .map((Tuple!(string, string[]) a) => a[1].sort!debianPackageNumberComparator()[0..$-1]) .array .joiner; because the compiler keeps complaining about: ldc2 '-Isource/approx-gc@exe' '-Isource' '-I../../Repositories/Git/Masters/ApproxGC_D/source' '-enable-color' '-wi' '-O3' '-release' -of 'source/approx-gc@exe/main.d.o' -c ../../Repositories/Git/Masters/ApproxGC_D/source/main.d ../../Repositories/Git/Masters/ApproxGC_D/source/main.d(75): Error: template std.algorithm.iteration.map cannot deduce function from argument types !()(MapResult!(__lambda2, Result), SortedRange!(string[], debianPackageNumberComparator) function(Tuple!(string, string[]) a) @system), candidates are: /usr/lib/ldc/x86_64-linux-gnu/include/d/std/algorithm/iteration.d(448): std.algorithm.iteration.map(fun...) if (fun.length >= 1) Anyone any idea on how to move forward on this? -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder signature.asc Description: This is a digitally signed message part
Re: List Comprehension equivalent
On Friday, 17 March 2017 at 17:13:48 UTC, Russel Winder wrote: I have a bit of code: string[] returnValue; foreach(string key, string[] value; groups) { returnValue ~= value.sort!debianPackageNumberComparator()[0..$-1].array; } return returnValue; [...] You forgot a ! on the map call. .map!((Tuple!(string, string[]) a) => a[1].sort!debianPackageNumberComparator()[0..$-1])
Re: List Comprehension equivalent
On 03/17/2017 10:51 AM, Jerry wrote: On Friday, 17 March 2017 at 17:13:48 UTC, Russel Winder wrote: I have a bit of code: string[] returnValue; foreach(string key, string[] value; groups) { returnValue ~= value.sort!debianPackageNumberComparator()[0..$-1].array; } return returnValue; [...] You forgot a ! on the map call. .map!((Tuple!(string, string[]) a) => a[1].sort!debianPackageNumberComparator()[0..$-1]) This happens to me too. I think the compiler can generate a warning when mandatory template parameters are not provided. Submitted: https://issues.dlang.org/show_bug.cgi?id=17263 Ali
Re: List Comprehension equivalent
On Fri, 2017-03-17 at 17:51 +, Jerry via Digitalmars-d-learn wrote: > On Friday, 17 March 2017 at 17:13:48 UTC, Russel Winder wrote: > > I have a bit of code: > > > > string[] returnValue; > > foreach(string key, string[] value; groups) { > > returnValue ~= > > value.sort!debianPackageNumberComparator()[0..$-1].array; > > } > > return returnValue; > > > > [...] > > You forgot a ! on the map call. > > .map!((Tuple!(string, string[]) a) => > a[1].sort!debianPackageNumberComparator()[0..$-1]) How embarrassed am I? :-) However, the result of the map appears to be untransformable to anything related to a sequence of string. The result is some type that renders as a sequence of sequence using writeln but how to reduce it to a sequence? reduce isn't defined on MapResult and if I transform to an array reduce is not defined on SortedRange[]. Rust ownership problems seem to be a doddle compared to this problem. -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder signature.asc Description: This is a digitally signed message part
Re: List Comprehension equivalent
On Friday, 17 March 2017 at 19:08:36 UTC, Russel Winder wrote: On Fri, 2017-03-17 at 17:51 +, Jerry via Digitalmars-d-learn wrote: On Friday, 17 March 2017 at 17:13:48 UTC, Russel Winder wrote: > I have a bit of code: > > string[] returnValue; >foreach(string key, string[] value; groups) { >returnValue ~= > value.sort!debianPackageNumberComparator()[0..$-1].array; >} >return returnValue; > > [...] You forgot a ! on the map call. .map!((Tuple!(string, string[]) a) => a[1].sort!debianPackageNumberComparator()[0..$-1]) How embarrassed am I? :-) However, the result of the map appears to be untransformable to anything related to a sequence of string. The result is some type that renders as a sequence of sequence using writeln but how to reduce it to a sequence? reduce isn't defined on MapResult and if I transform to an array reduce is not defined on SortedRange[]. Rust ownership problems seem to be a doddle compared to this problem. reduce is a free function in std.algorithm. Just import it and you're away. Anyway, is this what you wanted? string[] blah(string[][string] groups) { import std.algorithm : map, joiner; import std.array : array, byPair; return groups.byPair() .map!(a => a[1].sort!debianPackageNumberComparator()[0..$-1]) .joiner .array; }
Re: List Comprehension equivalent
On Fri, 2017-03-17 at 19:21 +, John Colvin via Digitalmars-d-learn wrote: > […] > > reduce is a free function in std.algorithm. Just import it and > you're away. Anyway, is this what you wanted? > > string[] blah(string[][string] groups) > { > import std.algorithm : map, joiner; > import std.array : array, byPair; > return groups.byPair() > .map!(a => > a[1].sort!debianPackageNumberComparator()[0..$-1]) > .joiner > .array; > } Yes it is. I had failed to import joiner in previous experiments. I feel sufficiently like an idiot now, that I shall imbibe excessively of the Ricard. However on the upside the code work very declaratively. :-) -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder signature.asc Description: This is a digitally signed message part
Re: List Comprehension equivalent
On 03/17/2017 12:21 PM, John Colvin wrote: > On Friday, 17 March 2017 at 19:08:36 UTC, Russel Winder wrote: >> Rust ownership problems >> seem to be a doddle compared to this problem. It sometimes feels like that but we don't want to lose you. :) > reduce is a free function in std.algorithm. Just import it and you're > away. 'fold' (a relatively recent addition) is recommended is almost all cases because it actually works with chained range syntax. https://dlang.org/phobos/std_algorithm_iteration.html#fold Ali
Re: List Comprehension equivalent
On Fri, Mar 17, 2017 at 07:21:50PM +, John Colvin via Digitalmars-d-learn wrote: [...] > reduce is a free function in std.algorithm. Just import it and you're > away. [...] Also, there is now a variant of reduce called `fold`, that has a nicer order of parameters, i.e., you can use it in UFCS chains: myData.map!(a => transform(a)) .fold!((a,b) => a + b)(0); whereas `reduce` won't work this way because it has an incompatible order of parameters. T -- There are three kinds of people in the world: those who can count, and those who can't.
Re: List Comprehension equivalent
On Fri, 2017-03-17 at 12:29 -0700, H. S. Teoh via Digitalmars-d-learn wrote: > […] > Also, there is now a variant of reduce called `fold`, that has a > nicer > order of parameters, i.e., you can use it in UFCS chains: > > myData.map!(a => transform(a)) > .fold!((a,b) => a + b)(0); > > whereas `reduce` won't work this way because it has an incompatible > order of parameters. Is this foldr or foldl' ? -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder signature.asc Description: This is a digitally signed message part
Re: List Comprehension equivalent
On Fri, 2017-03-17 at 12:32 -0700, Ali Çehreli via Digitalmars-d-learn wrote: > On 03/17/2017 12:21 PM, John Colvin wrote: > > On Friday, 17 March 2017 at 19:08:36 UTC, Russel Winder wrote: > > >> Rust ownership problems > >> seem to be a doddle compared to this problem. > > It sometimes feels like that but we don't want to lose you. :) Rust has a lot going for it, some of it hype, agreed, that has transformed into a vibrant user community. Go also has a vibrant user community, at least in London. Even C++ has a vibrant user community in London – reinvigorated and spurred on by Phil Nash, JetBrains, and C++17. In the face of Rust, Go, and C++ activity in London, and D's failure to get any user community activity going, it is hard to not shift away from D to Rust, Go, and C++. > > reduce is a free function in std.algorithm. Just import it and > you're > > away. > > 'fold' (a relatively recent addition) is recommended is almost all > cases > because it actually works with chained range syntax. > > https://dlang.org/phobos/std_algorithm_iteration.html#fold > The whole inject/reduce/fold/foldr/foldl/foldl' naming and API definition thing is a mess. But then that is 50 years of programming language evolution for you. :-) -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder signature.asc Description: This is a digitally signed message part
Re: List Comprehension equivalent
On Saturday, 18 March 2017 at 11:09:34 UTC, Russel Winder wrote: Is this foldr or foldl' ? It's a left fold of course, having foldr by default in eager language would be awkward.
Re: List Comprehension equivalent
On Sat, 2017-03-18 at 11:38 +, thedeemon via Digitalmars-d-learn wrote: > On Saturday, 18 March 2017 at 11:09:34 UTC, Russel Winder wrote: > > Is this foldr or foldl' ? > > It's a left fold of course, having foldr by default in eager > language would be awkward. I would expect foldl, but there is no "of course" of the semantics of fold (or reduce) with any library. People have been known to implement the wrong thing. -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder signature.asc Description: This is a digitally signed message part