Skip to content

Instantly share code, notes, and snippets.

@nattybear
Last active January 22, 2021 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nattybear/c4dd80cb5308794446af46c653656be3 to your computer and use it in GitHub Desktop.
Save nattybear/c4dd80cb5308794446af46c653656be3 to your computer and use it in GitHub Desktop.

이 글은 Canol Gokel님이 만든 Computer Programming using GNU Smalltalk를 읽고 정리한 것이다.

Common Classes and Their Usage: Part 2

Array

new:

클래스 Array에 메세지 new:를 보내면 객체 Array를 만들 수 있다.

Array를 만들 때는 크기를 정해줘야 한다.

st> | anArray |
st> anArray := Array new: 10
(nil nil nil nil nil nil nil nil nil nil )

Array를 생성하면 안의 원소들이 모두 nil로 초기화 된다.

nil은 스몰토크에서 '아무 것도 없음'을 나타내는 객체이다.

at:

Array에서 특정 위치에 있는 원소 하나에 접근하고 싶을 때는 메세지 at:을 사용하면 된다.

st> anArray at:1
nil

Array의 위치는 인덱스 index 라고 부른다.

스몰토크에서 인덱스는 숫자 1부터 시작한다.

at:put:

Array의 특정 위치에 객체를 저장하려면 메세지 at:put:을 사용한다.

st> anArray at: 1 put: 'Toothbrush'
'Toothbrush'

객체 anArray에 어떤 것들이 들어 있는지 보려면 객체 이름 anArray를 입력하면 된다.

st> anArray
('Toothbrush' nil nil nil nil nil nil nil nil nil )

위와 같이 Array의 첫번째 자리에 클래스 String의 인스턴스인 'Toothbrush'가 저장되어 있다.

이번에는 'Soap'를 넣어보자.

st> anArray at: 2 put: 'Soap'
'Soap'
st> anArray
('Toothbrush' 'Soap' nil nil nil nil nil nil nil nil )

includes:

어떤 객체가 Array 안에 들어 있는지 보려면 메세지 include:를 사용한다.

st> anArray includes: 'Soap'
true
st> anArray includes: 'Toothpaste'
false

reverse

Array는 원소를 저장할 때 순서가 있는데 메세지 reverse를 이용하면 순서를 뒤집을 수 있다.

st> anArray reverse
(nil nil nil nil nil nil nil nil 'Soap' 'Toothbrush' )

Set

Set은 저장하는 원소에 순서가 없고 원소의 개수도 제한이 없다.

new

메세지 new를 이용하면 Set 인스턴스를 만들 수 있다. 인자는 필요 없다.

st> | aSet |
st> aSet := Set new
Set ()

처음에 Set을 만들면 안에 아무 것도 들어있지 않다.

add:

메세지 add:를 이용하면 Set 안에 원소를 넣을 수 있다.

st> aSet add: 'Toothbrush'
'Toothbrush'
st> aSet
Set ('Toothbrush' )

이제 Set 안에는 문자열 객체 'Toothbrush'가 들어있다.

문자열 객체를 하나 더 넣어보자.

st> aSet add: 'Soap'
'Soap'
st> aSet
Set ('Toothbrush' 'Soap' )

겉으로 보기에는 원소들에 순서가 있는 것 같지만 사실 순서는 없다.

문자열 객체를 하나 더 넣어보자.

st> aSet add: 'Toothpaste'
'Toothpaste'
st> aSet
Set ('Toothbrush' 'Soap' 'Toothpaste' )

여기서 'Toothbrush'를 한 번 더 넣으면 어떻게 될까?

st> aSet add: 'Toothbrush'
'Toothbrush'
st> aSet
Set ('Toothbrush' 'Soap' 'Toothpaste' )

이미 Set 안에 들어있는 원소는 또 다시 추가되지 않는다.

remove:

Set 안에 들어있는 원소를 제거하려면 메세지 remove:를 사용한다.

st> aSet remove: 'Toothpaste'
'Toothpaste'
st> aSet
Set ('Toothbrush' 'Soap' )

메세지 remove:는 제거한 객체를 리턴한다.

remove:를 사용한다고 해서 그 객체가 시스템에서 완전히 제거되는 것은 아니다. 그냥 해당 원소가 더 이상 Set에 속하지 않게 되는 것 뿐이다.

st> | anElement |
st> anElement := 'Perfume'
'Perfume'

문자열 객체를 가리키는 별도 변수를 하나 만들었다. 이걸 Set에 넣어보자.

st> aSet add: anElement
'Perfume'
st> aSet
Set ('Toothbrush' 'Soap' 'Perfume' )

그리고 이제 remove:로 제거해보자.

st> aSet remove: anElement
'Perfume'
st> aSet
Set ('Toothbrush' 'Soap' )

이제 anElementSet에서 제거됐다. 아까 만든 변수가 아직 살아 있는지 보자.

st> anElement
'Perfume'

아직 살아 있다!

Dictionary

Dictionary의 인덱스에는 숫자만 넣을 수 있는 것은 아니고 어떤 객체라도 넣을 수 있다.

DictionarySet과 마찬가지로 원소들에 순서가 없고 만들 때 길이를 정해주지 않아도 된다.

new

메세지 newDictionary를 만들 수 있다.

st> | aDictionary |
st> aDictionary := Dictionary new
Dictionary (
)

처음 Dictionary를 만들면 안에 아무 것도 들어있지 않다.

at:put:

메세지 at:put:을 이용해서 딕셔너리에 원소를 추가할 수 있다.

st> aDictionary at: 'Canol' put: 'Gokel'
'Gokel'
st> aDictionary
Dictionary (
        'Canol'->'Gokel'
)

문자열 객체를 하나 더 넣어보자.

st> aDictionary at: 'Paolo' put: 'Bonzini'
'Bonzini'
st> aDictionary
Dictionary (
        'Canol'->'Gokel'
        'Paolo'->'Bonzini'
)

keys

메세지 keys를 이용하면 딕셔너리 내에 모든 키를 Set 객체로 리턴한다.

st> aDictionary keys
Set ('Canol' 'Paolo' )

removeKey:

removeKey를 이용하면 딕셔너리에 있는 원소를 제거할 수 있다.

st> aDictionary removeKey: 'Canol'
'Gokel'
st> aDictionary
Dictionary (
        'Paolo'->'Bonzini'
)

Computer Programming with GNU Smalltalk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment