| encode |
encode := [:text | | dictionary next phrase joined output |
 dictionary := Dictionary new.
 dictionary at: 'A' put: 1; at: 'B' put: 2.
 next := 3. phrase := ''. output := OrderedCollection new.
 text do: [:character |
  ('AB' includes: character) ifFalse: [Error signal: 'Use only A and B'].
  joined := phrase, character asString.
  (dictionary includesKey: joined)
   ifTrue: [phrase := joined]
   ifFalse: [
    output add: (dictionary at: phrase).
    next > 256 ifTrue: [Error signal: 'Dictionary capacity reached'].
    dictionary at: joined put: next. next := next + 1.
    phrase := character asString]].
 phrase isEmpty ifFalse: [output add: (dictionary at: phrase)].
 output asArray].
encode value: 'ABABABA'
