Required Reading: "Functional Programming in C++"

Ola Fosheim Grøstad ola.fosheim.grostad+dlang at gmail.com
Sun Oct 29 09:03:51 UTC 2017


On Saturday, 28 October 2017 at 23:26:11 UTC, Jonathan M Davis 
wrote:
> The type system can help ensure the correctness of functional 
> programming (e.g. pure helps ensure that you didn't 
> accidentally do something involving global variables), but it's 
> certainly not required to write in that style.

Yes, the type-system would prevent some aspects of non-functional 
programming, but it doesn't enable functional programming…

Anyway, both C++ and D have abstraction mechanisms for building 
the tools you need to get functional-style programming. And I 
assume production backends also do tail-call optimization in D as 
well as C++.

What you don't get in either is a convenient applicative pattern 
matching syntax, and functional programming in the technical 
recursive sense is mostly not a very good idea (even with tail 
call optimization since you cannot enforce it, I think?).

Anyway, with the improvements to C++ lambdas it has become more 
attractive to do small-scale functional style programming in C++. 
So I think D and C++ are pretty much the same in that respect. (D 
has pure, but C++ has explicit capturing of references)

> And actually, most D code that's functional doesn't do much 
> with either const or pure. Most of the functional code is 
> range-based,

I guess it makes sense to call range-based programming functional 
in spirit (although not so much on a technical level, e.g. 
recursiveness). And you can do similar things with iterators, 
although the C++ stdlib does not provide much out-of-the-box.

In the context of the linked article I think maybe he is assuming 
that you should have SIMD access to the values, so maybe there is 
an underlying assumption there that libraries for 
ranges/iterators don't support SIMD well in their current 
incarnations.

Anyway, I think you can rely on RVO in  C++ in 2017, maybe not in 
2012. But the addition of constexpr to C++ has made some 
improvements to the C++ ecosystem where D used to be a little bit 
ahead.

The improvements to C++ in C++14 and C++17 are relative small, 
but as far as I am concerned, those improvements has enabled a 
more functional style of programming for initialization (small 
scale functional programming).

Ola.


More information about the Digitalmars-d mailing list