Paolo Bonzini <[EMAIL PROTECTED]> 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"
        ^{'[EMAIL PROTECTED] to: [EMAIL PROTECTED]' -> '[:`g1 | `g1 between: 
[EMAIL PROTECTED] and: [EMAIL PROTECTED]'
          -> [:m |
              ((m atAll: #('[EMAIL PROTECTED]' '[EMAIL PROTECTED]')) 
allSatisfy: [:each |
                   each isLiteral and: [each value isInteger]])
                  ifTrue: [{'`g1' -> self newVariable}]].
          '[EMAIL PROTECTED]' -> '[:`g1 | `g1 `sel]'
          -> [:m | | sel |
              sel := m at: '[EMAIL PROTECTED]'.
              {sel isLiteral.
               sel value isSymbol.
               sel value numArgs = 0}
                  condEvery ifTrue: [{'`g1' -> self newVariable.
                                      #'`sel' -> sel value}]].
          '[EMAIL PROTECTED]' -> '[:`g1 | `g1 ~ [EMAIL PROTECTED]'
          -> [:m | | x |
              x := m at: '[EMAIL PROTECTED]'.
              (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


_______________________________________________
help-smalltalk mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/help-smalltalk

Reply via email to