[Python-ideas] "Immutable Builder" Pattern and Operator
Wes Turner
wes.turner at gmail.com
Mon Jan 23 10:48:25 EST 2017
On Sunday, January 22, 2017, Wes Turner <wes.turner at gmail.com> wrote:
>
>
> On Sunday, January 22, 2017, Soni L. <fakedme+py at gmail.com
> <javascript:_e(%7B%7D,'cvml','fakedme%2Bpy at gmail.com');>> wrote:
>
>>
>>
>> On 22/01/17 10:03 PM, Wes Turner wrote:
>>
>>
>>
>> On Sunday, January 22, 2017, Wes Turner <wes.turner at gmail.com> wrote:
>>
>>> Have you looked at pyrsistent for immutable/functional/persistent/copy-on-write
>>> data structures in Python?
>>>
>>> https://github.com/tobgu/pyrsistent/
>>>
>>> (freeze() / thaw())
>>>
>>> ... e.g. List and Dict NamedTuple values are not immutable (because
>>> append() and update() still work)
>>>
>>
>> fn.py also has immutables:
>> https://github.com/kachayev/fn.py/blob/master/README.rst#per
>> sistent-data-structures
>>
>>
>> You seem to be thinking of "immutable object builder". Not "the builder
>> itself is immutable and operations on it create new builders".
>>
>
> My mistake.
> Something like @optionable and/or @curried from fn.py in conjunction with
> PClass from pyrsistent may accomplish what you describe?
>
From
http://pyrsistent.readthedocs.io/en/latest/api.html#pyrsistent.PClass.set :
"Set a field in the instance. Returns a new instance with the updated
value. The original instance remains unmodified. Accepts key-value pairs or
single string representing the field name and a value."
>
>>
>>
>>
>>
>>>
>>> On Sunday, January 22, 2017, Soni L. <fakedme+py at gmail.com> wrote:
>>>
>>>> I've been thinking of an Immutable Builder pattern and an operator to
>>>> go with it. Since the builder would be immutable, this wouldn't work:
>>>>
>>>> long_name = mkbuilder()
>>>> long_name.seta(a)
>>>> long_name.setb(b)
>>>> y = long_name.build()
>>>>
>>>> Instead, you'd need something more like this:
>>>>
>>>> long_name = mkbuilder()
>>>> long_name = long_name.seta(a)
>>>> long_name = long_name.setb(b)
>>>> y = long_name.build()
>>>>
>>>> Or we could add an operator to simplify it:
>>>>
>>>> long_name = mkbuilder()
>>>> long_name .= seta(a)
>>>> long_name .= setb(b)
>>>> y = long_name.build()
>>>>
>>>> (Yes, I'm aware you can x = mkbuilder().seta(a).setb(b), then y =
>>>> x.build(). But that doesn't work if you wanna "fork" the builder. Some
>>>> builders, like a builder for network connections of some sort, would work
>>>> best if they were immutable/forkable.)
>>>> _______________________________________________
>>>> Python-ideas mailing list
>>>> Python-ideas at python.org
>>>> https://mail.python.org/mailman/listinfo/python-ideas
>>>> Code of Conduct: http://python.org/psf/codeofconduct/
>>>>
>>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170123/cdb959cb/attachment-0001.html>
More information about the Python-ideas
mailing list