262
u/dbgprint Aug 10 '20
There is nothing wrong with this. This is perfectly valid test code for demonstrating / testing std::function and lambdas. Stop treating all code you see as production code.
68
u/ws-ilazki &{$$_[0]}(@{$$_[1]}); Aug 10 '20
Yeah this is basically someone trying to feel out how functional programming works with some test code. It's a rudimentary higher-order function and there's nothing bad about that when you're trying out FP concepts. It looks kind of gnarly because being C++ makes it a bit noisy, sure, but I don't even use the language and I can still understand what's going on. Same thing in a more FP-friendly language:
let executeif (c : bool) (f : unit -> unit) = if c then f () executeif true (fun () -> print_string "Test")It's baby's first lambda and HOF, nothing wrong with that at all. OP should feel bad for trying to make fun of someone's else's learning code because he apparently didn't understand it himself.
-30
Aug 10 '20
This isn't FP, a void function doesn't make any sense in functional programming.
22
u/ws-ilazki &{$$_[0]}(@{$$_[1]}); Aug 10 '20
executeif, by taking a function as an argument, is a higher-order function, so yes it is functional programming. FP doesn't require function purity, just the use of functions as first-class values that can be returned from or used as arguments to other functions (as is done here).Granted, it's just not a particularly effective form of it, but criticising that in a snippet of learning code is a bit unfair. It's okay for someone to start small and then work up from that to better things.
-31
Aug 10 '20
I don't know what definition of functional programming you use to justify that using higher order functions is functional programming. In my mind functional programming is programming using expressions, combinators and higher level abstractions built using the two. The
executeifhere is a procedure and that is an imperative concept, not a functional.21
u/ws-ilazki &{$$_[0]}(@{$$_[1]}); Aug 10 '20
In your mind doesn't mean shit. Everyone wants to add all this extra crap on top (like you adding combinators as a req for FP) but ultimately the whole point is that functional programming is about functions being first-class and usable like any other value, and the benefits that come from that abstraction
However, you quickly discover that the usefulness of first-class functions is pretty limited if everything you write relies entirely on side effects, so the next step is writing composable functions that use arguments as input and return values as output. From there, FP starts to become interesting and useful. So, while not technically needed, composability (your 'using expressions' requirement) is still pretty damn important for practical usage of functional programming.
Anything past that, though, is fluff. Nice to have, and often a big part of what makes functional languages interesting, but not strictly required for FP itself. So there's nothing inherently wrong with someone writing a fairly useless HOF as a first step toward more useful FP.
-28
Aug 10 '20
So what's in your mind is factually correct and what is in my mind isn't, interesting pov
19
5
u/Keroths Aug 10 '20
People are a bit agressive towards you because you sounded way too much confident while saying arguably false things.
Yes, you are right, in practice when using a functional programming language, you do use combinators, more complexe expressions, etc.. But strictly speaking this isn't FP at it's core, at it's core is higher order functions. That's because FP comes from lambda calculus.
That's also why math students (at least in France) learn OCaml a functional programming language at it's core.
3
u/Jerudo Aug 10 '20
Just another point- even in Haskell, a pure functional language, you still have
when :: Applicative f => Bool -> f () -> f ()which will conditionally execute monadic/applicative actions, which are the pure equivalent of void functions.9
8
u/CommenceTheConfusion Aug 10 '20
Agreed. This is exactly what you should be doing after learning a new concept/new syntax, testing it out with the simplest, stupidest examples before you go and see what you can build with it.
5
52
30
u/SuitableDragonfly Aug 10 '20
It looks like someone is testing out new functional features of the language? That's not bad.
22
Aug 10 '20
Only issue I have here is that the defenition for executeif() isn't closed (missing closing curly-brace)
2
u/fb39ca4 depraved Aug 11 '20
I see that and the lack of a return value from main.
1
Aug 22 '20
MSVC doesn't require you to actually write it though. I always do, since the lack of a return value just looks weird in main(), but you don't have to do it.
Also, I've never seen main() declared as auto, is there any reason you'd do that?
2
u/fb39ca4 depraved Aug 22 '20
It's using the return type after the arguments syntax so it's not actually deduced by the compiler, which would have been impossible without a return statement. Not sure why it's written that way...
1
Aug 22 '20
Yeah, I saw that int and assumed that, but I asked anyway since I didn't even know this syntax existed in C++.
16
11
u/FTC55 Aug 10 '20
Plot twist: the code was written by OP, expecially since it's very clearly a mobile text editor, which suggests it was probably him experimenting out of boredom. Assuming everyone would shame him he then posted these 10 lines on this subreddit pretending to have found the code somewhere else and hopelessly trying to sell it as bad production code.
19
u/Tvde1 Aug 10 '20
You mean the missing } on line 7?
Otherwise, this is perfectly good code
-1
u/Yeet3r__ Aug 10 '20
It’s on the end of line 6
3
0
5
u/Mr_Sky_Wanker Aug 10 '20
Where did you find it ? Seems fake to me. Also what triggers me the most is the indentation. Boo
5
5
u/j3lackfire Aug 10 '20
For me, it's not bad, the executeIf function. It saves some lines which improve readability. Especially if you have just one condition function call,
for example:
if (condition)
{
function();
}that's 4 lines, so instead, we can have with just 1 line
executeIf(condition, function);-2
u/TorbenKoehn Aug 10 '20
or
if (condition) function();?Functional patterns have a lot of advantages, readability surely is not one of them
7
u/ws-ilazki &{$$_[0]}(@{$$_[1]}); Aug 10 '20
Functional patterns have a lot of advantages, readability surely is not one of them
That's both harsh and unfair, FP can be quite good for readability because it helps you write code in a declarative style. For a trivial example, doubling every number in an array. An imperative version might look something like this:
a = [1,2,3,4,5]; for (x=0; x<a.length; x++) { a[x] = a[x] * 2 }Meanwhile, the functional equivalent describes what you're doing to the list instead of how you're doing it:
let apply = List.map (* Not necessary or even useful normally, but aliasing "map" to a more beginner-friendly name for illustration purposes here *) let double i = i * 2 apply double [1;2;3;4;5]Ignoring that the code is shorter, the idea here is that the code that does most of the work is declarative so that the intent is understood without having to dig into the specifics of how it works. You create a function named
doublethat's easy to understand: it takes a number and doubles it. Then you apply (map) that to every number in a list. Even if the underlying details aren't known (or understood), the basic concept of what is happening could be described and understood even by someone with little or no programming ability. Doing the same with the imperative loop is actually harder because the intent is buried in details about how it's getting done, like array indexing and assignment and the syntax of for loops.People just think FP has to be unreadable because the power of first-class functions goes to some people's heads (especially FP newbies) and they start throwing lambdas everywhere to avoid naming things. Naming is hard after all, so it's tempting to toss a lambda into your higher-order function instead of giving it a sane name.
2
u/TorbenKoehn Aug 10 '20
While I agree with what you said generally, I believe imperative code reads more naturally, especially in larger code bases. Taking a quick example of doubling a number is always good and cool, but big functional code bases look a lot different to your example. In functional programming sometimes the fact that you only declare and never touch the actual context or what is being processed is exactly what makes it unreadable, you simply lose the context when trying to understand code that is not yours. Surely that happens in bad imperative languages implementations, too. Just as you can name things well there just as good.
Let me get this straight before functional ideologists hit the downvote button right away, I love functional programming, I can read it, I write it a lot and I know of all the advantages. But it’s really hard to get across my team, or any bigger team for that matter, as it (in my opinion and experience) has a steeper learning curve than imperative programming, especially when solid type systems are involved.
I understand it has reasons which are hard to circumvent. Most people are used to imperative programming, unless you’ve gone declarative right from the start and never learned something else, you always try putting your imperative experience in there and it ends up making no sense at all.
I’ve been working with huge imperative code bases and huge functional code bases and while surely there was a lot of shit I’ve seen in imperative ones, I’ve also seen a lot of functional ones where even the last single ternary operator had to be an ifElse function and every single module came with its own “FP library” because “it’s chain and not flat_map” and that’s where ideology stops for me and I say, the best code base is a clean one, a well named one and one that leverages the advantages of both styles without downvoting people on Reddit because someone dared to insult the holy grail FP
1
1
u/nend0410 Aug 10 '20
To be honest the only thing that's bad about it is the formatting/indentation.
1
1
-4
0
Aug 10 '20
[deleted]
1
Aug 10 '20
is called a lambda function , aka an anonymous function that you define on-place. std::function<void()> is another way of defining a void function.
30
u/Play4u Aug 10 '20
I mean.. What's so bad about it?
I can see some applications of the function