Oct 18 1999.

Integer Madness, Smalltalk abuse


While using "string copyFrom: 2 to: string size" is the correct way to create a string without its first character, the Smalltalk newsgroup had some more amusing versions.

{if you dig around in the image it's amazing what you find}

------------------------------------------------------------------------

Re: Deleting the first character of a string Author: David N. Smith

Date: 1997/08/15 Forum: comp.lang.smalltalk

INSANITY ON

One of those 'crazy fields' swept over Danbury as I was reading this thread and I discovered another solution (in IBM Smalltalk at least):

('Now is the time' asByteArray asOrderedCollection 
 removeFirst; yourself) asByteArray asString

Answers: 'ow is the time'

After the field moved on I was ashamed.

Dave

PS: This is no where as bad as what happened the last time the field passed over...

PPS: I live near a major interstate highway with heavy truck traffic; I think they're either moving crashed UFO parts or contaminated nuclear waste.

PPPS: It's Friday.

_____________________________________________

David N. Smith IBM T J Watson Research Center, Hawthorne, NY Mailto: dnsmith@watson.ibm.com
Home Page: http://www.dnsmith.com/
Any opinions or recommendations are those of the author and not of his employer.
_____________________________________________

------------------------------------------------------------------------
Re: Deleting the first character of a String Author: John M McIntosh
Date: 1997/08/16 Forum: comp.lang.smalltalk
David, it must be very late there... you can do better? With VisualWorks try:

| number string newstring |
  string := 'Now is the time'.
  number := Integer new: string size neg: false.
  string asByteArray keysAndValuesDo: 
    [[:i :v | 
     number digitAt: i put: v]].
  number := number bitShift: -8.
  newstring := string species new: number digitLength.
  1 to: newstring size do: 
    [[:i | 
    newstring at: i put: (number digitAt:i) asCharacter]].
^newstring

 

Your's is shorter, but I couldn't resist bitshifting left those bits...
PS It's only friday, but I'm on holidays and have somewhat left my senses...
John M. McIntosh Corporate Smalltalk Consulting Ltd.

------------------------------------------------------------------------
Re: Deleting the first character of a string Author: David N. Smith
Date: 1997/08/15 Forum: comp.lang.smalltalk

I think whatever is in those trucks must come from your town. :-)
 
That is one of the most disgusting bits of Smalltalk I've ever seen. I looked at it and looked at it and swore it couldn't possibly work, and it wasn't until I fired up VW and actually tried it that I found to my horror that it really does. I've never seen any kind of #new: message sent to Integer before. I've never seen such blatant use of internal knowledge and mechanisms. (But isn't that a right shift?) Congratulations!
 ...

------------------------------------------------------------------------
Re: Deleting the first character of a String Author: John Brant

Date: 1997/08/15 Forum: comp.lang.smalltalk


Here's some more disgusting VW examples :). The first extends the large integer idea above:

(('Now is the time' changeClassToThatOf: 16rFFFFFFFF) bitShift: -8)
changeClassToThatOf: ''

By using the changeClassToThatOf: message we can avoid copying the string to the large integer and back to a string. However, this won't work for small strings since the bitShift will result in a SmallInteger which can't change its class. The next example doesn't even create a new string. Instead it uses a lightweight class. While it still contains the extra character, it is unnoticed by the inspectors:

| newClass string |
string := 'Now is the time'.
newClass := string class copy.
newClass superclass: string class.
newClass compile: 'at: index ^super at: index + 1' notifying: nil.
newClass compile: 'basicAt: index ^super basicAt: index + 1' notifying: nil.
newClass compile: 'size ^super size - 1' notifying: nil.
newClass compile: 'basicSize ^super basicSize - 1' notifying: nil.
string changeClassToThatOf: newClass basicNew

I use lightweight classes for setting per object breakpoints; just compile the method with a self halt before sending the message to super.
John Brant