[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-smalltalk] [poll] regex literals
From: |
Stephen Compall |
Subject: |
Re: [Help-smalltalk] [poll] regex literals |
Date: |
Wed, 13 Feb 2008 14:30:56 -0600 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/23.0.50 (gnu/linux) |
Paolo Bonzini <address@hidden> writes:
> However, *always* treating string literals as regexes is going to give
> problems in the long term. In particular, it would break with another
> extension that I was thinking about:
>
> #(1 3 2 6 5 4) select: #odd => #(1 3 5)
This is sort of in the Presource test suite:
#(1 3 2 6 5 4) select: #odd sendingBlock
-| #(1 3 2 6 5 4) select: [:gensym | gensym odd]
=> #(1 3 5)
> #(1 12 2) select: (1 to: 10) => #(1 12)
I would not use that :)
> #('foo' 'bar') select: ##/f./ => #('foo')
>
> This would be quite easily implemented (#select: would send a new
> message to its argument, e.g. #~, instead of #value:).
I would rather have a generalization of the sendingBlock protocol to
send explicitly, perhaps with this extension (because with literals,
there's no chance for confusion):
Eval [NoCandy.MyCodeMindset installIn: Namespace current]
NoCandy.Presrc.MessageMacro subclass: SelectLiteralBlocks [
<pool: NoCandy.Presrc> "eh?"
"obviously you would memoize this result"
SelectLiteralBlocks class >> inlinableActions [
"since, for all these cases, the standard #select: semantics
*obviously* aren't useful"
^{'address@hidden to: address@hidden' -> '[:`g1 | `g1 between:
address@hidden and: address@hidden'
-> [:m |
((m atAll: #('address@hidden' 'address@hidden')) allSatisfy:
[:each |
each isLiteral and: [each value isInteger]])
ifTrue: [{'`g1' -> self newVariable}]].
'address@hidden' -> '[:`g1 | `g1 `sel]'
-> [:m | | sel |
sel := m at: 'address@hidden'.
{sel isLiteral.
sel value isSymbol.
sel value numArgs = 0}
condEvery ifTrue: [{'`g1' -> self newVariable.
#'`sel' -> sel value}]].
'address@hidden' -> '[:`g1 | `g1 ~ address@hidden'
-> [:m | | x |
x := m at: 'address@hidden'.
(x isLiteral and: [x value isRegex])
ifTrue: [{'`g1' -> self newVariable}]].
} collect: [:triplet |
{CodeTemplate fromExpr: triplet key key.
CodeTemplate fromExpr: triplet key value.
triplet value}]
]
expandMessage: sel to: rcv withArguments: args [
| filter |
filter := args first.
self class inlinableActions do: [:triplet | | match expand test |
match := triplet first. expand := triplet second.
test := triplet third.
(match match: filter) ifNotNil: [:pm |
(test value: pm) ifNotNil: [:xtn |
xtn do: [:each | pm add: each].
^STInST.RBMessageNode
receiver: rcv
selector: sel
arguments: {expand expand: pm}]]].
^self forgoExpansion
]
]
#(1 12 2) select: (1 to: 10)
-| #(1 12 2) select: [:gensym | gensym between: 1 and: 10]
=> #(1 2)
On a side note, with Unicode, #∋ would be a good name for #~, or maybe
#includes: :)
--
But you know how reluctant paranormal phenomena are to reveal
themselves when skeptics are present. --Robert Sheaffer, SkI 9/2003