4 ms·
When did method chaining become 'functional programming'? It's not 'functional programming' that makes the code unreadable, but overly long chains of array-pro
by flohofwoe 1y ago
When did method chaining become 'functional programming'?
It's not 'functional programming' that makes the code unreadable, but overly long chains of array-processing functions. Sometimes a simple for-loop which puts all operations that need to happen on an array item into the loop-body is indeed much more readable.
- jolux 1y ago> When did method chaining become 'functional programming'? It's very similar to applicative style in FP. Conceptually, method chaining is equivalent to nested function application, it just comes with syntax sugar for specifying the `self` parameter.
- Jaxan 1y agoA big difference is that it must include the thing the functions act upon (self). In functional programming you often leave that out. In FP you can also refactor little pieces of the chain into its own separate name (eg: smallest = sort >> head). This avoids long chains.
- fuzztester 1y agoInteresting. In which functional programming languages (that you know), can this be done? That refactoring little pieces, I mean. Looks like it would help with code readability.
- mrkeen 1y ago> When did method chaining become 'functional programming'? As soon as you stop calling it "method chaining" and start calling it "function composition". If you chain together a bunch of methods ('.' operator) in an OO setting, that's called a "fluent interface". It's a sign of good design and is to be commended. If you compose a bunch of functions ('.' operator) in an FP setting, it's an unreadable mess, and you will receive requests to break it into separate assignment statements and create named variables for all the intermediate states.
- Animats 1y agoYou know, stuff like this, which I had to write today, for turning a 2D floating point height map into a 2D greyscale image. let max = self .heights .elements_row_major_iter() .max_by(|a, b| a.total_cmp(b)) .unwrap(); let min = self .heights .elements_row_major_iter() .min_by(|a, b| a.total_cmp(b)) .unwrap(); // Scale into 0..255 let range = (max - min).max(0.001); let height_array = self .heights .as_rows() .into_iter() .map(|r| { r.into_iter() .map(|v| ((((v - min) / range) / 256.0).round() as usize).clamp(0, 255) as u8) .collect() }) .collect(); After Rust formats it, it's probably more lines than doing it with FOR statements. Every parenthesis matters. So much of the typing is implicit that it's not at all clear what's going on.