Live data from Hacker News

What's functional programming all about? (2017)

lihaoyi.com

81–90 of 158 posts

Re: What's functional programming all about? (2017)

#81
post #2

I'm personally a fan of FP. It offers clear benefits: simplified parallelization, improved testability, and reduced side effects. These advantages often lead to more maintainable and robust code. However, FP's benefits can be overstated, especially for complex real-world systems. These systems frequently have non-unidirectional dependencies that create challenges in FP. For example, when component A depends on B, but…

It offers none of those things and provably doesn’t have more robust code.

Maintainable code? I dunno. From what I’ve seen, FP is much worse for changing when you have shit deep in your call stack. I wouldn’t call that maintainable.

More readable? Nah. Nearly everyone has a much easier time consume code when they’re not constantly context switching between function jumps all over the place.

Additionally, FP only makes threading “easier” in one very specific circumstance. Once you need to parallelize the same workload, FP is generally much much harder to thread, whereas this is trivial in other languages.

Re: What's functional programming all about? (2017)

#82
post #58

> Anything vertically separated can be done in parallel. This assumes no contention on a limited number of bowls or having only one kitchen tool for beating or whisking etc. :P I point that out not to demand that the metaphor be bulletproof, but because I think it may help explore something about state-handling and the FP/imperative split. How might this tiramisu-tree model change if we were limited to N bowls and 1…

Sounds like you need a Kitchen Monad!

Re: What's functional programming all about? (2017)

#83
post #74
post #68

Earlier quoted context omitted.

There is an interesting trend where things with a strong mathematical definition tend to have the advantage. "Functional Programming" doesn't have a precise definition at all as far as I know, so it is likely it doesn't refer to a real thing. Most of the paradigms are similar as they don't seem to actually mean anything. People seem to want to describe something (in today's article, data flow) but they don't quite ha…

It's arguable that SQL is "basically applying and composing functions," given that its mathematical underpinnings lie in the relational algebra (hence relational databases). Further, while it's maybe technically correct to make statements such as, all programming languages are based on lambda calculus, a universal model of computation [1], this is about as precise a statement as saying that all mathematics is based o…

The distinguishing characteristics don't distinguish FP from anything. Those are features like first class functions that I would expect to find support for in Python or C++, for example. And it isn't possible to write a useful program that doesn't have side effects because there would be no IO.

It is a case of the paradigm not really meaning anything. It is an arbitrary bunching of language features and code properties that doesn't provide a particularly useful guide. Anything that is a good idea in one paradigm is also a good idea in any other paradigm too.

Re: What's functional programming all about? (2017)

#84
post #71
post #70

Earlier quoted context omitted.

It's funny you say that because I'd say FP has a much more well defined mathematical foundation compared to OOP. In fact, before it became as dominant as it is today, it was often criticized as being too scholarly and theoretical. It's built mostly on the foundations of Lambda Calculus (and maybe some Type Theory). I don't think OOP ever had nearly as strong of a mathematical/scholarly foundation as FP has

> It's built mostly on the foundations of Lambda Calculus (and maybe some Type Theory). Can you describe a programming language that isn't based on lambda calculus? It is a universal model of computation.

In that sense, every programming language (as well as lambda calculus) is based on Java, since Java is a universal model of computation. So I don't think it's very useful to take "is based on" to be as broad as "is interpretable using".

Re: What's functional programming all about? (2017)

#85

Missed the key point: FP serves to make someone feel like they're smarter than someone else.

Sounds like a take from an individual who a) does not see the benefit or b) just does not get it.

I have refactored large enterprise systems, and always tend to write in an FP style depending on the language. Im not talking about full blown Haskell here, but traditional C like languages.

Just keeping it simple, avoiding mutation, isolate side effects and having pure functions will take you a LONG way for better software.

You dont have to go balls deep in category theory for FP, and usually the language you use is not really suited for a monadic way of things, i dont like to shoehorn that stuff in if im not working with something like ocaml etc.

Re: What's functional programming all about? (2017)

#86
post #83
post #74

Earlier quoted context omitted.

It's arguable that SQL is "basically applying and composing functions," given that its mathematical underpinnings lie in the relational algebra (hence relational databases). Further, while it's maybe technically correct to make statements such as, all programming languages are based on lambda calculus, a universal model of computation [1], this is about as precise a statement as saying that all mathematics is based o…

The distinguishing characteristics don't distinguish FP from anything. Those are features like first class functions that I would expect to find support for in Python or C++, for example. And it isn't possible to write a useful program that doesn't have side effects because there would be no IO. It is a case of the paradigm not really meaning anything. It is an arbitrary bunching of language features and code propert…

It's worth noting that both languages cited (Python, C++) are multi-paradigm and therefore contain aspects of both OOP and FP; there are very few languages that are exclusively object-oriented or functional. The naming of the functools [0] stdlib package for Python's higher-order functions is illustrative in this respect, and there is an entire HOWTO for "implementing programs in a functional style" [1] in the Python docs.

If you want to see object-orientation taken to the extreme, and how it makes writing programs in a functional style extremely unergonomic, you can read old-school Guava code from Java 6 and try to decipher all the line noise; there is even a caveat in its official documentation [2] that states,

    functional programming without Java 8+ requires awkward and verbose use of anonymous classes.

    Excessive use of Guava's functional programming idioms can lead to verbose, confusing, unreadable,
    and inefficient code. These are by far the most easily (and most commonly) abused parts of Guava,
    and when you go to preposterous lengths to make your code "a one-liner," the Guava team weeps.
> It is a case of the paradigm not really meaning anything. It is an arbitrary bunching

This is a case of throwing the baby out with the bathwater; just because we can't come up with a perfectly precise definition doesn't mean that we should dispense with rough heuristics and (perhaps crudely drawn) boundaries that serve as a useful clustering of related features. Try asking a machine learning or computer vision algorithm for the criteria it uses to categorize something as a "dog;" it's unlikely that you'll come up with something as precise as the definition of the Platonic ideal of a dog, but we can still use its model to recognize dogs with a reasonable degree of accuracy.

[0] https://docs.python.org/3/library/functools.html

[1] https://docs.python.org/3/howto/functional.html#small-functi...

[2] https://github.com/google/guava/wiki/FunctionalExplained

Re: What's functional programming all about? (2017)

#87
post #69

It’s hard to characterize what fp is. A lot of people think fp is a bunch of techniques like map, reduce, immutability or high level functions or monads. None of those things are exclusive to fp. They are just tricks developed by fp. Here’s how to get a high level characterization of what fp actually is: You know how in math and physics they have formulas? Formulas for motion, for area, etc. Functional programming is…

> Functional programming is about finding the formula for a particular program. Can be disproved trivially. Anyone can code up a program that generates the nth prime for some n, by iteratively accumulating n primes starting from 2 using trial division. otoh, an actual formula that produces the nth prime would be an earth shaking event.

There exist many formulas that encode the program you mentioned or other sieving methods, so it is unclear what you mean by "actual formula". See https://en.wikipedia.org/wiki/Formula_for_primes

Re: What's functional programming all about? (2017)

#88
post #2

I'm personally a fan of FP. It offers clear benefits: simplified parallelization, improved testability, and reduced side effects. These advantages often lead to more maintainable and robust code. However, FP's benefits can be overstated, especially for complex real-world systems. These systems frequently have non-unidirectional dependencies that create challenges in FP. For example, when component A depends on B, but…

I don't know exactly what you are trying model with this hypothetical circular dependency.

However, circular dependencies can be represented with lazy (aka non-strict) references and deferred function calls (aka thunks / call-by-name), and are IMO easier to reason about than mutable imperative techniques for representing such relationships. They also have the advantage of being totally thread-safe.

The OP (Lihaoyi) is the author of an important set of Scala libraries, and Scala is an example of what you are describing. Scala is a hybrid OO / FP language that is not dogmatic about purity. You can be totally imperative if you want.

It is common in the Scala ecosystem to implement performance-critical library code using local mutability and null references. Internally the function is imperative; to the caller, it is functionally pure.

Re: What's functional programming all about? (2017)

#89

Earlier quoted context omitted.

this is wrong! ocaml is a functional programming language with side effects.

To be fair, OCaml is multi-paradigm.

i mean, then we get into defining multi-paradigm :P does it have objects that could, theoretically, be used in a java-like object oriented system? yes! does anybody do that? not really! it's far more functional than any other paradigm, and that's what counts.
Post reply on HN