1c. Smalltalk Collections

1 Intro

 1.1 Purpose of Collections

 1.2 Kinds of Collections

 1.3 Bags, Sets

 1.4 Dictionaries

 1.5 Indexed Collections

 1.6 Arrays

 1.7 OrderedCollections

 1.8 ByteArrays, Strings, Symbols

 1.9 Intervals

2 Collection Protocol

 2.1 Basic Collection Protocol

 2.1.1 Creation Messages

 2.1.2 Iteration Messages

 2.1.3 Conversion Messages

 2.1.4 Testing Messages

 2.2 Modifying while Iterating

 3 Using add:

 3.1 How to use the add: message?

 3.2 Why does add: return its parameter?

 4 Using inject:into:

 4.1 Using inject:into: for Summing

 4.2 Using inject:into: for forming products

 4.3 Injecting Collections

 4.4 Simulating collect: and select: with inject:into:

 4.5 Using inject:into: to find a maximum value

 5 Hashed Collections

 5.1 Intro

 5.2 How do hashed collections work?

 5.3 (Re)Implement hash method

 6 Bags and Sets

 6.1 Concatenate Message (#,) for Bags and Sets

 7 OrderedCollections

 7.1 Size of an OrderedCollection

 7.2 Capacity of an OrderedCollection

 8 SortedCollection

 9 Strings

 9.1 Streams vs. Concatenation

 9.2 Regular Expressions

 10 Symbols

 10.1 Comparing Symbols vs. Comparing Strings

1 Intro

1.1 Purpose of Collections

1.2 Kinds of Collections

1.3 Bags, Sets

1.4 Dictionaries

1.5 Indexed Collections

1.6 Arrays

1.7 OrderedCollections

1.8 ByteArrays, Strings, Symbols

1.9 Intervals

2 Collection Protocol

2.1 Basic Collection Protocol

2.1.1 Creation Messages

2.1.2 Iteration Messages

2.1.3 Conversion Messages

2.1.4 Testing Messages

2.2 Modifying while Iterating

3 Using add:

3.1 How to use the add: message?

" Being burnt by add: "
| items |
items := Set new add: 1.
items add: 2.
^ items
  • add: message answers its parameter, rather than self.
  • Use yourself message.
  • " Using the yourself message "
    | items |
    items := Set new
       add: 1;
       yourself.
    items add: 2.
    ^items

    " Using the yourself message "
    ^Set new
       add: 1;
       add: 2;
       yourself
     

    3.2 Why does add: return its parameter?

    4 Using inject:into:

    4.1 Using inject:into: for Summing

    Iteraton Injected value Element value Result
    1 0 1 1
    2 1 2 3
    3 3 3 6
    4 6 4 10
    5 10 5 15

    4.2 Using inject:into: for forming products

    " n Factorial using inject:into: "
    | n |
    n := 10.
    (1 to: n)
      inject: 1
      into: [ :inj :ele | inj * ele ]
    " Answer: 3628800 "

    4.3 Injecting Collections

    4.4 Simulating collect: and select: with inject:into:

    4.5 Using inject:into: to find a maximum value

    5 Hashed Collections

    5.1 Intro

    5.2 How do hashed collections work?

    5.3 (Re)Implement hash method

    6 Bags and Sets

    6.1 Concatenate Message (#,) for Bags and Sets

    7 OrderedCollections

    7.1 Size of an OrderedCollection

    7.2 Capacity of an OrderedCollection

    8 SortedCollection

    9 Strings

    9.1 Streams vs. Concatenation

    9.2 Regular Expressions

    10 Symbols

    10.1 Comparing Symbols vs. Comparing Strings

    Expression Squeak 2.6 (NT)
    symbolOne == symbolTwo 170
    stringOne = stringTwo 561
    symbolOne == stringOne asSymbol 2123
    stringOne asSymbol == stringTwo asSymbol  4066