April 04, 2017
On 04/04/2017 03:29 PM, Meta wrote:
> On Tuesday, 4 April 2017 at 05:04:04 UTC, Dukc wrote:
[...]
>>     fold!"+"(1, 2, 3).writeln; //6
[...]
> However, it's still more verbose. My goal was to emulate almost exactly
> what C++ was doing by using a template so you could just write
> "fold!('+', args)"

I'm probably missing something, but `fold!"+"(args)` isn't more verbose than `fold!('+', args)`.
April 04, 2017
On Tuesday, 4 April 2017 at 13:38:57 UTC, ag0aep6g wrote:
> On 04/04/2017 03:29 PM, Meta wrote:
>> On Tuesday, 4 April 2017 at 05:04:04 UTC, Dukc wrote:
> [...]
>>>     fold!"+"(1, 2, 3).writeln; //6
> [...]
>> However, it's still more verbose. My goal was to emulate almost exactly
>> what C++ was doing by using a template so you could just write
>> "fold!('+', args)"
>
> I'm probably missing something, but `fold!"+"(args)` isn't more verbose than `fold!('+', args)`.

I mean what goes on inside fold. If you look at the C++ example it's very concise and IMO beautiful:

<typename ...Args> auto f(Args ...args) { return (0 + ... + args); }

So I wanted a solution that was about the same in terms of brevity. My first attempt was:

enum fold(string op, Args...) = mixin("Args[0] " ~ op ~ " fold!(op, Args[1..$])";

But of course this doesn't work.
April 04, 2017
On Tuesday, 4 April 2017 at 13:45:47 UTC, Meta wrote:
> I mean what goes on inside fold. If you look at the C++ example it's very concise and IMO beautiful:

Luckily that hardly matters, you just need to define one generic template for the whole program, which can even be in a library.

Btw my fold template implementation was erroneous, I forgot to mixin the operator string passed to it.
April 04, 2017
On 04/04/2017 03:45 PM, Meta wrote:
> I mean what goes on inside fold.

Ok. That's what I missed.

> If you look at the C++ example it's
> very concise and IMO beautiful:
>
> <typename ...Args> auto f(Args ...args) { return (0 + ... + args); }

But the shown `fold` implements the functionality of that C++ language feature. That is, you have to compare `fold!"+"(args)` with `(0 + ... + args)`. The C++ version may be more beautiful, but verbosity seems about the same.

I think I accidentally sent this directly to your email, too. Sorry about that; Thunderbird's "answer" buttons can be confusing for me.
April 04, 2017
On Tue, 2017-04-04 at 12:46 +0000, Xinok via Digitalmars-d wrote:
> On Tuesday, 4 April 2017 at 02:43:26 UTC, evilrat wrote:
> > String interpolation would be nice too, it would really help with readability!
> 
> This really isn't in the spirit of D and is better left to library functions which give the user far more power and flexibility. Incorporating such a feature into the language raises many questions and concerns:

Interestingly, or not, Python 3.6 introduces string interpolation even though Python already has the format function. And pythonistas are happy about this.

> * It becomes a language feature thereby further bloating the
> language and runtime
> * What if you want it to be @nogc?
> * What if you want to combine it with allocators?
> * What if you want to store the result in a particular buffer?
> * What if you want the result to be lazily evaluated?
> * What if you want an input range of chars? ... that is lazily
> evaluated?
> * What if you want to format the arguments in a specific way?
> 
> Given the ease in which such a feature can be implemented and used as a library function, I don't see interpolated strings as being a necessary feature in D.
-- 
Russel. ============================================================================= Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder@ekiga.net 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

April 04, 2017
On Tue, Apr 04, 2017 at 01:38:32AM -0700, Walter Bright via Digitalmars-d wrote:
> On 4/3/2017 11:24 AM, Meta wrote:
> > I don't know how fold expressions could be emulated in D.
> 
> http://dlang.org/phobos/std_algorithm_iteration.html#.fold

That is not what "fold expressions" mean.  Fold expressions are newly-introduced C++ syntax where you can write:

	bool b = ... && args;

(with a literal "...") and the compiler automatically expands it to
`args[0] && args[1] && args[2]` (assuming args has 3 elements).

Of course, whether or not D ought to have such a thing is up for debate. My present opinion is that D doesn't need it, because D's compile-time capabilities are already convenient enough to use without needing special syntax support from the language.


T

-- 
Customer support: the art of getting your clients to pay for your own incompetence.
April 05, 2017
On Tuesday, 4 April 2017 at 13:30:47 UTC, Meta wrote:
> On Tuesday, 4 April 2017 at 08:38:32 UTC, Walter Bright wrote:
>> http://dlang.org/phobos/std_algorithm_iteration.html#.fold
>
> Not quite the same as this is a fold over a TypeTuple/AliasSeq. You could of course do:
>
> only(args).fold!"a + b"()
>
> But the semantics are different.

To avoid the runtime loop above, we could add an overload fold(alias fun, A...)(A args). (There are already min, max that take variadic arguments in std.algorithm). I think this would be more powerful than the C++ feature as it would support any function.

To make it a little nicer, we could have binaryFun accept operators as strings, e.g. fold!"+"(args). Doing that would allow e.g. sort!">"(r).
April 06, 2017
On 03.04.2017 23:43, Meta wrote:
> On Monday, 3 April 2017 at 21:33:20 UTC, Timon Gehr wrote:
>> On 03.04.2017 20:24, Meta wrote:
>>> On Monday, 3 April 2017 at 08:53:57 UTC, Andrea Fontana wrote:
>>>> https://isocpp.org/files/papers/p0636r0.html
>>>
>>> The fold expressions and inference of constructor template args look
>>> very nice. C++ is quickly catching up to D in a lot of areas, although
>>> usually still with worse syntax. I don't know how fold expressions could
>>> be emulated in D.
>>
>> String mixins.
>
> I try to avoid string mixins if possible as they're incredibly ugly. I
> did try hacking something together with templates and string mixins,
> though, but could not get it to work. I stopped here:
>
> template fold(string op, Args...)
> {
>     static if (Args.length == 1)
>         enum fold = Args[0];
>     else
>         enum fold = mixin("Args[0] " ~ op ~ " fold!(op, Args[1..$])");
> //variable _param_2 cannot be read at compile time
> }
>
> auto f(Args...)(Args args)
> {
>     return fold!("+", args);
> }
>
> void main()
> {
>     assert(f(1, 2, 3) == 6);
> }
>
> Any suggestions as to how to get something similar working?

I usually just use:

mixin(iota(args.length).map!(i=>text("args[",i,"]")).join("+"))

Obvious and fully customizable; IMHO, the aesthetics are actually better than for the C++ example:

"0 + ... + args" ?

"0 + args" does not make sense, so why should "0 + ... + args" ?

Furthermore, the feature explicitly special-cases operators (why not allow fold over arbitrary binary functions?) and adds a few more rules to C++'s language definition.
April 06, 2017
On Tuesday, 4 April 2017 at 15:41:08 UTC, Russel Winder wrote:
> Interestingly, or not, Python 3.6 introduces string interpolation even though Python already has the format function. And pythonistas are happy about this.

I can say the redditor Python userbase wasn't. The same sentiment was repeated by every comment: "So now there's three ways to format a string that all do the same thing? Why?".
April 07, 2017
On Thu, 2017-04-06 at 21:02 +0000, Jack Stouffer via Digitalmars-d wrote:
> On Tuesday, 4 April 2017 at 15:41:08 UTC, Russel Winder wrote:
> > Interestingly, or not, Python 3.6 introduces string interpolation even though Python already has the format function. And pythonistas are happy about this.
> 
> I can say the redditor Python userbase wasn't. The same sentiment was repeated by every comment: "So now there's three ways to format a string that all do the same thing? Why?".

Different communities then. I don't do Reddit – waste of time.

Why three? There is the format function and now f-strings, that makes
two.

-- 
Russel. ============================================================================= Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder@ekiga.net 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