Functional Programming Self-Affirmations

(norikitech.com)

61 points | by napsterbr 5 hours ago ago

18 comments

  • wesselbindt 10 minutes ago

    I do not work in a functional language, but these ideas have helped me a lot anyway. The only idea here that I find less directly applicable outside purely functional languages is the "Errors as values [instead of exceptions]" one.

    On the surface, it makes complete sense, the non-locality of exceptions make them hard to reason about for the same reasons that GOTO is hard to reason about, and representing failure modes by values completely eliminates this non-locality. And in purely functional languages, that's the end of the story. But in imperative languages, we can do something like this:

      def my_effectful_function():
        if the_thing_is_bad:
          # do the failure thing
          raise Exception
          # or
          return Failure()
        return Success()
    
    and a client of this function might do something like this:

      def client_function():
        ...
        my_effectful_function()
        ...
    
    and completely ignore the failure case. Now, ignoring the failure is possible with both the exception and the failure value, but in the case of the failure value, it's much more likely to go unnoticed. The exception version is much more in line with the "let it crash" philosophy of Erlang and Elixir, and I'm not sure if the benefits of locality outweigh those of the "let it crash" philosophy.

    Have any imperative folks here successfully used the "errors as values" idea?

  • agentultra an hour ago

    These are great ideas and patterns even if you’re not doing functional programming.

    FP-first/only languages tend to push you in these directions because it makes programming with them easier.

    In languages where FP is optional, it takes discipline and sometimes charisma to follow these affirmations/patterns/principles.. but they’re worth it IMO.

    • greener_grass 43 minutes ago

      I'm not convinced that you can follow all of these outside of Functional Programming.

      How can you "Make illegal states unrepresentable" with mutable state and sequences of mutations that cannot be enforced with the type system?

      How can you do "Errors as values" at a large scale without do-notation / monads?

      How can you do "Functional core, imperative shell" without the ability to create mini DSLs and interpreters in your language?

      • solomonb 4 minutes ago

        > How can you do "Errors as values" at a large scale without do-notation / monads?

        You don't need monads for this. You just need the ability to encode your error in some term like `Either e a` and ability to eliminate those terms. In Rust for example that is the `Result` type and you use pattern matching to eliminate those terms.

      • tubthumper8 17 minutes ago

        Genuinely curious for all these follow up questions:

        Is immutability exclusive to functional programming?

        Is the ability to use data/values exclusive to functional programming?

        Are monads exclusive to functional programming?

        For discussions like this, how do we separate "it was done first in functional programming but can also be done in procedural programming" with "it cannot be followed outside of functional programming"?

  • beastman82 41 minutes ago

    Completely agree with these.

    One way to achieve "Make illegal states unrepresentable" is by using "refined" types, a.k.a. highly constrained types. There is a "refined" library in both Haskell and Scala and the "iron" library for Scala 3.

  • falcor84 2 hours ago

    I'm very disappointed. I was really hoping for something like the SRE affirmations - https://youtu.be/ia8Q51ouA_s

  • revskill an hour ago

    A class is just a function in the category of Javascript.

  • lupire 2 hours ago

    This is fine but it's just a rehash of old well-knowned stuff.

    I don't see the value of learning this stuff one random blog post at a time.

    There are many books and established blogs with long series of material to give you an education.