One liner challenge

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

One liner challenge

Travis Griggs-3
I was looking at someone's ruby/regex code. I could do the equivalent  
of what they were doing with the 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
[hidden email]
"The dissenter is every human being at those moments of his life when  
he resigns momentarily from the herd and thinks for himself." -
Archibald MacLeish, poet and librarian



_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: One liner challenge

Eliot Miranda-2


On Thu, Mar 11, 2010 at 5:26 PM, Travis Griggs <[hidden email]> wrote:
I was looking at someone's ruby/regex code. I could do the equivalent
of what they were doing with the 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)'

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]]
 


--
Travis Griggs
[hidden email]
"The dissenter is every human being at those moments of his life when
he resigns momentarily from the herd and thinks for himself." -
Archibald MacLeish, poet and librarian



_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: One liner challenge

Travis Griggs-3

Here's 3 solutions I came up with after posting the challenge:

1) Brute force, like Eliot's:

coercion := #yourself.
string collect:
                [:c |
                coercion := c = $(
                                        ifTrue: [#asUppercase]
                                        ifFalse: [c = $) ifTrue: [#yourself] ifFalse: [coercion]].
                c perform: coercion]

2) Without side state:

((string piecesCutWhere: [:a :b | b = $( or: [a = $)]])
        collect: [:phrase | phrase first = $( ifTrue: [phrase asUppercase]  
ifFalse: [phrase]])
                fold: [:a :b | a , b]

3) Abuse of the new differences API: that shows up in tomorrows dev  
build:

(string differences: (string reject: [:c | '()' includes: c]))
        fold: [:a :b | a , (a last = $( ifTrue: [b collect: #asUppercase]  
ifFalse: [b])]

--
Travis Griggs
Objologist
Light travels faster than sound. This is why some people appear bright  
until you hear them speak...




_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

New VW diff engine (was: One liner challenge)

Travis Griggs-3
On Mar 11, 2010, at 10:46 PM, Travis Griggs wrote:

> 3) Abuse of the new differences API: that shows up in tomorrows dev
> build:
>
> (string differences: (string reject: [:c | '()' includes: c]))
> fold: [:a :b | a , (a last = $( ifTrue: [b collect: #asUppercase]
> ifFalse: [b])]

You can read more about the new differences: method here:

http://www.cincomsmalltalk.com/userblogs/travis/blogView?showComments=true&printTitle=How_to_get_a_Human_from_a_Chimpanzee&entry=3445815344

--
Travis Griggs
Objologist
"There are a thousand hacking at the branches of evil to one who is  
striking at the root" - Henry David Thoreau


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: One liner challenge

Paul Baumann
In reply to this post by Travis Griggs-3

Here is an example of a single statement #inject:into: with simple blocks and (very basic) support for nested parenthesis of other types.

('This is a (string) with some (parenthetical fields)'
        inject: (Array with: (WriteStream on: String new) with: 0 asValue)
        into: [:streamAndBraceLevel :chr |
                ('([{<' includes: chr)
                        ifTrue: [streamAndBraceLevel last value: streamAndBraceLevel last value + 1].
                (')]}>' includes: chr)
                        ifTrue: [streamAndBraceLevel last value: streamAndBraceLevel last value - 1].
                streamAndBraceLevel first
                        nextPut: (streamAndBraceLevel last value > 0 ifTrue: [chr asUppercase] ifFalse: [chr]).
                streamAndBraceLevel
        ]) first contents.

 'This is a (STRING) with some (PARENTHETICAL FIELDS)'

More elegant solutions are available without the single line/statement requirement or when using Smalltalk extensions.

Paul Baumann


-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Travis Griggs
Sent: Thursday, March 11, 2010 8:26 PM
To: VWNC NC
Subject: [vwnc] One liner challenge

I was looking at someone's ruby/regex code. I could do the equivalent of what they were doing with the 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
[hidden email]
"The dissenter is every human being at those moments of his life when he resigns momentarily from the herd and thinks for himself." - Archibald MacLeish, poet and librarian



_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc


This message may contain confidential information and is intended for specific recipients unless explicitly noted otherwise. If you have reason to believe you are not an intended recipient of this message, please delete it and notify the sender. This message may not represent the opinion of IntercontinentalExchange, Inc. (ICE), its subsidiaries or affiliates, and does not constitute a contract or guarantee. Unencrypted electronic mail is not secure and the recipient of this message is expected to provide safeguards from viruses and pursue alternate means of communication where privacy or a binding message is desired.


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: New VW diff engine (was: One liner challenge)

Paul Baumann
In reply to this post by Travis Griggs-3

Since we are really just promoting code...

High performance, comment-aware, formating-ignoring, code scanning and comparison was also required for GemKit tools. The VW scanners of the time didn't do this well; I've never checked if they've improved. The few methods in the attached file are what I created for GemKit to scan Smalltalk syntax of multiple dialects. It has had years of use, with no problems noticed so far. It is very efficient, but I know tricks to make it even faster now.

Paul Baumann


-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Travis Griggs
Sent: Friday, March 12, 2010 2:59 AM
To: VWNC NC
Subject: [vwnc] New VW diff engine (was: One liner challenge)

On Mar 11, 2010, at 10:46 PM, Travis Griggs wrote:

> 3) Abuse of the new differences API: that shows up in tomorrows dev
> build:
>
> (string differences: (string reject: [:c | '()' includes: c]))
>       fold: [:a :b | a , (a last = $( ifTrue: [b collect: #asUppercase]
> ifFalse: [b])]

You can read more about the new differences: method here:

http://www.cincomsmalltalk.com/userblogs/travis/blogView?showComments=true&printTitle=How_to_get_a_Human_from_a_Chimpanzee&entry=3445815344

--
Travis Griggs
Objologist
"There are a thousand hacking at the branches of evil to one who is striking at the root" - Henry David Thoreau


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc


This message may contain confidential information and is intended for specific recipients unless explicitly noted otherwise. If you have reason to believe you are not an intended recipient of this message, please delete it and notify the sender. This message may not represent the opinion of IntercontinentalExchange, Inc. (ICE), its subsidiaries or affiliates, and does not constitute a contract or guarantee. Unencrypted electronic mail is not secure and the recipient of this message is expected to provide safeguards from viruses and pursue alternate means of communication where privacy or a binding message is desired.

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc

String-codeScanningExtensions.st (6K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: One liner challenge

Jason Rogers-4
In reply to this post by Paul Baumann
As much as I love Smalltalk, Ruby (and other scripting languages like
it) have the upper hand here:

    s = "This is a (string) with some (parenthetical fields)"
    s.gsub(/\((.+?)\)/) { |e| e.upcase }

Not that you couldn't do something as simple (elegant?) as this in
Smalltalk -- it's just not built-in to most distributions.

On Fri, Mar 12, 2010 at 10:52 AM, Paul Baumann <[hidden email]> wrote:

>
> Here is an example of a single statement #inject:into: with simple blocks and (very basic) support for nested parenthesis of other types.
>
> ('This is a (string) with some (parenthetical fields)'
>        inject: (Array with: (WriteStream on: String new) with: 0 asValue)
>        into: [:streamAndBraceLevel :chr |
>                ('([{<' includes: chr)
>                        ifTrue: [streamAndBraceLevel last value: streamAndBraceLevel last value + 1].
>                (')]}>' includes: chr)
>                        ifTrue: [streamAndBraceLevel last value: streamAndBraceLevel last value - 1].
>                streamAndBraceLevel first
>                        nextPut: (streamAndBraceLevel last value > 0 ifTrue: [chr asUppercase] ifFalse: [chr]).
>                streamAndBraceLevel
>        ]) first contents.
>
>  'This is a (STRING) with some (PARENTHETICAL FIELDS)'
>
> More elegant solutions are available without the single line/statement requirement or when using Smalltalk extensions.
>
> Paul Baumann
>
>
> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On Behalf Of Travis Griggs
> Sent: Thursday, March 11, 2010 8:26 PM
> To: VWNC NC
> Subject: [vwnc] One liner challenge
>
> I was looking at someone's ruby/regex code. I could do the equivalent of what they were doing with the 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
> [hidden email]
> "The dissenter is every human being at those moments of his life when he resigns momentarily from the herd and thinks for himself." - Archibald MacLeish, poet and librarian
>
>
>
> _______________________________________________
> vwnc mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
>
>
> This message may contain confidential information and is intended for specific recipients unless explicitly noted otherwise. If you have reason to believe you are not an intended recipient of this message, please delete it and notify the sender. This message may not represent the opinion of IntercontinentalExchange, Inc. (ICE), its subsidiaries or affiliates, and does not constitute a contract or guarantee. Unencrypted electronic mail is not secure and the recipient of this message is expected to provide safeguards from viruses and pursue alternate means of communication where privacy or a binding message is desired.
>
>
> _______________________________________________
> vwnc mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
>



--
Jason Rogers

"I am crucified with Christ: nevertheless I live;
yet not I, but Christ liveth in me: and the life
which I now live in the flesh I live by the faith of
the Son of God, who loved me, and gave
himself for me."
    Galatians 2:20

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: One liner challenge

Alan Knight-2
Well, the challenge did specifically disallow regexes. And I'm not sure that regex line-noise qualifies as "elegantly" :-) But since this is the VWNC list, and this distribution does have it built in...

  s := 'This is a (string) with some (parenthetical fields)'.
  s copyWithRegex: '\([^)]+\)' matchesTranslatedUsing: [:x | x asUppercase].

I couldn't figure out the particular regex you were using, and it seems to be a slightly different regex variation than Regex11 likes, so I wrote it slightly differently. Seems to work fine for this example. I think it could be simplified further by dropping the trialing \), depending on how you think 'This isn't (properly closed.' should capitalize.

At 11:52 AM 2010-03-12, Jason Rogers wrote:
As much as I love Smalltalk, Ruby (and other scripting languages like
it) have the upper hand here:

    s = "This is a (string) with some (parenthetical fields)"
    s.gsub(/\((.+?)\)/) { |e| e.upcase }

Not that you couldn't do something as simple (elegant?) as this in
Smalltalk -- it's just not built-in to most distributions.

On Fri, Mar 12, 2010 at 10:52 AM, Paul Baumann <[hidden email]> wrote:
>
> Here is an example of a single statement #inject:into: with simple blocks and (very basic) support for nested parenthesis of other types.
>
> ('This is a (string) with some (parenthetical fields)'
>        inject: (Array with: (WriteStream on: String new) with: 0 asValue)
>        into: [:streamAndBraceLevel :chr |
>                ('([{<' includes: chr)
>                        ifTrue: [streamAndBraceLevel last value: streamAndBraceLevel last value + 1].
>                (')]}>' includes: chr)
>                        ifTrue: [streamAndBraceLevel last value: streamAndBraceLevel last value - 1].
>                streamAndBraceLevel first
>                        nextPut: (streamAndBraceLevel last value > 0 ifTrue: [chr asUppercase] ifFalse: [chr]).
>                streamAndBraceLevel
>        ]) first contents.
>
>  'This is a (STRING) with some (PARENTHETICAL FIELDS)'
>
> More elegant solutions are available without the single line/statement requirement or when using Smalltalk extensions.
>
> Paul Baumann
>
>
> -----Original Message-----
> From: [hidden email] [[hidden email]] On Behalf Of Travis Griggs
> Sent: Thursday, March 11, 2010 8:26 PM
> To: VWNC NC
> Subject: [vwnc] One liner challenge
>
> I was looking at someone's ruby/regex code. I could do the equivalent of what they were doing with the 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
> [hidden email]
> "The dissenter is every human being at those moments of his life when he resigns momentarily from the herd and thinks for himself." - Archibald MacLeish, poet and librarian
>
>
>
> _______________________________________________
> vwnc mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
>
>
> This message may contain confidential information and is intended for specific recipients unless explicitly noted otherwise. If you have reason to believe you are not an intended recipient of this message, please delete it and notify the sender. This message may not represent the opinion of IntercontinentalExchange, Inc. (ICE), its subsidiaries or affiliates, and does not constitute a contract or guarantee. Unencrypted electronic mail is not secure and the recipient of this message is expected to provide safeguards from viruses and pursue alternate means of communication where privacy or a binding message is desired.
>
>
> _______________________________________________
> vwnc mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
>



--
Jason Rogers

"I am crucified with Christ: nevertheless I live;
yet not I, but Christ liveth in me: and the life
which I now live in the flesh I live by the faith of
the Son of God, who loved me, and gave
himself for me."
    Galatians 2:20

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc

--
Alan Knight [|], Engineering Manager, Cincom Smalltalk

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: One liner challenge

Colin Putney, DeepCove Labs (YVR)
In reply to this post by Jason Rogers-4
Jason Rogers wrote:

> As much as I love Smalltalk, Ruby (and other scripting languages like
> it) have the upper hand here:
>
>    s = "This is a (string) with some (parenthetical fields)"
>    s.gsub(/\((.+?)\)/) { |e| e.upcase }
>
> Not that you couldn't do something as simple (elegant?) as this in
> Smalltalk -- it's just not built-in to most distributions.

The challenge was to do it *without* regex. Want to try again?

Colin

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: One liner challenge

Jason Rogers-4
Should have read the whole thing...

Here it is in Ruby without regular expressions...

    s.split(' ').collect{|e| e.rindex('(') ? e.tr('a-z', 'A-Z') : e}.join(' ')

That said, I still prefer Smalltalk. My only point is scripting
exercises like this are not as simple in Smalltalk, in my opinion.

On Fri, Mar 12, 2010 at 12:19 PM, Colin Putney, DeepCove Labs (YVR)
<[hidden email]> wrote:

> Jason Rogers wrote:
>
>> As much as I love Smalltalk, Ruby (and other scripting languages like
>> it) have the upper hand here:
>>
>>    s = "This is a (string) with some (parenthetical fields)"
>>    s.gsub(/\((.+?)\)/) { |e| e.upcase }
>>
>> Not that you couldn't do something as simple (elegant?) as this in
>> Smalltalk -- it's just not built-in to most distributions.
>
> The challenge was to do it *without* regex. Want to try again?
>
> Colin
>
> _______________________________________________
> vwnc mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
>



--
Jason Rogers

"I am crucified with Christ: nevertheless I live;
yet not I, but Christ liveth in me: and the life
which I now live in the flesh I live by the faith of
the Son of God, who loved me, and gave
himself for me."
    Galatians 2:20

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: One liner challenge

Michel Tilman
In reply to this post by Paul Baumann
Not the most generic or efficient, but works for this case and is kind of fun nonetheless (tested in vw7.6).

('This is a (string) with some (parenthetical fields)'
        inject: false -> String new
        into: [ :assoc :ch | (ch == $( or: [ assoc key and: [ ch ~~ $) ]]) -> (assoc value copyWith: (ch perform: (#(? yourself asUppercase) at: assoc key hash))) ]) value

 'This is a (STRING) with some (PARENTHETICAL FIELDS)'

michel

> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On Behalf Of Travis Griggs
> Sent: Thursday, March 11, 2010 8:26 PM
> To: VWNC NC
> Subject: [vwnc] One liner challenge
>
> I was looking at someone's ruby/regex code. I could do the equivalent of what they were doing with the 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
> [hidden email]
> "The dissenter is every human being at those moments of his life when he resigns momentarily from the herd and thinks for himself." - Archibald MacLeish, poet and librarian
>
>
>
> _______________________________________________
> vwnc mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
>
>
> This message may contain confidential information and is intended for specific recipients unless explicitly noted otherwise. If you have reason to believe you are not an intended recipient of this message, please delete it and notify the sender. This message may not represent the opinion of IntercontinentalExchange, Inc. (ICE), its subsidiaries or affiliates, and does not constitute a contract or guarantee. Unencrypted electronic mail is not secure and the recipient of this message is expected to provide safeguards from viruses and pursue alternate means of communication where privacy or a binding message is desired.
>
>
> _______________________________________________
> vwnc mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
>


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
jas
Reply | Threaded
Open this post in threaded view
|

Re: One liner challenge

jas
In reply to this post by Travis Griggs-3
At 05:26 PM 3/11/2010, Travis Griggs wrote:
>I was looking at someone's ruby/regex code. I could do the equivalent  
>of what they were doing with the 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


Taking (tersely/elegantly) to mean inversely proportional:


iou :=
[:i :o :u| | rs ws |
 rs := i readStream.
 ws := o writeStream.
 [ rs atEnd
 ] whileFalse:
   [ws ,, (rs through: u first)
       ,, (rs throughMatching: u) asUppercase
   ].
 o orIfNil: [ws contents]
].

iou value: 'This is a (string) with some (parenthetical fields)'
    value: nil
    value: '()'
 

>turn them into
>
>'This is a (STRING) with some (PARENTHETICAL FIELDS)'


The extensions are as useful as they are obvious,
with the possible exception of

  UndefinedObject>>writeStream
    ^WriteStream on: String new

which is so wrong it could stay hidden for *decades*.

Cheers,

-cstb

---

Highly subjective inflamatory BS.
Or votes for challenge winners...

Eliot: terse and clean.
       So much so, I have to wonder (as usual)
       why the rest of us bothered to continue?

Martin: clean and thorough.

Travis: good show. and a triple crown, to boot.
        toy soldier indeed.

.*: lowercase arrogant bastard wannabe's, the whole lot.  ;-)
    but interesting, nonetheless.



_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc