DEV Community

loading...

Discussion on: Why I don't believe in pure functional programming anymore

Collapse
zilti profile image
Daniel Ziltener

So you want to force mutability into functional programming. Maybe you didn't get as deep into the functional programming mindset as you thought you did. Especially with that claim that Rust provided "all the advantages" just because mutability has to be explicit.

There is more to functional programming and immutability. You can modify an immutable data structure, you'll just end up with a modified copy of it instead of modifying the original, and THAT is what the immutability is about: not about making your life hard by making it cumbersome to modify a data structure, but by having you be very explicit about who gets to see and operate on that modified structure.

The problem with mutable data structures isn't that they are mutable. It is that you have no idea what you cause by mutating it, because who-knows-how-many things might be accessing it.

Lazy operations are another classic. It looks like in Rust you need a library to even get lazy evaluation in the first place. But it opens so many doors in functional programming. Composing operations, operating on infinite collections, and so on.

Collapse
maan2003 profile image
Maan2003 • Edited

You can modify an immutable data structure, you'll just end up with a modified copy of it instead of modifying the original, and THAT is what the immutability is about: not about making your life hard by making it cumbersome to modify a data structure, but by having you be very explicit about who gets to see and operate on that modified structure

collections are not language feature. So there are immutable collections in rust.

Collapse
maan2003 profile image
Maan2003

As soon as you modified a variable, everything you ever gave this variable to now implicitly has this modified version.

If you gave this variable to something, how can you mutate it? the variable has moved.
I tried my best

Collapse
yujiri8 profile image
Ryan Westlund Author

So you want to force mutability into functional programming. Maybe you didn't get as deep into the functional programming mindset as you thought you did. Especially with that claim that Rust provided "all the advantages" just because mutability has to be explicit.

Please do not reach for questioning my experience as your first response to disagreement. You also misrepresent what I said, as I never claimed that Rust provided all the advantages. I said it provided "most, maybe all". I even described how I was open to the idea that there were some benefits it was missing out on, but that I believed they didn't justify the imposition of total purity because they were mutually exclusive with more important benefits.

There is more to functional programming and immutability. You can modify an immutable data structure, you'll just end up with a modified copy of it instead of modifying the original, and THAT is what the immutability is about: not about making your life hard by making it cumbersome to modify a data structure, but by having you be very explicit about who gets to see and operate on that modified structure.

What you describe here actually sounds like Rust. You can modify things, but have to be very explicit (via the ownership system) about who gets to see and operate on that modified structure. In Haskell, changing to do this requires a heavy refactor replete with arcane type system hacks.

The problem with mutable data structures isn't that they are mutable. It is that you have no idea what you cause by mutating it, because who-knows-how-many things might be accessing it.

Again, in Rust, ownership makes it clear what things are accessing it.

Lazy operations are another classic. It looks like in Rust you need a library to even get lazy evaluation in the first place. But it opens so many doors in functional programming. Composing operations, operating on infinite collections, and so on.

Rust doesn't have pervasive lazy evaluation, but it has lazy iterators which allow composing operations and operating on infinite collections. So does Python. I hear so does Javascript these days.

Are you aware of Idris, a remake of Haskell that retains pure functional programming but isn't lazy everywhere? The point I'm making by mentioning it is that laziness and functional purity are not as intertwined as you seem to think.

Collapse
zilti profile image
Daniel Ziltener

Again, in Rust, ownership makes it clear what things are accessing it.

Ownership in Rust is pretty much the same thing as having variables belong to a class in object oriented languages, from what I understood. As soon as you modified a variable, everything you ever gave this variable to now implicitly has this modified version. This is very different from the functional programming approach where you explicitly have to give the modified variant of the collection to every place you want to see the modification.

Are you aware of Idris, a remake of Haskell that retains pure functional programming but isn't lazy everywhere?

I am aware, but I never used it, and also don't really plan to - same as how I did look into Haskell a little, but opted not to use it; these days I am a Clojure user.

laziness and functional purity are not as intertwined as you seem to think.

They aren't as intertwined, but they are both part of the concepts of functional programming.

Thread Thread
maan2003 profile image
Maan2003 • Edited
pub struct Immut<T>(T);
impl<T> std::ops::Deref for Immut<T> {
  type Target = T;
  fn deref(&self) -> &T { &self.0 }
}
pub fn immut<T>(x: T) -> Immut<T> {
  Immut(x)
}

this guarantees that Immut can't be mutated from outside this module(rust has module level privacy).

Ownership in Rust is pretty much the same thing as having variables belong to a class in object oriented languages, from what I understood. As soon as you modified a variable, everything you ever gave this variable to now implicitly has this modified version. This is very different from the functional programming approach where you explicitly have to give the modified variant of the collection to every place you want to see the modification.

I don't understand what you mean. I would love to see code example.