[squeak-dev] One liner challenge

Eliot Miranda eliot.miranda at gmail.com
Fri Mar 12 03:22:53 UTC 2010


On Thu, Mar 11, 2010 at 6:17 PM, Levente Uzonyi <leves at elte.hu> wrote:

> On Thu, 11 Mar 2010, Travis Griggs wrote:
>
>  (Preamble: I have lurked here without really paying attention much for a
>> year+ now. I thought I'd try to reinvolve myself. The following discussions
>> are some of my favorite/funnest).
>>
>> I was looking at someone's ruby/regex code. I could do the equivalent of
>> what they were doing with the ST Regex library. But I was curious if it
>> could be done tersely/elegantly without using regex. Here's the challenge.
>>
>> Given strings of the form
>>
>> 'This is a (string) with some (parenthetical fields)'
>>
>> turn them into
>>
>> 'This is a (STRING) with some (PARENTHETICAL FIELDS)'
>>
>
> ((('This is a (string) with some (parenthetical fields)' findTokens: '()')
> collectWithIndex: [ :each :index | index odd ifFalse: [ '(', each
> asUppercase, ')' ] ifTrue: [ each ] ]) gather: #yourself) as: String.
>
> 'This is a (string) with some (parenthetical fields)' in: [ :string | |
> transformation | transformation := #yourself. string collect: [ :each |
> (transformation := each = $( ifTrue: [ #asUppercase ] ifFalse: [ each = $)
> ifTrue: [ #yourself ] ifFalse: [ transformation ] ]) value: each ] ].
>
> 'This is a (string) with some (parenthetical fields)' in: [ :string |
> String streamContents: [ :output | | input | input := string readStream. [
> input atEnd ] whileFalse: [ output nextPutAll: (input upTo: $(); nextPut:
> $(; nextPutAll: (input upTo: $)) asUppercase; nextPut: $) ] ] ].
>
> Let me know which if your favorite. ;)
>

inelegant & obvious but terse:

| inp | inp := false. 'This is a (string) with some (parenthetical fields)'
collect: [:c| (inp := c = $( or: [inp and: [c ~= $)]]) ifTrue: [c
asUppercase] ifFalse: [c]]


>
>
> Levente
>
>
>
>> --
>> Travis Griggs
>> Objologist
>> "Dying men never wish they'd spent more time at the office"
>>
>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20100311/98a5a585/attachment.htm


More information about the Squeak-dev mailing list