Remix.run Logo
flohofwoe 13 hours 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.

Animats 9 hours ago | parent | next [-]

You 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.
mrkeen 13 hours ago | parent | prev | next [-]

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

jolux 13 hours ago | parent | prev [-]

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