dbrattli / OSlash

Functors, Applicatives, And Monads in Python
MIT License
714 stars 50 forks source link

Mutability Issue #8

Closed Checkroth closed 4 years ago

Checkroth commented 7 years ago

I was experimenting a bit with how OSlash operates with Python's inherent mutability.

While you can't assign within a map, you can still mutate variables outside of the context, for instance a dict:


In [77]: y = Just(1)

In [78]: y.map(lambda x: d.pop('hoge'))
Out[78]: Just 10

In [79]: d
Out[79]: {}

This is troublesome in a context in which you would want to map with a function that relies on the mutability of an object - for instance splitting one dict in to two via popping the appropriate variables in to a second dict, while maintaining the structure of the initial dict.

Obviously this can be avoided by copying the dict, either outside the context or within the function. I would however expect that applying something in a functional context would absolutely not mutate anything. I don't know if this is avoidable in a way that isn't wasteful (simply implementing a copy within the library wouldn't be ideal) but it caught my interest.

dbrattli commented 4 years ago

Yes, OSlash does not provide immutable replacements for all Python types, nor does it prevent you from using mutable Python types.