(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)' -- Travis Griggs Objologist "Dying men never wish they'd spent more time at the office" |
On Thu, Mar 11, 2010 at 8:36 PM, Travis Griggs <[hidden email]> 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). In one line: 'This is a (string) with some (parenthetical fields)' withParentheticalFieldsCapitalized
...and then in the implementation of #withParentheticalFieldsCapitalized, take all the space you need to make it nicely readable and fast. ;) - Stephen
|
In reply to this post by Travis Griggs-4
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. ;) Levente > > -- > Travis Griggs > Objologist > "Dying men never wish they'd spent more time at the office" > > > |
On Thu, Mar 11, 2010 at 6:17 PM, Levente Uzonyi <[hidden email]> wrote:
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]]
|
In reply to this post by Travis Griggs-4
>>>>> "Travis" == Travis Griggs <[hidden email]> writes:
Travis> Given strings of the form Travis> 'This is a (string) with some (parenthetical fields)' Travis> turn them into Travis> 'This is a (STRING) with some (PARENTHETICAL FIELDS)' Somehow, I think streams and #upTo: are gonna be the key there. -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 <[hidden email]> <URL:http://www.stonehenge.com/merlyn/> Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc. See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion |
#upTo: is inconvenient for this because the result doesn't include the matched character. If we had #upToWith: we could write:
| in | in := 'This is a (string) with some (parenthetical fields)' readStream. String streamContents: [:out | [in atEnd] whileFalse: [out nextPutAll: (in upToWith: $( ); nextPutAll: (in upToWith: $) ) asUppercase]] Cheers, Josh On Mar 11, 2010, at 7:34 PM, Randal L. Schwartz wrote: >>>>>> "Travis" == Travis Griggs <[hidden email]> writes: > > Travis> Given strings of the form > > Travis> 'This is a (string) with some (parenthetical fields)' > > Travis> turn them into > > Travis> 'This is a (STRING) with some (PARENTHETICAL FIELDS)' > > Somehow, I think streams and #upTo: are gonna be the key there. > > -- > Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 > <[hidden email]> <URL:http://www.stonehenge.com/merlyn/> > Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc. > See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion > |
On 12 March 2010 06:56, Josh Gargus <[hidden email]> wrote:
> #upTo: is inconvenient for this because the result doesn't include the matched character. If we had #upToWith: we could write: > > | in | in := 'This is a (string) with some (parenthetical fields)' readStream. > String streamContents: [:out | [in atEnd] whileFalse: [out nextPutAll: (in upToWith: $( ); nextPutAll: (in upToWith: $) ) asUppercase]] > a slight variation | f | f := false. 'This is a (string) with some (parenthetical fields)' collect: [:c | c = $( ifTrue: [f:=true]. c = $) ifTrue: [f:=false]. f ifTrue: [ c asUppercase] ifFalse: [c]] | f | f := #yourself. 'This is a (string) with some (parenthetical fields)' collect: [:c | c = $( ifTrue: [f:=#asUppercase]. c = $) ifTrue: [f:=#yourself]. c perform: f] > Cheers, > Josh > > > On Mar 11, 2010, at 7:34 PM, Randal L. Schwartz wrote: > >>>>>>> "Travis" == Travis Griggs <[hidden email]> writes: >> >> Travis> Given strings of the form >> >> Travis> 'This is a (string) with some (parenthetical fields)' >> >> Travis> turn them into >> >> Travis> 'This is a (STRING) with some (PARENTHETICAL FIELDS)' >> >> Somehow, I think streams and #upTo: are gonna be the key there. >> >> -- >> Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 >> <[hidden email]> <URL:http://www.stonehenge.com/merlyn/> >> Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc. >> See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion >> > > > -- Best regards, Igor Stasenko AKA sig. |
In reply to this post by Randal L. Schwartz
Here's 3 solutions from me:
Similar to others: coercion := #yourself. string collect: [:c | coercion := c = $( ifTrue: [#asUppercase] ifFalse: [c = $) ifTrue: [#yourself] ifFalse: [coercion]]. c perform: coercion] Requires VW specific methods piecesCutWhere: and fold: ((string piecesCutWhere: [:a :b | b = $( or: [a = $)]]) collect: [:phrase | phrase first = $( ifTrue: [phrase asUppercase] ifFalse: [phrase]]) fold: [:a :b | a , b] I know Eliot and others know how to write both of these methods, nudge, nudge :) Requires VW difference engine that shows up in tomorrows build (I regret that this ends making my original post look like an opportunity to tout this. I honestly didn't even think about the differences engine, it was only later this evening while I was thinking about ways to solve it, that this donned on me): (string differences: (string reject: [:c | '()' includes: c])) fold: [:a :b | a , (a last = $( ifTrue: [b collect: #asUppercase] ifFalse: [b])] This last one is definitely terse and elegant, but much more obscure how it actually pulls it off. -- Travis Griggs Objologist I multiply all time estimates by pi, to account for running around in circles. |
Hi!
I know this is "cheating" but if we use my SimpleMacro package: http://map.squeak.org/packagebyname/simplemacro ...then it is trivially written as: (MacroProcessor block: [:x | '(', x asUppercase, ')' ] between: '(' and: ')') deep: false; process: 'This is a (string) with some (parenthetical fields)' :) Note the "deep: false;", otherwise it will try to expand the expansions and goes into an infinite loop. regards, Göran |
In reply to this post by Josh Gargus
On 3/12/10 2:56 AM, "Josh Gargus" <[hidden email]> wrote: > #upTo: is inconvenient for this because the result doesn't include the matched > character. If we had #upToWith: we could write: > > | in | in := 'This is a (string) with some (parenthetical fields)' readStream. > String streamContents: [:out | [in atEnd] whileFalse: [out nextPutAll: (in > upToWith: $( ); nextPutAll: (in upToWith: $) ) asUppercase]] > > Cheers, > Josh I like this one. Nice ! Cheers. Edgar |
In reply to this post by Göran Krampe
On 3/12/10 4:51 AM, "Göran Krampe" <[hidden email]> wrote: > Hi! > > I know this is "cheating" but if we use my SimpleMacro package: > > http://map.squeak.org/packagebyname/simplemacro > > ...then it is trivially written as: > > (MacroProcessor block: [:x | '(', x asUppercase, ')' ] > between: '(' and: ')') deep: false; process: > 'This is a (string) with some (parenthetical fields)' > > :) > > Note the "deep: false;", otherwise it will try to expand the expansions > and goes into an infinite loop. > > regards, Göran This one likes more. Great Göran |
In reply to this post by Göran Krampe
On Mar 11, 2010, at 10:51 PM, Göran Krampe wrote:
> Hi! > > I know this is "cheating" but if we use my SimpleMacro package: > > http://map.squeak.org/packagebyname/simplemacro > > ...then it is trivially written as: > > (MacroProcessor block: [:x | '(', x asUppercase, ')' ] > between: '(' and: ')') deep: false; process: > 'This is a (string) with some (parenthetical fields)' > > :) > > Note the "deep: false;", otherwise it will try to expand the > expansions and goes into an infinite loop. I don't think it's cheating at all. The whole point of such a post is to see what techniques AND tools others use out of the box to solve problems. And you showed a cool one. One that I wasn't that aware of. I'm curious what other ways you use your MacroProcessor for now. I looked at the link, but the example didn't stir my imagination that much. It's no more cheating than my use of a diff engine to tokenize the string. <aside>I know I'm not going to see my own email, I've been to my subscription page and turned said option on, what gives?</aside> -- Travis Griggs Objologist "I did not have time to write you a short program, so I wrote you a long one instead." |
Hi!
Hi! Travis Griggs wrote: > I don't think it's cheating at all. The whole point of such a post is to > see what techniques AND tools others use out of the box to solve > problems. And you showed a cool one. One that I wasn't that aware of. > I'm curious what other ways you use your MacroProcessor for now. I > looked at the link, but the example didn't stir my imagination that much. I wrote it initially in order to do ASP-ish templating. Then I ended up making it support #deep: (tries to expand the expansions in turn) and #recursive: (if macros inside macros should be expanded before the macro itself is expanded). And btw, it is a single class and doesn't do more than it says it does :) I have used it both for web page templates and email templates. Btw, here is most of the class comment: "MacroProcessor is a preprocessor that can process a String and given a startString and an endString it will call a given model (or a pluggable block) to expand the contents found between those markers. The content between one such pair of start/end markers is called a macro. The result returned from the model/block is called an expansion. MacroProcessor can either do one level expansion (not further expanding macros possibly produced by the expansions) or do deep expansion which means it will immediately try to further process produced expansions. It can also be configured to not recurse, which means it will not process the macro itself before expanding it. If recursing it will do left to right, inner first expansion." regards, Göran |
In reply to this post by Josh Gargus
VisualWorks has that one, as #through:, and the
#corresponding throughAll: for a collection argument.
At 11:56 PM 2010-03-11, Josh Gargus wrote: #upTo: is inconvenient for this because the result doesn't include the matched character. If we had #upToWith: we could write: --
Alan Knight [|], Engineering Manager, Cincom Smalltalk
|
Thanks Alan, that's good to know. I don't much like the name #upToWith: anyway; it was just the first reasonable thing that came to mind.
Cheers, Josh On Mar 12, 2010, at 5:17 AM, Alan Knight wrote:
|
In reply to this post by Travis Griggs-4
On Fri, 2010-03-12 at 02:01 -0800, Travis Griggs wrote:
> <aside>I know I'm not going to see my own email, I've been to my > subscription page and turned said option on, what gives?</aside> The culprit here is your email 'handler', I guess they consider it a feature: http://en.wikipedia.org/wiki/Gmail#Technical_limitations Or have I misunderstood the question? > -- > Travis Griggs > Objologist > "I did not have time to write you a short program, so I wrote you a > long one instead." Ken signature.asc (197 bytes) Download Attachment |
Free forum by Nabble | Edit this page |