[squeak-dev] Smalltalk string API

Nicolas Cellier nicolas.cellier.aka.nice at gmail.com
Sun Feb 20 21:43:08 UTC 2011


2011/2/19 Levente Uzonyi <leves at elte.hu>:
> On Wed, 16 Feb 2011, Nicolas Cellier wrote:
>
>> I started referencing Smalltalk idioms at
>>
>> http://en.wikipedia.org/wiki/Comparison_of_programming_languages_(string_functions)
>> I could have focused on ANSI but have chosen Squeak/Pharo. Feel free
>> to correct me and to complete me.
>>
>> This is a very enlighting exercize, especially for pointing when API
>> turns to be not that bright.
>> During my perigrination, I notably noticed this:
>>
>> #compare: returns 1, 2, or 3 : this is both very object oriented, very
>> intuitive and very standard and the rest of the world is stupid,
>> unless...
>
> 1, 2 and 3 was a bad choice, -1, 0 and 1 would have been better, though I
> don't like those either.
>
>>
>> #findLastOccurrenceOfString:startingAt: in its current form is stupid
>> to my taste, because
>> 1) implementation is inefficient
>> 2) the startingAt: only skip the beginning of the string which seems
>> odd for a rfind operation
>> I would rather expect this kind of usage:
>> last := aString findLastOccurrenceOfString: 'to' startingAt: aString size.
>> lastButOne := aString findLastOccurrenceOfString: 'to' startingAt: last -
>> 1.
>
> Agreed.
>
>>
>> The CamelCase is sometimes abusive like #includesSubString:
>
> This part of the system is really messy. There are also several methods for
> doing the same (or very similary) things. For example:
>
> includesSubString:
> includesSubstring:caseSensitive:
>
> indexOfSubCollection:*
> findString:*
>
> or my favorites
>
> findTokens:
> subStrings:
> findBetweenSubStrs:
> splitBy:
>
> etc
>
>>
>> There is no format. I know, purists will tell me that encoding a
>> format in a cryptic string is not in the Smalltalk spirit, but please
>> then tell me how to specify a formatting efficiently and also remove
>> cryptic regex encoding (a pity, it's not in trunk).
>
> Should we create our own fork of VB-Regex or should we resurrect the PCRE
> regex support (it's available on SqueakMap IIRC)?
>
>>
>> I let a few holes (split/join etc...)
>
> I badly needed #join: yesterday. Writing the following multiple times is not
> fun:
> String streamContents: [ :stream | foo asStringOn: stream delimiter: bar ]
>
> What Wikipedia contains* as the Squeak/Pharo implementation of join is
> "interesting":
>
> array_of_strings reduce: [:left :right | left , separator , right ])
>
> - it's inefficent (O(n^2) worst case runtime, unnecessary allocations)
> - it suggests that _ in a valid character in Smalltalk variable names
>
>
> Levente

Yes I know, I wrote this thing (before me Smalltalk was not mentionned).
Maybe it was a bit late at night ;) , anyway I had not so many choices:
- use join: (I think it is integrated in Pharo)
- use String streamContents: [:stream | arrayOfStrings do:
[:eachString | stream nextPutAll: eachString] separatedBy: [stream
nextPutAll: separator]]
  I would have written this form in Smalltalk, however I find the
periphrase a bit long versus other languages
- use reduce: to find a shorter form comprehensible by foreign
programming language users, though not optimal

Of course, it would also be nice to see a Xtreams solution.

Nicolas

>
> *http://en.wikipedia.org/wiki/Comparison_of_programming_languages_(string_functions)#join
>
>>
>> Cheers
>>
>>
>
>



More information about the Squeak-dev mailing list