dkubb / adamantium

Create immutable objects
MIT License
373 stars 13 forks source link

generic method for transforming values into new immutable values #1

Closed misfo closed 10 years ago

misfo commented 11 years ago

Hamster::Immutable has a succinct and generic way of producing new immutable values based on existing ones. The method is called transform and it allows you to mutate an object within a block which is then frozen to produce the value that's returned.

It'd be nice if this project exposed a method for producing new values like that. Is that something you'd accept a patch for?

dkubb commented 11 years ago

Actually Hamster::Immutable was my initial inspiration for Virtus::Immutable, which is what this was extracted from.

I would support this kind of method. Is #transform the best name for it? I'm not saying it's not a good name, I just like to think about API changes and not blindly copy another API without putting as much thought in as if I was designing it from scratch.

misfo commented 11 years ago

Yeah, it seems like there's no standard word in programming for the create-a-new-value-based-on-another-value operation...

How about "beget"? or "produce"?

dkubb commented 11 years ago

Hmm. I'm not so sure about those. I kind of like to use terms that are either in common use in the domain, or in common use within natural language. EDIT: well I guess produce is in common use, but not beget.

I wonder if there are any common names in the functional programming world for this kind of thing?

misfo commented 11 years ago

FP peeps seem to just use the word "update" accompanied by some air quotes or asterisks

Sent from my phone

On Oct 10, 2012, at 11:37 PM, Dan Kubb notifications@github.com wrote:

Hmm. I'm not so sure about those. I kind of like to use terms that are either in common use in the domain, or in common use within natural language.

I wonder if there are any common names in the functional programming world for this kind of thing?

— Reply to this email directly or view it on GitHub.

dkubb commented 11 years ago

I actually wouldn't have a problem with #update as the method name.

dkubb commented 10 years ago

FWIW I am planning on adding the protected methods #transform and #transform_unless from hamster: https://github.com/harukizaemon/hamster/blob/master/lib/hamster/immutable.rb#L67-L75

dkubb commented 10 years ago

@misfo I have added Adamantium#transform and Adamantium#transform_unless to the master branch, which were inspired by hamster.

mbj commented 10 years ago

@dkubb If we have transform_unless shouldnt we also have transform_if ?