[squeak-dev] One liner challenge
leves at elte.hu
Fri Mar 12 02:17:30 UTC 2010
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. ;)
> Travis Griggs
> "Dying men never wish they'd spent more time at the office"
More information about the Squeak-dev