News for hackers. Stuff that matters.
Re:Functional programming in C++ by cousin_it on Monday September 26, 2016 @11:53AM (#12582852)
Quick and dirty imperative version:

    #include <climits>

    int count_common_chars(const std::string& str) {
      int n = 0, common = 0, counts[CHAR_MAX - CHAR_MIN + 1] = {};
      for (char c : str) {
        if (c == '\n') {
          common = 0;
          n++;
        }
        if (counts[c - CHAR_MIN] == n) {
          counts[c - CHAR_MIN]++;
          common++;
        }
      }
      return common;
    }
Tastes may vary, but to my eyes the imperative version has some advantages:

* Faster

* No allocations

* No dependencies

* Easy to add an early return if common == 0 (in the functional version it's much harder)

Re:Functional programming in C++ by tzs on Monday September 26, 2016 @01:36PM (#12583841)
> counts[CHAR_MAX - CHAR_MIN]

Shouldn't that be counts[CHAR_MAX - CHAR_MIN + 1] ?

Re:Functional programming in C++ by cousin_it on Monday September 26, 2016 @01:45PM (#12583928)
Good catch, thanks!
Re:Functional programming in C++ by anthay on Tuesday September 27, 2016 @04:16PM (#12592751)
That's cool and I agree about the advantages you mention.

In your code \n must be a line separator rather than a line terminator. (Otherwise, if the last line is \n terminated, the function returns 0.)

Re:Functional programming in C++ by cousin_it on Tuesday September 27, 2016 @05:09PM (#12593238)
Yeah, fair point.
Re:Functional programming in C++ by hellofunk on Monday September 26, 2016 @09:43AM (#12581720)
Cool, but I'm left with some observations:

1) C++ already provides a transform and accumulate (just for starters) which are much like map and fold. Isn't it better to learn to use the built-in tools rather than rely on a library? There is quite a lot of functional-style stuff in the standard already.

2) The cost of these abstractions is not clear. You can do a lot of functional idioms in C++, but often a tremendous amount of copying is happening compared to an imperative solution, because C++ lacks functional data structures under the hood.

3) Points 1) and 2) above are largely addressed by proposals for C++20 specification, such as the Range library.

Re:Functional programming in C++ by BeetleB on Monday September 26, 2016 @11:21AM (#12582555)
transform in C++ is not that great. The standard does not guarantee order is preserved, which makes it virtually useless. People recommend for_each if you want to preserve the order, but it's almost the same as writing a for loop.

As for cost, I had a C++ teacher who once sat on the standards committee. His advice: Always use the algorithms library even if it "feels" like it is costlier. Reasons:

1. Much lower likelihood of having a bug.

2. Virtually every time someone decided writing an explicit solution rather than using the algorithms library, it did not perform better (even in cases where it seems "obvious" it should).

Re:Functional programming in C++ by hellofunk on Monday September 26, 2016 @12:02PM (#12582952)
> The standard does not guarantee order is preserved

Are you sure about that? The standard does not guarantee which order transform will actually run and apply the function, but that is a different matter than the order of the final result. I'm pretty sure the standard guarantees that. If you have side effects and require application order guarantees, for_each is the one to use.

edit: this SO answer from a Google engineer suggests I am correct: http://stackoverflow.com/questions/34167638/stdtransform-ord...

Re:Functional programming in C++ by BeetleB on Monday September 26, 2016 @01:03PM (#12583531)
Ah, yes, you are correct. Will now happily use transform :-)
Re:Functional programming in C++ by king_magic on Monday September 26, 2016 @11:02AM (#12582375)
"This is code I personally would love to maintain compared to what an imperative version could look like."

I think this piece would have been much more compelling if it didn't end with this line.

I personally would never want to maintain that code (in C++). I mean hey, look, I get it. Functional programming is awesome. But with all of the magic behind the scenes needed to make that work in C++, it just feels like an overly complicated, Rube Goldberg-esque solution to a relatively simple problem.

Re:Functional programming in C++ by hellofunk on Monday September 26, 2016 @10:05AM (#12581881)
For the curious, there are many other stabs at doing functional programming in C++:

https://github.com/beark/ftl

https://github.com/BartoszMilewski/Okasaki

https://github.com/fons/functional-cpp

https://cat.github.io/

There is a lot of interest in making this happen in C++, which is awesome.

Re:Functional programming in C++ by gpderetta on Monday September 26, 2016 @10:25AM (#12582037)
All great links, I just wanted to add a link to the library that started it all, FC++:

https://www.cics.umass.edu/~yannis/fc++/fc++-sigplan.pdf

Note that this the library is now obsolete, but it did introduce a lot of techniques.

Re:Functional programming in C++ by goatlover on Monday September 26, 2016 @10:47AM (#12582252)
I don't understand. What's the point? If you really want to do function programming, use a language made for it, like Haskell or OCaml. Why the need to make one language do everything. Whatever happened to using the right tool?
Re:Functional programming in C++ by HelloNurse on Monday September 26, 2016 @11:38AM (#12582715)
This is a direct demonstration that C++ is the right tool. Most good modern programming languages have sufficiently convenient syntax (in C++, function types, templates and lambdas/closures) and sufficiently advanced libraries (like the set implementation the article relies on) to allow a functional programming style, which mostly means, like in the linked article, read-only data structures and higher order functions.

For an established C++ or Python programmer, being able to "make one language do everything" is great and functional programming style is "the right tool" for some specific needs. Preferring certain "pure" languages for the purpose of doing functional programming, regardless of the domain, can be an interesting experiment but is completely backwards from a practical engineering point of view.

Re:Functional programming in C++ by AnimalMuppet on Monday September 26, 2016 @12:02PM (#12582954)
> This is a direct demonstration that C++ is the right tool.

Well, it's a demonstration that C++ can be made into an adequate tool. The right tool? That might be a bit of an overstatement...

Re:Functional programming in C++ by blub on Monday September 26, 2016 @11:22AM (#12582566)
Multi-paradigm languages like C++ (or C#, Swift, etc) are the right tool because they allow you to use different solutions depending on the project you're working on.

As you can probably tell, languages that support one paradigm aren't particularly successful. This is painfully clear in the case of F#.

Re:Functional programming in C++ by enricosada on Monday September 26, 2016 @11:37AM (#12582708)
Just curious, what are the pain points of F#?
Re:Functional programming in C++ by gumby on Monday September 26, 2016 @11:59AM (#12582919)
Consider why you might want a functional approach in the first place: perhaps you have a large dataset to manage and want to use an approach that minimizes the amount of garbage you generate. Or maybe you have a major algorithm that is simply easier to express and maintain in a functional style.

But it's part of a larger project. That project could have other constraints that make C++ a better choice (or it could be an existing codebase).

Re:Functional programming in C++ by AnimalMuppet on Monday September 26, 2016 @08:26PM (#12586743)
The amount of garbage you generate isn't a big reason to use functional in C++...
Re:Functional programming in C++ by plinkplonk on Monday September 26, 2016 @11:06AM (#12582416)
This makes as much sense as "Imperative Programming in Haskell".

Sure, it can be done, but it would go against the grain of the language (imho).

Re:Functional programming in C++ by nepstein on Monday September 26, 2016 @09:30AM (#12581618)
Nice article and effective demonstration of the FunctionalPlus library.

That said, this makes me a bit nervous about shoe-horning functional programming into contexts where it is inappropriate. FunctionalPlus aside, C++ is very clearly designed for an object oriented style (even the name, C++, refers to "C plus objects").

Going too far down the path of working against the intent of the standards committee seems like a recipe for trouble (unless you're just talking about hobby projects).

Re:Functional programming in C++ by nmeofthestate on Monday September 26, 2016 @09:33AM (#12581649)
I completely disagree - C++ is a multi-paradigm language and the C++ standard is already chock full of non-OO stuff.

The example code in the article also makes use of objects such as std::string, sets etc.

Re:Functional programming in C++ by gpderetta on Monday September 26, 2016 @10:18AM (#12581975)
In fact the only part of the standard library that can be considered OO, iostream, is considered an overengineered disaster.

I actually happen to like iostream, for the reasons of it being standard and extendible; just don't expect any particular performance out of it.

Re:Functional programming in C++ by nepstein on Monday September 26, 2016 @09:52AM (#12581788)
I'm not sure I agree that STL that containers are explicitly not OO.

That said, I don't disagree that C++ can support functional programming. I would just argue there are languages better suited for this.

(edited for clarity)

Re:Functional programming in C++ by hellofunk on Monday September 26, 2016 @09:58AM (#12581828)
The author of the first STL has famously said that, even after all these decades, he has never written any C++ code that uses OO inheritance, because he hasn't found a use for it.

The STL is mainly a generic library, not an OO library.

Re:Functional programming in C++ by gpderetta on Monday September 26, 2016 @10:40AM (#12582187)
Yes, Stepanov is not exactly an OO fan. This interview with him, were he declared that "OO is an Hoax", is great:

http://www.stlport.org/resources/StepanovUSA.html

Re:Functional programming in C++ by jmcomets on Monday September 26, 2016 @09:38AM (#12581678)
As another comment points out, C++ is already full of non-OO components (take a look at the STL's <algorithm> interface for example). Moreover, the current path of the standards committee is clearly moving away from the gool-ol' OO and closer to the newer strong typing with better generics.
Re:Functional programming in C++ by anthay on Tuesday September 27, 2016 @04:04PM (#12592643)
The Medium article has been removed. I think the same article (possibly under a different author's name?) appeared here https://github.com/Dobiasd/articles/blob/master/functional_p...
Re:Functional programming in C++ by hellofunk on Monday September 26, 2016 @09:55AM (#12581800)
take a look at one of the macros this library depends on:

https://github.com/kennytm/utils/blob/master/traits.hpp#L47-...

Wow that scares the heck out of me to do that!

Re:Functional programming in C++ by PeCaN on Monday September 26, 2016 @12:42PM (#12583346)
What? That macro is super straightforward. Yes, generating C++ templates from CPP macros is a little ugly, but it's not particularly hard to understand.

CPP macros only start to get weird once you start implementing control structures in them. http://pastebin.com/raw/tNaX99eA (Yes, that's code from a project I work on. Don't ask.)

Re:Functional programming in C++ by hellofunk on Monday September 26, 2016 @02:15PM (#12584213)
I didn't say it was hard to understand; but it does scare me to depend on any C-style macro like that in a C++ project.
Re:Functional programming in C++ by beached_whale on Monday September 26, 2016 @10:45AM (#12582224)
I've done that, usually I will put an undef after so that the macro's scope is limited.

As far as macro's go, that is not so bad and generating the exact same code over and over is asking for errors too. Not sure of how else to do something like that, but I could be ignorant.

Re:Functional programming in C++ by popcorncolonel on Monday September 26, 2016 @10:57AM (#12582339)
Why?
Re:Functional programming in C++ by wmu on Monday September 26, 2016 @03:49PM (#12584970)
Years ago I saw a WWW server written in PostScript. People were also asking "why?" :)