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

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 พ.ย. 2024

ความคิดเห็น • 78

  • @AusSkiller
    @AusSkiller หลายเดือนก่อน +53

    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 หลายเดือนก่อน +6

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

    • @1InVader1
      @1InVader1 หลายเดือนก่อน +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 หลายเดือนก่อน

      @@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 หลายเดือนก่อน

      @@jasonenns5076 So does plain old function pointers.

    • @jasonenns5076
      @jasonenns5076 27 วันที่ผ่านมา +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.

  • @semicolon2599
    @semicolon2599 หลายเดือนก่อน +30

    Love the thumbnail

  • @dyershov
    @dyershov หลายเดือนก่อน +95

    C++ is an everything language.

    • @jasonenns5076
      @jasonenns5076 หลายเดือนก่อน +11

      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 หลายเดือนก่อน

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

    • @isodoublet
      @isodoublet หลายเดือนก่อน

      @@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 หลายเดือนก่อน

      @@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 หลายเดือนก่อน

      @@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 หลายเดือนก่อน +12

    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  27 วันที่ผ่านมา +1

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

  • @davidfrischknecht8261
    @davidfrischknecht8261 หลายเดือนก่อน +17

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

    • @gumz4183
      @gumz4183 หลายเดือนก่อน +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 หลายเดือนก่อน

      @@gumz4183 seconded!

    • @aniketbisht2823
      @aniketbisht2823 หลายเดือนก่อน

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

    • @szymoniak75
      @szymoniak75 หลายเดือนก่อน +1

      ​@@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 หลายเดือนก่อน

      @@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.

  • @KhalilEstell
    @KhalilEstell หลายเดือนก่อน +10

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

  • @TheDoubleBee
    @TheDoubleBee หลายเดือนก่อน +5

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

  • @qustrolabe
    @qustrolabe หลายเดือนก่อน +18

    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 หลายเดือนก่อน

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

    • @qustrolabe
      @qustrolabe หลายเดือนก่อน +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 หลายเดือนก่อน

      @@qustrolabe thx for your excellent explanation!

  • @nopair5688
    @nopair5688 หลายเดือนก่อน +1

    How a language could be beautiful 🤗 thank you dude.

  • @Evan490BC
    @Evan490BC หลายเดือนก่อน +2

    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  27 วันที่ผ่านมา

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

  • @andreasnilsson4300
    @andreasnilsson4300 หลายเดือนก่อน

    A good summary Jason!

  • @MissNorington
    @MissNorington หลายเดือนก่อน +1

    I'm mostly functional too

  • @alskidan
    @alskidan หลายเดือนก่อน +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 หลายเดือนก่อน

      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.

  • @thesa1nt01
    @thesa1nt01 หลายเดือนก่อน +2

    I like the mene

  • @mikemironov7551
    @mikemironov7551 หลายเดือนก่อน

    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

  • @bigsbypt
    @bigsbypt หลายเดือนก่อน

    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.

  • @szaszm_
    @szaszm_ หลายเดือนก่อน +3

    jack of all trades

    • @DamianReloaded
      @DamianReloaded หลายเดือนก่อน +3

      jack++

    • @stefanalecu9532
      @stefanalecu9532 หลายเดือนก่อน

      Master of none

    • @jasonenns5076
      @jasonenns5076 27 วันที่ผ่านมา

      @@stefanalecu9532 but better than a master of one

  • @thunder852za
    @thunder852za หลายเดือนก่อน +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 หลายเดือนก่อน +1

      Smalltalk is OOP, C++ is a poser

    • @Bravo-oo9vd
      @Bravo-oo9vd หลายเดือนก่อน

      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.

  • @chrissherlock1748
    @chrissherlock1748 4 วันที่ผ่านมา

    Can it do lazy evaluation?

  • @szymoniak75
    @szymoniak75 หลายเดือนก่อน +1

    does your CLion discount work for companies or individuals only?

    • @sadunozer2241
      @sadunozer2241 หลายเดือนก่อน

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

  • @michaelmueller9635
    @michaelmueller9635 หลายเดือนก่อน

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

    • @Not.Your.Business
      @Not.Your.Business หลายเดือนก่อน

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

  • @zlucoblij
    @zlucoblij 28 วันที่ผ่านมา +1

    god how I hated the decltype...

  • @bobweiram6321
    @bobweiram6321 หลายเดือนก่อน

    C++ templates are a language within a language.

  • @01MeuCanal
    @01MeuCanal หลายเดือนก่อน

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

    • @keris3920
      @keris3920 หลายเดือนก่อน +5

      Guaranteed no side effects, I suppose.

    • @DamianReloaded
      @DamianReloaded หลายเดือนก่อน

      @@keris3920 c++ has guaranteed side effects

    • @stefanalecu9532
      @stefanalecu9532 หลายเดือนก่อน

      Not being C

    • @sofiaknyazeva
      @sofiaknyazeva หลายเดือนก่อน +1

      Monads in Haskell, pattern matching, etc.

    • @zlucoblij
      @zlucoblij 28 วันที่ผ่านมา

      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++?

  • @ujin981
    @ujin981 หลายเดือนก่อน

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

  • @happygofishing
    @happygofishing หลายเดือนก่อน +1

    functional programming is a meme

  • @OGdoCavaco
    @OGdoCavaco หลายเดือนก่อน +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 หลายเดือนก่อน

    People complain about this? Really?

    • @cppweekly
      @cppweekly  27 วันที่ผ่านมา +1

      It's definitely happened!

  • @nickst2797
    @nickst2797 หลายเดือนก่อน +5

    c++ is a mess

  • @israelgaladima292
    @israelgaladima292 หลายเดือนก่อน +1

    first

    • @downs9724
      @downs9724 หลายเดือนก่อน +1

      you will always be last