digitalmars.D.learn - Regarding std.array.Appender
- bearophile (3/3) Feb 29 2012 Do you know why std.array.Appender defines a "put" method instead of ove...
- Jonathan M Davis (3/5) Feb 29 2012 put is a function on output ranges, and Appender is an output range.
- Andrej Mitrovic (2/2) Feb 29 2012 Luckily you can always use alias this and overload opCatAssign. 'alias
- Jonathan M Davis (4/9) Feb 29 2012 Also, given that it doesn't define ~ (and it wouldn't really make sense ...
- bearophile (5/9) Feb 29 2012 I don't understand why that's weird.
- Adam D. Ruppe (7/10) Feb 29 2012 I agree entirely.
- Jonathan M Davis (12/27) Feb 29 2012 True, but it can't do all of the other operations that array can do eith...
- Adam D. Ruppe (13/16) Feb 29 2012 Yeah, but the one operation it replaces, ~=, can be done
- James Miller (16/33) Feb 29 2012 I can see both sides, but I'm on Adam's side here. While all the other
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (14/19) Feb 29 2012 Yes you can but whatever you put() into the array is immediately
- Jonathan M Davis (5/17) Feb 29 2012 Would you define += without defining +? Or *= without defining *? It str...
- Timon Gehr (2/19) Mar 01 2012 It is not the same thing. a=a~b has different semantics from a~=b;
- =?UTF-8?B?U8O2bmtlIEx1ZHdpZw==?= (2/19) Mar 01 2012 Consider matrix * vector and matrix *= vector for an opposite example.
- Steven Schveighoffer (6/8) Mar 05 2012 It should (in addition to put). I see you have already filed an
- Suliman (9/17) Oct 13 2015 Can I use Appender to add at end of every string my symbol?
- Suliman (3/3) Oct 13 2015 I tried to use map! but it's look like it do not work with
- John Colvin (5/8) Oct 13 2015 I suspect you don't have it imported.
- Suliman (4/12) Oct 13 2015 Thanks, you are right! Can I add with map some element before and
- Suliman (2/2) Oct 13 2015 something like: auto content = file.byLine.map!("start " ~ a=>a ~
- anonymous (3/5) Oct 13 2015 That's not how it works at all. Maybe stick to the examples of whatever
- Suliman (3/8) Oct 13 2015 Yes, I understand that it's not work exactly like I try to use
- anonymous (2/4) Oct 13 2015 Use ~ instead of ~=, like so: map!(a => "+" ~ a ~ "+")
- Suliman (2/6) Oct 13 2015 Thanks!
Do you know why std.array.Appender defines a "put" method instead of overloading the "~=" operator? Bye and thank you, bearophile
Feb 29 2012
On Wednesday, February 29, 2012 20:25:35 bearophile wrote:Do you know why std.array.Appender defines a "put" method instead of overloading the "~=" operator?put is a function on output ranges, and Appender is an output range. - Jonathan M Davis
Feb 29 2012
Luckily you can always use alias this and overload opCatAssign. 'alias this' is a great tool for customizing APIs. :)
Feb 29 2012
On Wednesday, February 29, 2012 20:53:04 Jonathan M Davis wrote:On Wednesday, February 29, 2012 20:25:35 bearophile wrote:Also, given that it doesn't define ~ (and it wouldn't really make sense for it to), it would be very weird IMHO to define ~=. - Jonathan M DavisDo you know why std.array.Appender defines a "put" method instead of overloading the "~=" operator?put is a function on output ranges, and Appender is an output range.
Feb 29 2012
Jonathan M Davis:I don't understand why that's weird. In Java you can't overload an append operator, so using a method is right. But for me it's weird that Appender doesn't use the D operator to _append_. I sometimes use "add" instead of "put" by mistake, forgetting the right method name, because I find it quite unnatural. If Appender needs a put, then I suggest to give it both "put" method and "~=" operator. Bye, bearophileput is a function on output ranges, and Appender is an output range.Also, given that it doesn't define ~ (and it wouldn't really make sense for it to), it would be very weird IMHO to define ~=.
Feb 29 2012
On Thursday, 1 March 2012 at 02:23:55 UTC, bearophile wrote:But for me it's weird that Appender doesn't use the D operator to _append_. [...] I suggest to give it both "put" method and "~=" operator.I agree entirely. Another annoyance is if you have a function that works on regular arrays, you probably used ~=. But you decide to switch to Appender to try for a speed boost. Now you have to change all the usage too, since the interfaces are incompatible!
Feb 29 2012
On Thursday, March 01, 2012 03:29:06 Adam D. Ruppe wrote:On Thursday, 1 March 2012 at 02:23:55 UTC, bearophile wrote:True, but it can't do all of the other operations that array can do either. It's an output range, not an array. And odds are that it's going to be refactored such that it doesn't even contain an array internally anymore, beacause that's an inefficient way to implement appending. Someone (Robert Jacques IIRC, but I'd have to check) has already created such an implementation, and there's a decent chance that it's going to make it into Phobos. So, I'm not sure that treating Appender as an array is really a good idea in the first place. If you want the truly generic approach, then treat is an output range. - Jonathan M DavisBut for me it's weird that Appender doesn't use the D operator to _append_. [...] I suggest to give it both "put" method and "~=" operator.I agree entirely. Another annoyance is if you have a function that works on regular arrays, you probably used ~=. But you decide to switch to Appender to try for a speed boost. Now you have to change all the usage too, since the interfaces are incompatible!
Feb 29 2012
On Thursday, 1 March 2012 at 02:44:35 UTC, Jonathan M Davis wrote:True, but it can't do all of the other operations that array can do either.Yeah, but the one operation it replaces, ~=, can be done on an array. If you're trying to convert array code to Appender for speed, most likely you're going to be replacing a bunch of ~= calls. It's ok if the other op don't compile, but this one really should. Appender, regardless of the internal representation vs array is a speed optimization; an implementation detail.It's an output range, not an array.It's also an Appender, though. I think it is a little silly to have an Appender to which you can't /append/. (put is great too, don't get me wrong, but so is ~=).
Feb 29 2012
On 1 March 2012 15:49, Adam D. Ruppe <destructionator gmail.com> wrote:On Thursday, 1 March 2012 at 02:44:35 UTC, Jonathan M Davis wrote:I can see both sides, but I'm on Adam's side here. While all the other opOpAssign functions are defined in terms of their opBinary equivalent (e.g, += is 'add and assign'), ~= is essentially an operator in its own right, specifically an append (as opposed to '~' which is concatenate). Having both ~= and .put() would be fine, and would make switching from arrays to Appenders much easier. I understand that Appenders aren't arrays, and should not be used as such, but you /can/ use an array as an Appender. At some point, you have to concede design purity to convenience, otherwise you have a language that is actively hostile to changing designs. *In best "nagging wife" voice* "You should have used an Appender from the start. Now you have to go change all that code. Its your own fault really."... -- James MillerTrue, but it can't do all of the other operations that array can do either.Yeah, but the one operation it replaces, ~=, can be done on an array. If you're trying to convert array code to Appender for speed, most likely you're going to be replacing a bunch of ~= calls. It's ok if the other op don't compile, but this one really should. Appender, regardless of the internal representation vs array is a speed optimization; an implementation detail.It's an output range, not an array.It's also an Appender, though. I think it is a little silly to have an Appender to which you can't /append/. (put is great too, don't get me wrong, but so is ~=).
Feb 29 2012
On 02/29/2012 08:28 PM, James Miller wrote:I understand that Appenders aren't arrays, and should not be used as such, but you /can/ use an array as an Appender.Yes you can but whatever you put() into the array is immediately popFront()'ed from the array. ;) You must use a temporary surrogate slice: import std.stdio; import std.range; void main() { int[] array = [ 1, 2, 3 ]; int[] slice = array; put(slice, 100); // <-- slice shrinks! :) } slice.length is 2 and array.length is 3.At some point, you have to concede design purity to convenience, otherwise you have a language that is actively hostile to changing designs.Agreed. Ali
Feb 29 2012
On Wednesday, February 29, 2012 21:23:54 bearophile wrote:Jonathan M Davis:Would you define += without defining +? Or *= without defining *? It strikes me as a misuse of operator overloading if you have an opOpAssign without its corresponding opBinary. - Jonathan M DavisI don't understand why that's weird. In Java you can't overload an append operator, so using a method is right. But for me it's weird that Appender doesn't use the D operator to _append_. I sometimes use "add" instead of "put" by mistake, forgetting the right method name, because I find it quite unnatural. If Appender needs a put, then I suggest to give it both "put" method and "~=" operator.put is a function on output ranges, and Appender is an output range.Also, given that it doesn't define ~ (and it wouldn't really make sense for it to), it would be very weird IMHO to define ~=.
Feb 29 2012
On 03/01/2012 03:40 AM, Jonathan M Davis wrote:On Wednesday, February 29, 2012 21:23:54 bearophile wrote:It is not the same thing. a=a~b has different semantics from a~=b;Jonathan M Davis:Would you define += without defining +? Or *= without defining *? It strikes me as a misuse of operator overloading if you have an opOpAssign without its corresponding opBinary. - Jonathan M DavisI don't understand why that's weird. In Java you can't overload an append operator, so using a method is right. But for me it's weird that Appender doesn't use the D operator to _append_. I sometimes use "add" instead of "put" by mistake, forgetting the right method name, because I find it quite unnatural. If Appender needs a put, then I suggest to give it both "put" method and "~=" operator.put is a function on output ranges, and Appender is an output range.Also, given that it doesn't define ~ (and it wouldn't really make sense for it to), it would be very weird IMHO to define ~=.
Mar 01 2012
Am 01.03.2012 03:40, schrieb Jonathan M Davis:On Wednesday, February 29, 2012 21:23:54 bearophile wrote:Consider matrix * vector and matrix *= vector for an opposite example.Jonathan M Davis:Would you define += without defining +? Or *= without defining *? It strikes me as a misuse of operator overloading if you have an opOpAssign without its corresponding opBinary. - Jonathan M DavisI don't understand why that's weird. In Java you can't overload an append operator, so using a method is right. But for me it's weird that Appender doesn't use the D operator to _append_. I sometimes use "add" instead of "put" by mistake, forgetting the right method name, because I find it quite unnatural. If Appender needs a put, then I suggest to give it both "put" method and "~=" operator.put is a function on output ranges, and Appender is an output range.Also, given that it doesn't define ~ (and it wouldn't really make sense for it to), it would be very weird IMHO to define ~=.
Mar 01 2012
On Wed, 29 Feb 2012 20:25:35 -0500, bearophile <bearophileHUGS lycos.com> wrote:Do you know why std.array.Appender defines a "put" method instead of overloading the "~=" operator?It should (in addition to put). I see you have already filed an enhancement. http://d.puremagic.com/issues/show_bug.cgi?id=4287 -Steve
Mar 05 2012
On Monday, 5 March 2012 at 15:35:59 UTC, Steven Schveighoffer wrote:On Wed, 29 Feb 2012 20:25:35 -0500, bearophile <bearophileHUGS lycos.com> wrote:Can I use Appender to add at end of every string my symbol? foreach (pointsfile; pointsFiles) { File file = File(pointsfile, "r"); string content = file.byLine; // I need to add "+" at at end of every string }Do you know why std.array.Appender defines a "put" method instead of overloading the "~=" operator?It should (in addition to put). I see you have already filed an enhancement. http://d.puremagic.com/issues/show_bug.cgi?id=4287 -Steve
Oct 13 2015
I tried to use map! but it's look like it do not work with string, becouse I got error: Error: no property 'map' for type 'ByLine!(char, char)'
Oct 13 2015
On Tuesday, 13 October 2015 at 13:21:54 UTC, Suliman wrote:I tried to use map! but it's look like it do not work with string, becouse I got error: Error: no property 'map' for type 'ByLine!(char, char)'I suspect you don't have it imported. import std.algorithm; or import std.algorithm : map;
Oct 13 2015
On Tuesday, 13 October 2015 at 13:34:02 UTC, John Colvin wrote:On Tuesday, 13 October 2015 at 13:21:54 UTC, Suliman wrote:Thanks, you are right! Can I add with map some element before and after string? map!(a=> a~=" +") work fine, but how to add before at same time?I tried to use map! but it's look like it do not work with string, becouse I got error: Error: no property 'map' for type 'ByLine!(char, char)'I suspect you don't have it imported. import std.algorithm; or import std.algorithm : map;
Oct 13 2015
something like: auto content = file.byLine.map!("start " ~ a=>a ~ " end");
Oct 13 2015
On Tuesday 13 October 2015 15:47, Suliman wrote:something like: auto content = file.byLine.map!("start " ~ a=>a ~ " end");That's not how it works at all. Maybe stick to the examples of whatever resource you're learning from, for now.
Oct 13 2015
On Tuesday, 13 October 2015 at 13:55:07 UTC, anonymous wrote:On Tuesday 13 October 2015 15:47, Suliman wrote:Yes, I understand that it's not work exactly like I try to use it, but which function can solve my problem?something like: auto content = file.byLine.map!("start " ~ a=>a ~ " end");That's not how it works at all. Maybe stick to the examples of whatever resource you're learning from, for now.
Oct 13 2015
On Tuesday 13 October 2015 15:42, Suliman wrote:map!(a=> a~=" +") work fine, but how to add before at same time?Use ~ instead of ~=, like so: map!(a => "+" ~ a ~ "+")
Oct 13 2015
On Tuesday, 13 October 2015 at 13:51:50 UTC, anonymous wrote:On Tuesday 13 October 2015 15:42, Suliman wrote:Thanks!map!(a=> a~=" +") work fine, but how to add before at same time?Use ~ instead of ~=, like so: map!(a => "+" ~ a ~ "+")
Oct 13 2015