Category:Smalltalk: Difference between revisions

Content added Content deleted
Line 498: Line 498:
For example, if asked to provide an example for a factorial function, a typical Smalltalk solution would be to define a method in the Integer class called "factorial", which might look like the following naïve version:
For example, if asked to provide an example for a factorial function, a typical Smalltalk solution would be to define a method in the Integer class called "factorial", which might look like the following naïve version:
<lang smalltalk>factorial
<lang smalltalk>factorial
self == 1 ifTrue:[^1].
^ (self <= 1) ifTrue:[1] ifFalse:[self * (self-1) factorial]</lang>
(here 'self' is the Integer receiver object, and "ˆ" returns a value from the message send).
^self * (self - 1) factorial</lang>
(here self would be the Integer receiver object, and "ˆ" will return a value from the message send).


To get the factorial value, we'd evaluate in a workspace:<lang>10 factorial</lang>.
To get the factorial value, we'd evaluate in a workspace:<lang>10 factorial</lang>.
Line 508: Line 507:
<br>b) save the snippet to a file (in a fileIn format) and "fileIn" (aka. "load") the file.
<br>b) save the snippet to a file (in a fileIn format) and "fileIn" (aka. "load") the file.


In both cases, you'd end up with a system infected with many Rosetta methods, which you'd have to remove afterwards (using "undo" or "delete"). And because Smalltalk keeps track of your changes, it usually involves additional cleanup work in your change history.
In both cases, you'd end up with a system infected with many Rosetta methods, which you'd have to remove afterwards (using "undo" or "delete"). And because Smalltalk keeps track of your changes, it usually involves additional cleanup work in your change history (changeFile/changeSet/changeRepository).

With b) comes the additional trouble that fileIn formats are different (chunk file, vs. XML file, vs. Monticello, vs. GNU-ST etc.).
With b) comes the additional trouble that fileIn formats are different (chunk file, vs. XML file, vs. Monticello, vs. GNU-ST etc.).
<br>For example, for export/import, GNU-ST uses a ''private format'' (which has the advantage of not needing the chunk format's bangs and especially the ugly bang doubling inside code and the empty chunk at the end):
<br>For example, for export/import, GNU-ST uses a ''private format'' (which has the advantage of not needing the chunk format's bangs and especially the ugly bang doubling inside code and the empty chunk at the end):
Line 516: Line 516:
]
]
]</lang>
]</lang>
however, it is incompatible and not supported by those which use the historical chunk format:
however, it is incompatible and not supported by most others, which use the historical chunk format:
<lang smalltalk>!Number methodsFor:'math'!
<lang smalltalk>!Number methodsFor:'math'!
my_factorial
my_factorial
Line 522: Line 522:
! !</lang>
! !</lang>


So in which dialect's fileOut format should the example be presented to be most convenient?
So in which dialect's fileOut format should the example be presented to be most convenient, and to be repeatable in case someone wants to try Smalltalk?




Therefore, expression-like snippets work more or less in all dialects, and snippets are usually presented in a functional or expression style, which works outside any class.
Expression-like snippets work more or less in all dialects, and such snippets are usually presented in a functional or expression style, which works outside any class.
Typically these define a function (here called "block") and then call it.
<br>Typically these define a function (here called "block") and then call it.
<br>For the above, this might look like:
<br>For the above, this might look like:
<lang smalltalk>factorial := [:n |
<lang smalltalk>factorial := [:n |
Line 533: Line 533:
].
].
factorial value:10</lang>
factorial value:10</lang>
(here there is no self, and the code works anywhere outside a class context. the <tt>value></tt> message sent to the block called the function. There is no "ˆ" return statement, because the value returned from the block is the value of the last expression in it, and there actually will be no method from which to return).
(here there is no self, and the code works anywhere outside any class context. the <tt>value></tt> message sent to the block called the function. There is no "ˆ" return statement, because the value returned from the block is the value of the last expression in it, and there actually will be no method from which to return).

The advantage is that this code can be simply selected as a whole and evaluated. Any variables created will be local to the evaluation scope and not infect the system.


The advantage is that this code can be simply selected as a whole and evaluated.
The disadvantage is that it might look somewhat non-Smalltalk-like to not have it in a class/method.
The disadvantage is that it might look somewhat non-Smalltalk-like to not have it in a class/method.

However, it shows that Smalltalk albeit being a pure OO-language, does have functional aspects in it.


==Citations==
==Citations==