4 ms·
Functional looks good for applying x^2 to a list of numbers, but when you need to do something a little more complex, it becomes hard to put in in a single line
by visarga 10y ago
Functional looks good for applying x^2 to a list of numbers, but when you need to do something a little more complex, it becomes hard to put in in a single line/lambda.
- deleted 10y ago[deleted]
- hawaiianbrah 10y agoThat's fine, that's not really one of the points to functional programming.
- bhrgunatha 10y agoYou decompose the complex task into separate other tasks and compose them together the same way that you would in any other paradigm. You wouldn't expect to express a complex task in a single line in an OO program for example. documents = filter(pdf, folder) summaries = map(summarise_document, documents) Is pretty clear despite summarising a document being a complex task.
- visarga 10y agoI appreciate how nice the functional style is, but sometimes a for loop acting over a few local variables is simpler to read than a bunch of map's and lambdas. Does this look readable and nicer to you than a for loop? print pipeline_each(bands, [call(lambda x: 'Canada', 'country'), call(lambda x: x.replace('.', ''), 'name'), call(str.title, 'name'), extract_name_and_country] Sometimes it's easier to implement directly what you want done than to remember the name of the function that plucks a field from a list of records or something like that.
- caconym_ 10y agoYou seem to be obsessed with this idea that "functional programming" is a fancy term for squishing your entire program onto one line via gratuitous use of anonymous functions, which simply isn't true. I don't know what this snippet of code is supposed to do, and it's not very pretty, but guess what: it is just as easy to produce unreadable garbage in an "imperative style". I've seen a lot of both, but more of the latter.
- hskl 10y agoThat's why one should use more than a single one-line function per program.
- xyz-x 10y agoOh, it's good for so much more. Just take this refactoring of a functional test library of mine: 1. https://github.com/haf/expecto/commit/e4f2ce3cc2b2cf3c848c5e2a09ea9034d188e912 https://github.com/haf/expecto/commit/e4f2ce3cc2b2cf3c848c5e... 2. https://github.com/haf/expecto/commit/9f8380cdb17002e09ae169a0df394c869091b7e7 https://github.com/haf/expecto/commit/9f8380cdb17002e09ae169... 3. https://github.com/haf/expecto/commit/3fafd9ada4e932dcadc24a3fcb0b7098f36fbb20 https://github.com/haf/expecto/commit/3fafd9ada4e932dcadc24a... 4. https://github.com/haf/expecto/commit/0c243c9eb781e7527b0bd2357c1a867da196134d https://github.com/haf/expecto/commit/0c243c9eb781e7527b0bd2... 5. https://github.com/haf/expecto/issues/36 https://github.com/haf/expecto/issues/36 6. https://github.com/haf/expecto/commit/9284173599145a1bb08ee8ed7acd865dfa3c0429 https://github.com/haf/expecto/commit/9284173599145a1bb08ee8... Functional programming also makes for really smooth control flow and that's a huge part of programming. In short what it does, is give you the ability to compose and reason with your code.