www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - List Comprehension equivalent

reply Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
I have a bit of code:

	string[] returnValue;
	foreach(string key, string[] value; groups) {
		returnValue ~=3D 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) =3D> a[1].sort!debianPackageNumberCompa=
rator()[0..$-1])
	 .array
	 .joiner;

because the compiler keeps complaining about:


ldc2=C2=A0=C2=A0'-Isource/approx-gc exe' '-Isource' '-I../../Repositories/G=
it/Masters/ApproxGC_D/source' '-enable-color' '-wi' '-O3' '-release'=C2=A0=
=C2=A0-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: templat=
e std.algorithm.iteration.map cannot deduce function from argument types !(=
)(MapResult!(__lambda2, Result), SortedRange!(string[], debianPackageNumber=
Comparator) function(Tuple!(string, string[]) a)  system), candidates are:
/usr/lib/ldc/x86_64-linux-gnu/include/d/std/algorithm/iteration.d(448):=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0std.algorithm.iteration.map(fu=
n...) if (fun.length >=3D 1)

Anyone any idea on how to move forward on this?

--=20
Russel.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder ekiga.n=
et
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
Mar 17 2017
parent reply Jerry <Kickupx gmail.com> writes:
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])
Mar 17 2017
next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Mar 17 2017
prev sibling parent reply Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Fri, 2017-03-17 at 17:51 +0000, 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:
=20
 	string[] returnValue;
 	foreach(string key, string[] value; groups) {
 		returnValue ~=3D=C2=A0
 value.sort!debianPackageNumberComparator()[0..$-1].array;
 	}
 	return returnValue;
=20
 [...]
=20 You forgot a ! on the map call. =20 .map!((Tuple!(string, string[]) a) =3D>=C2=A0 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. =20 --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Mar 17 2017
parent reply John Colvin <john.loughran.colvin gmail.com> writes:
On Friday, 17 March 2017 at 19:08:36 UTC, Russel Winder wrote:
 On Fri, 2017-03-17 at 17:51 +0000, 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; }
Mar 17 2017
next sibling parent Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Fri, 2017-03-17 at 19:21 +0000, John Colvin via Digitalmars-d-learn
wrote:
 [=E2=80=A6]
=20
 reduce is a free function in std.algorithm. Just import it and=C2=A0
 you're away. Anyway, is this what you wanted?
=20
 string[] blah(string[][string] groups)
 {
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0import std.algorithm : map, joiner;
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0import std.array : array, byPair;
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0return groups.byPair()
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0.map!(a =3D>=C2=A0
 a[1].sort!debianPackageNumberComparator()[0..$-1])
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0.joiner
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0.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. :-) --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Mar 17 2017
prev sibling next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Mar 17 2017
parent Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Fri, 2017-03-17 at 12:32 -0700, Ali =C3=87ehreli via Digitalmars-d-learn
wrote:
 On 03/17/2017 12:21 PM, John Colvin wrote:
 =C2=A0> On Friday, 17 March 2017 at 19:08:36 UTC, Russel Winder wrote:
=20
 =C2=A0>> Rust ownership problems
 =C2=A0>> seem to be a doddle compared to this problem.
=20
 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 =E2=80=93 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++.
 =C2=A0> reduce is a free function in std.algorithm. Just import it and
 you're
 =C2=A0> away.
=20
 'fold' (a relatively recent addition) is recommended is almost all
 cases=C2=A0
 because=C2=A0=C2=A0it actually works with chained range syntax.
=20
 =C2=A0=C2=A0=C2=A0https://dlang.org/phobos/std_algorithm_iteration.html#f=
old
=20
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. :-) --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Mar 18 2017
prev sibling next sibling parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Fri, Mar 17, 2017 at 07:21:50PM +0000, 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.
Mar 17 2017
prev sibling parent reply Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Fri, 2017-03-17 at 12:29 -0700, H. S. Teoh via Digitalmars-d-learn
wrote:
=20
[=E2=80=A6]
 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:
=20
 	myData.map!(a =3D> transform(a))
 	=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0.fold!((a,b) =3D> a + b)(0);
=20
 whereas `reduce` won't work this way because it has an incompatible
 order of parameters.
Is this foldr or foldl' ? --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Mar 18 2017
parent reply thedeemon <dlang thedeemon.com> writes:
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.
Mar 18 2017
parent Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sat, 2017-03-18 at 11:38 +0000, thedeemon via Digitalmars-d-learn
wrote:
 On Saturday, 18 March 2017 at 11:09:34 UTC, Russel Winder wrote:
 Is this foldr or foldl' ?
=20 It's a left fold of course, having foldr by default in eager=C2=A0 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. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Mar 18 2017