[Tutor] running a script from IDLE
alan.gauld@bt.com
alan.gauld@bt.com
Sat, 9 Mar 2002 21:11:21 -0000
> >known as "Functional Programming" - along with having functions
> >that always return a value.
>
> Seems to me the OO model is somewhat at odds with the
> FP model,
They are orthogonal.
In Smalltalk an FP style is encouraged and the default
return value from methods is self - Thus you get a
reference to the modified object. You can use or discard
that reference as you like but it allows an FP approach
if required/desired.
> is inherently destructive, unless you want your method
> to make a [deep] copy of self, manipulate that copy, and
> return a new instance.
Or just a reference to the mofified object
myfoo = myfoo.setBar(42)
myfoo is still referencing the same object it always did
but with a new value for bar...
> about some axis. But if I want to leave icosa as is, and
> return the rotated icosa, then I have to copy icosa
> internally, and if the points are themselves point objects
> in a list, deepcopy to get copies of those.
You need to do that anyway since the original is different
to the rotated version. I don't see the difference?
> the OO model doesn't seem to insist that I should.
Absolutely, OO, FP and imperative programming styles are
different in emphasis. You can combine all three by adopting
the intersecting attributes and abandioning the unique
features. Some people would argue that this leads to the best
(most reliable and maintainable) programming style of all.
But it does bring its own issues in performance and resource
usage.
> Instances are the termini, and, being objects, it makes
> plenty of sense to change their state. When I move a
> pencil on my desk, it's the pencil that moves, not
> some clone of the pencil, such that now I have two.
Correct and FP is quite happy for you to return a new reference
to the modified pencil. The point of returning values is that
you can apply value substitution to prove the correctness of
the solution. (The real FP afficianados wuill be screaming
at me real soon :-) When a function performs its task without
returning a value you lose the concept of the entire program
being an expression that is evaluated.
> Doubtless this is not a new insight. Some FP purists
> probably fight the encroachment of OO thinking into
> their space as a result.
On the contrary most embrace OO but practiced in an
FP style - FP uses polymorphism heavily for example.
A few zealots would have us abandon OO for a pure FP
approach but most see value in the encapsulation/abstraction
that OO provides.
Alan G