What am I doing wrong here?

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

What am I doing wrong here?

Bram Neijt
Hi all,

Sorry for the bad subject, but I really couldn't think of any better one.

The following puzzles me (in gst):
st> 'a/b' indexOf: '/' ifAbsent: ['ABSENT' printNl]!
'ABSENT'
'ABSENT'

What am I doing wrong here?

(I am just a beginner, so any kind of awnser is welcome)

Greetings,
  Bram

PS Does anybody know of (or own) a windows port of GNU Smalltalk with
gtk support?


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: unexpected #indexOf:ifAbsent: result (was What am I doing wrong here?)

S11001001
Bram Neijt wrote:
> The following puzzles me (in gst):
> st> 'a/b' indexOf: '/' ifAbsent: ['ABSENT' printNl]!
> 'ABSENT'
> 'ABSENT'

Here is the doc for (String whichClassIncludesSelector:
#indexOf:ifAbsent:) >> #indexOf:ifAbsent:

indexOf: anElement ifAbsent: exceptionBlock
     Answer the index of the first occurrence of anElement in the
     receiver.  Invoke exceptionBlock and answer its result if no item
     is found

Now, ask yourself, what does "anElement" mean in the context of
Strings?  Hint: the element '/' is in fact *not* present in the String
'a/b'.  Also look around SequenceableCollection's 'basic' methods for
the method that does what you expect here.

--
Stephen Compall
http://scompall.nocandysw.com/blog


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: unexpected #indexOf:ifAbsent: result (was What am I doing wrong here?)

Mike Anderson-3
Stephen Compall wrote:

> Bram Neijt wrote:
>
>>The following puzzles me (in gst):
>>st> 'a/b' indexOf: '/' ifAbsent: ['ABSENT' printNl]!
>>'ABSENT'
>>'ABSENT'
>
>
> Here is the doc for (String whichClassIncludesSelector:
> #indexOf:ifAbsent:) >> #indexOf:ifAbsent:
>
> indexOf: anElement ifAbsent: exceptionBlock
>      Answer the index of the first occurrence of anElement in the
>      receiver.  Invoke exceptionBlock and answer its result if no item
>      is found
>
> Now, ask yourself, what does "anElement" mean in the context of
> Strings?  Hint: the element '/' is in fact *not* present in the String
> 'a/b'.  Also look around SequenceableCollection's 'basic' methods for
> the method that does what you expect here.

I'm replying in more detail because (a) this isn't the finest part of
the class library (b) you used the word "element", which I think might
be a little confusing.

Bram, the method #indexOf:ifAbsent: comes from SequenceableCollection. A
String is a Collection of Characters, but '/' is not a Character (it is
also a String), so '/' is not an element of 'a/b', nor of any String.

What you wanted was either:

'a/b' indexOf: $/ ifAbsent: [ 'ABSENT' printNl ]!

or

'a/b' indexOf: '/' matchCase: true startingAt: 1

or

'a/b' indexOfSubCollection: '/'

I said this wasn't the finest part of the class library, because (a) you
might reasonably expect #indexOf:ifAbsent: and
#indexOf:matchCase:startingAt: to be variations on a theme, and they're
not; and (b) they're all pretty wordy when all you're wanting to do is
some string wrangling.

My advice, if you are working a lot with strings, is to add a method
like this:

CharacterArray methodsFor: 'syntactic sugar'!

% aSubString
        ^self indexOfSubCollection: aSubString
! !

Now you have:

st> 'a/b' % '/' !
2

which saves a lot of typing.

Regards,

Mike


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: unexpected #indexOf:ifAbsent: result (was What am I doing wrong here?)

Paolo Bonzini

> I said this wasn't the finest part of the class library, because (a) you
> might reasonably expect #indexOf:ifAbsent: and
> #indexOf:matchCase:startingAt: to be variations on a theme, and they're
> not; and (b) they're all pretty wordy when all you're wanting to do is
> some string wrangling.
>  
Yeah, I remember thinking about the same when I ported this from IBM
Smalltalk.  Which makes me think it's now exactly 10 years I've been
working on GNU Smalltalk.  Wow.

> My advice, if you are working a lot with strings, is to add a method
> like this:
>
> CharacterArray methodsFor: 'syntactic sugar'!
>
> % aSubString
> ^self indexOfSubCollection: aSubString
> ! !
>
> Now you have:
>
> st> 'a/b' % '/' !
> 2
>  
While I agree entirely with you, why the "%"? :-P

Paolo


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: unexpected #indexOf:ifAbsent: result (was What am I doing wrong here?)

Bram Neijt
Hi all,

First of, thanks to Stephen, Mike and Paolo for their both quick and
full replies to my question.

Having done some further reading into aString, I got the whole picture
and actually quite like the whole collection/element splitting of
functions. Although having a special notation might help, I think
redablility helps even more.

Be sure, your awnsers won't go undocumented. Appart from the online
access to this mailing list, I'm trying to write some documentation
while I learn smalltalk at smalltalk.infosnel.nl. As soon as it is
(almost) fully done, I'll post a message. If you have comments on
that, you can mail me personally.

Greetings,
  Bram


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: unexpected #indexOf:ifAbsent: result (was What am I doing wrong here?)

Mike Anderson-3
In reply to this post by Paolo Bonzini
Paolo Bonzini wrote:

>
>> I said this wasn't the finest part of the class library, because (a) you
>> might reasonably expect #indexOf:ifAbsent: and
>> #indexOf:matchCase:startingAt: to be variations on a theme, and they're
>> not; and (b) they're all pretty wordy when all you're wanting to do is
>> some string wrangling.
>>  
>
> Yeah, I remember thinking about the same when I ported this from IBM
> Smalltalk.  Which makes me think it's now exactly 10 years I've been
> working on GNU Smalltalk.  Wow.

Congratulations, I think.

*dons party hat*

>> My advice, if you are working a lot with strings, is to add a method
>> like this:
>>
>> CharacterArray methodsFor: 'syntactic sugar'!
>>
>> % aSubString
>>     ^self indexOfSubCollection: aSubString
>> ! !
>>
>> Now you have:
>>
>> st> 'a/b' % '/' !
>> 2
>>  
>
> While I agree entirely with you, why the "%"? :-P

I was looking for a binary selector. I seriously considered @? but
settled on % because it suggests modulo division... this made sense at
the time :-I

'Interesting' choice of selectors aside, it's really the ease of doing
this that I was trying to draw attention to.

Mike



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