C++ Weekly - Ep 450 - C++ is a Functional Programming Language

Поділитися
Вставка
  • Опубліковано 24 гру 2024

КОМЕНТАРІ • 80

  • @AusSkiller
    @AusSkiller 2 місяці тому +55

    This is another reason I love C++ so much, I can use it in the way that makes the most sense for what I'm doing, I'm not locked into doing things a specific way for arbitrary reasons, I can choose which is the best way to write code based on the code I'm writing, getting additional performance in some places or a more user friendly interface in others (and to be clear OO isn't always the more user friendly interface and functional programming isn't always the most performant).

    • @jasonenns5076
      @jasonenns5076 2 місяці тому +6

      Yup. Lambdas, std::bind, and std::function really comes in clutch when working with callbacks.

    • @1InVader1
      @1InVader1 2 місяці тому +1

      And that versatility goes right outa window when the project lead throws his coding standard at you. I for one like having top to bottom, left to right, readable code, without having to jump between parentheses.

    • @AusSkiller
      @AusSkiller 2 місяці тому

      @@1InVader1 I've always viewed coding standards as the guidelines for your first pass. I follow them until the code works, but then if the interface is too convoluted or the code doesn't perform as needed I ask the lead to let me improve it by breaking the standard. A good lead will listen to you and sometimes allow you to break the standard when it is beneficial to do so, but sometimes it is also worth sticking to the standard for consistency over marginal improvements to usability or performance, so I'm happy to comply with whatever the lead decides.

    • @skilz8098
      @skilz8098 2 місяці тому

      @@jasonenns5076 So does plain old function pointers.

    • @jasonenns5076
      @jasonenns5076 Місяць тому +1

      @@skilz8098 The problem is holding state... So a generic callable struct (pretty much a lambda) is superior. So you either have a normal function that you convert to a std::function after using std::bind or you use globals... Which are yucky. Unless you know how to do this with function pointers without std::bind.

  • @dyershov
    @dyershov 2 місяці тому +98

    C++ is an everything language.

    • @jasonenns5076
      @jasonenns5076 2 місяці тому +12

      Yes. That's why everyone who hates on it mentions: C++ tries to do too much. Which is the point of C++. And that is why I love it.

    • @stefanalecu9532
      @stefanalecu9532 2 місяці тому

      ​@@jasonenns5076 how's that a good thing? Jack of all trades, master of none

    • @isodoublet
      @isodoublet 2 місяці тому

      @@stefanalecu9532 It's a good thing because it gives you power to use whatever makes the most sense for a given situation. Sometimes writing OO code makes sense. Sometimes writing FP code makes sense. Pick Java and you're stuck with OO; pick Haskell and you're stuck with functional. But there's no reason why you can't have both.
      Recently there's even been a broad trend to add functional features to otherwise OO languages (particularly C#, but Java too), precisely because it's been recognized that FP together with OO data modeling is a very powerful paradigm. And Cpp has been ahead of the curve here.

    • @isodoublet
      @isodoublet 2 місяці тому

      @@stefanalecu9532 Because it gives you power to use whatever makes the most sense for a given situation. Sometimes writing OO code makes sense. Sometimes writing FP code makes sense. Pick Java and you're stuck with OO; pick Haskell and you're stuck with functional. But there's no reason why you can't have both.
      Recently there's even been a broad trend to add functional features to otherwise OO languages (particularly C#, but Java too), precisely because it's been recognized that FP together with OO data modeling is a very powerful paradigm. And Cpp has been ahead of the curve here.

    • @isodoublet
      @isodoublet 2 місяці тому

      @@stefanalecu9532 It's a good thing because it gives you power to use whatever makes the most sense for a given situation. Sometimes writing OO code makes sense. Sometimes writing FP code makes sense. Pick Java and you're stuck with OO; pick Haskell and you're stuck with functional. But there's no reason why you can't have both.
      Recently there's even been a broad trend to add functional features to otherwise OO languages (particularly CSharp, but Java too), precisely because it's been recognized that FP together with OO data modeling is a very powerful paradigm. And Cpp has been ahead of the curve here.

  • @UtahCppProgrammers
    @UtahCppProgrammers 2 місяці тому +15

    Ivan Cukic wrote a really good book "Functional Programming in C++" in 2019 that covers how to approach true functional programming in C++ by creating copy-on-write variants of vectors and other data types so that you can work from a true functional perspective of "no side effects". It's a good read, strong recommend from me.

    • @cppweekly
      @cppweekly  Місяць тому +3

      I even own this book. If I were smarter I would have referenced it in the video.

  • @semicolon2599
    @semicolon2599 2 місяці тому +31

    Love the thumbnail

  • @davidfrischknecht8261
    @davidfrischknecht8261 2 місяці тому +19

    C++ is a the Swiss Army Knife of programming languages.

    • @gumz4183
      @gumz4183 2 місяці тому +2

      This is a terrible analogy. A swiss knife is composed of multiple mediocre tools that just about get the job done in exchange for convenience.
      Individual versions of those tools are always better.
      There is no logic in comparing C++ to a swiss knife.
      In a majority of cases, using C++ is neither convenient nor worse than using other tools for the job.

    • @boycefenn
      @boycefenn 2 місяці тому

      @@gumz4183 seconded!

    • @aniketbisht2823
      @aniketbisht2823 2 місяці тому

      ​@@gumz4183 So C++ is not convenient, but it's great for doing jobs. Non-convenience would mostly mean "slightly" verbose.

    • @szymoniak75
      @szymoniak75 2 місяці тому +2

      ​@@gumz4183it's not a "terrible" analogy, it's just not super accurate, as analogies tend to be... what david meant was that it can do "everything", similar to how the swiss army knife can do "everything"

    • @gumz4183
      @gumz4183 2 місяці тому

      @@szymoniak75 it's a terrible analogy. Because C++ isn't 'the' swiss army knife of programming. There are better contenders for that. It's a terrible analogy and you're reasoning is quite bad too.

  • @qustrolabe
    @qustrolabe 2 місяці тому +20

    This episode doesn't feel complete without mention of monadic operations for std::optional from C++23 and coming std::optional ranges support in C++26, so I hope there some episodes on that topic coming in the future. As for C++26 you can already try out optional26 from beman-project that is supported by godbolt, although no constexpr yet

    • @bsdooby
      @bsdooby 2 місяці тому

      which monadic operations ? Dear to give some simple example(s)?

    • @qustrolabe
      @qustrolabe 2 місяці тому +2

      @@bsdooby std::optional page on cppreference got section that called Monadic operations and there 3 of them listed. It's basically just functions that take callable as parameter and based on either result of that callable or optional itself it does something.
      For example and_then would call callable only if optional itself has some value. The whole idea is that you can chain these and_then, or_else in a way that's very similar to how we can now chain ranges operations with | piping. Check code example for and_then on cppreference.
      There also great CppCon 23 talk about monads which explains all that with good examples and even does good explanation of 'monad' is

    • @bsdooby
      @bsdooby 2 місяці тому

      @@qustrolabe thx for your excellent explanation!

  • @KhalilEstell
    @KhalilEstell 2 місяці тому +11

    I did, in fact, like the meme at the start :D

  • @Evan490BC
    @Evan490BC 2 місяці тому +4

    C++ is a pretty good functional programming language. It even supports algebraic data types (product and sum types). What it lacks in order to be (even more) useful is standardised pattern matching (type matching).

    • @cppweekly
      @cppweekly  Місяць тому +1

      I do hope we get a good version of pattern matching in soon.

  • @TheDoubleBee
    @TheDoubleBee 2 місяці тому +5

    I'm generally not a fan of functional programming approach, but these helpers in C++ do come in handy from time to time

  • @thunder852za
    @thunder852za 2 місяці тому +18

    Never really understood why people say c++ is OOP? For me its never been either really. Its all about how you use it and implement your code back. Nothing is forced on you.

    • @stefanalecu9532
      @stefanalecu9532 2 місяці тому +1

      Smalltalk is OOP, C++ is a poser

    • @Bravo-oo9vd
      @Bravo-oo9vd 2 місяці тому

      AFAIK that's because initially the motivation for C++ was to have C with classes (it was even called that), but as we all learned OOP isn't the perfect solution for any problem, various languages, C++ included, became more multi-paradigm and provided very nice functional features in later versions. But still some people write in a very old-school C++ style, which can be either like C, or like C with classes.

  • @alskidan
    @alskidan 2 місяці тому +5

    IDK, for me functional means one thing: functions as data and data as functions in a LISP-y sense. C++ seems to be a far way from being functional that way.

    • @tonydelamancha5513
      @tonydelamancha5513 2 місяці тому

      callables are now std::functions that can either bind to certain parameters, or even be mutable to adjust captured lambda parameters. so i think there’s more here that jason will probably cover later.

  • @bigsbypt
    @bigsbypt 2 місяці тому

    I'd say that not allowing nulls and forcing, at compile time, that the code all paths, e.g., switches on enums, is way more relevant for a language to be functional than monads.

  • @nopair5688
    @nopair5688 2 місяці тому +1

    How a language could be beautiful 🤗 thank you dude.

  • @mikemironov7551
    @mikemironov7551 2 місяці тому

    Fun exercise you can do before making such bold claims about cpp, is to replace lambda in your examples to closure and pass it as template argument.
    Its so fun to use in implementing something like arena-allocators
    fun fun fun

  • @MissNorington
    @MissNorington 2 місяці тому +1

    I'm mostly functional too

  • @andreasnilsson4300
    @andreasnilsson4300 2 місяці тому

    A good summary Jason!

  • @szymoniak75
    @szymoniak75 2 місяці тому +1

    does your CLion discount work for companies or individuals only?

    • @sadunozer2241
      @sadunozer2241 2 місяці тому

      Get the full suite, it gets drastically cheaper after a few years ( though personal ) I assume similar adv for corpos

  • @chrissherlock1748
    @chrissherlock1748 Місяць тому

    Can it do lazy evaluation?

    • @cppweekly
      @cppweekly  21 день тому

      I mean, you can wrap your function objects in things like std::future so that it doesn't execute until it needs to...

  • @zlucoblij
    @zlucoblij 2 місяці тому +1

    god how I hated the decltype...

  • @Man836-b4r
    @Man836-b4r 2 місяці тому +1

    Thank you.

  • @thesa1nt01
    @thesa1nt01 2 місяці тому +2

    I like the mene

  • @Pesquisando0b1011
    @Pesquisando0b1011 2 місяці тому

    Cool video. Now I wonder, what a strict functional programming language has that C++ has not?

    • @keris3920
      @keris3920 2 місяці тому +6

      Guaranteed no side effects, I suppose.

    • @DamianReloaded
      @DamianReloaded 2 місяці тому

      @@keris3920 c++ has guaranteed side effects

    • @stefanalecu9532
      @stefanalecu9532 2 місяці тому

      Not being C

    • @sofiaknyazeva
      @sofiaknyazeva 2 місяці тому +1

      Monads in Haskell, pattern matching, etc.

    • @zlucoblij
      @zlucoblij 2 місяці тому

      I vaguely remember F# could do fully transparent copy on write to make all the passing data around chains of functions more performant. I'm sure you can do that in C++ but I'm guessing you would have to implement your own data structures for it?
      Also, is anything truly immutable in C++?

  • @szaszm_
    @szaszm_ 2 місяці тому +3

    jack of all trades

  • @bobweiram6321
    @bobweiram6321 2 місяці тому

    C++ templates are a language within a language.

  • @ujin981
    @ujin981 2 місяці тому

    if passing around callables constitutes a functional language, then C had it since at least shipping qsort, but that's not the point.

  • @michaelmueller9635
    @michaelmueller9635 2 місяці тому

    c++ is everything. Remember them good old times with the "c++ is java" vibes. Nowdays maybe Rust.

    • @Not.Your.Business
      @Not.Your.Business 2 місяці тому

      I might be misremembering, but wasn't that "c# is java" instead?

  • @happygofishing
    @happygofishing 2 місяці тому +1

    functional programming is a meme

  • @OGdoCavaco
    @OGdoCavaco 2 місяці тому +5

    C++ has had functional programming features for some time. A kinda of crappy functional programming language, but nonetheless, still a functional programming languange

  • @ruadeil_zabelin
    @ruadeil_zabelin 2 місяці тому

    People complain about this? Really?

    • @cppweekly
      @cppweekly  Місяць тому +1

      It's definitely happened!

  • @nickst2797
    @nickst2797 2 місяці тому +5

    c++ is a mess

  • @israelgaladima292
    @israelgaladima292 2 місяці тому +1

    first

    • @downs9724
      @downs9724 2 місяці тому +1

      you will always be last