help-smalltalk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-smalltalk] Re: [poll] regex literals


From: Paolo Bonzini
Subject: [Help-smalltalk] Re: [poll] regex literals
Date: Thu, 14 Feb 2008 09:18:29 +0100
User-agent: Thunderbird 2.0.0.9 (Macintosh/20071031)


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 :)

Note that it's just a special case of Collections:

        'foobar' select: 'aeiou' => 'ooa'

In fact, "#(1 1.2 2) select: (1 to: 10)" would *not* include 1.2 in the result.

My desire is to allow the common idea of "select: #odd" without implementing Symbol>>#value:. I see no need to implement #sendingBlock (all this IMHO of course) if you reason that:

1) right now, #select: and #collect: have the same "protocol" for the argument, but the two are very different. In the case of #select:/#reject: the argument should return true/false for any collection; for #collect: instead the argument should return an object in the same domain as the source.

Taking an extreme position: #value: is the most overloaded method in Smalltalk and the less you use it, the better. :-) (Because then you can achieve more polymorphism and more DWIM).

2) therefore, I decide that #select: (and #reject:) accept a different thing than a block, a "predicate". A predicate can be a unary block of course, but also a symbol, a regex, a collection, ... I chose #~ as the message that the predicate protocol would implement because it's what we use for regexes, but it's not necessary to implement it with that name (also because we currently have "aString ~ aRegex", not the other way round).

3) the same could apply to #collect:, but with a *different* message to emphasize that the argument is not a "predicate", it is an "xyz" (name to be decided :-) I didn't find any good one). I don't have very strong ideas on how to call the message, but it also could apply to symbols, regexes and collections: for example

  #('1.2' '3.4') collect: #allButLast => #('1.' '3.')

  #('1.2' '3.4') collect: '^.*\.' asRegex => #('1.' '3.')
  #('1.2' '3.4') collect: '\.(.*)' asRegex => #('2' '4')

  #('foo' 'bar') collect: #(1 3) => #('fo' 'br')

NoCandy.Presrc.MessageMacro subclass: SelectLiteralBlocks [
    <pool: NoCandy.Presrc>      "eh?"

You mean <import: ...> here?

On a side note, with Unicode, #∋ would be a good name for #~, or maybe
#includes: :)

Now what Unicode symbols would be binary messages, and which would be okay for identifiers/keywords? :-)

Paolo




reply via email to

[Prev in Thread] Current Thread [Next in Thread]