日常备忘。

由于 Pharo 派生自 Squeak,所以很多资源是可以共用的

一些材料

语法备忘

Terse Guide to Squeak

Array(list)

将 list 视为不可变的结构(历史不可变),每次都生成新的.

发挥 LISP 精神,灵活操控 list。

只使用少量操作符:

  • atom
  • eq (x=y)
  • car ([:x |x at: 1.])
  • cdr ([:x |x copyFrom: 2 to: x size.])
  • cons ([:x :y|Array with: x with: y])
  • cond (ifTrue:)
1
2
3
4
5
6
car := [:x |x at: 1.].
cdr := [:x |x copyFrom: 2 to: x size.].
cons:= [:x :y|Array with: x with: y].
car value: #(1 2 3 ).
cdr value: #(1 2 3 ).
cons value: #(1 2 3 ) value: 1.

Smalltalk 源码不是S表达式,尽可能将 list 用作 S表达式, 灵活使用 block, 诸如: #([:x |x at:1.], 1 2 3)

1
2
y := {[:x |x at:1.] . 1 . 2 . 3}.
(car value: y) value: (cdr value: y). "1"

由于 Smalltalk 和LISP 一样(Alan Kay是LISP忠实粉丝)是一门关注late binding的语言,所以 list 中可以携带各种东西!

1
2
3
"receiver perform: message."
y := {'factorial' asSymbol . 5 . 6 . 7}.
(car value: (cdr value: y)) perform: (car value: y). "120 (5x4x3x2x1)"

create

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
x := #(4 3 2 1). 

"Array 长度固定"
x := Array with: 5 with: 4 with: 3 with: 2.                 "create array with up to 4 elements"

x := Array new: 4.                                          "allocate an array with specified size"
x                                                           "set array elements"
   at: 1 put: 5;
   at: 2 put: 4;
   at: 3 put: 3;
   at: 4 put: 2.

a := 'hi'
x := {a . 1 . 2} "a 将被解析。 #(a 1 2)->a是symbol"

query && utils

1
2
3
4
5
x isEmpty.
x size.
x includes: 3.
x select: [:a | a > 2].
x do: [:a | Transcript show: a printString; cr].            "iterate over the array"

Dictionaries

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
x := Dictionary new.
x at: #e put: 3.
x at: 'key1' put: 3.
x includesKey: #a.
x keys.
x values.
x do: [:a | Transcript show: a printString; cr]        "iterate over the values collection"
x keysDo: [:a | Transcript show: a printString; cr].   "iterate over the keys collection"
x keysAndValuesDo: [:aKey :aValue | Transcript         "iterate over keys and values"
   show: aKey printString; space;
   show: aValue printString; cr].
x select: [:a | a > 2].       "return collection(dict) of elements that(value) pass test"

Smalltalk globals keys

环境介绍(视频)

Squeak from the very start

常用

Morphic

Morphic

late binding

在持续积累 late binding 相关技能

Dynamic Message Calling/Compiling

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"unary message"
receiver := 5.
message := 'factorial' asSymbol.
result := receiver perform: message.
result := Compiler evaluate: ((receiver storeString), ' ', message).
result := (Message new setSelector: message arguments: #()) sentTo: receiver.

"binary message"
receiver := 1.
message := '+' asSymbol.
argument := 2.
result := receiver perform: message withArguments: (Array with: argument).
result := Compiler evaluate: ((receiver storeString), ' ', message, ' ', (argument storeString)).
result := (Message new setSelector: message arguments: (Array with: argument)) sentTo: receiver.

"keyword messages"
receiver := 12.
keyword1 := 'between:' asSymbol.
keyword2 := 'and:' asSymbol.
argument1 := 10.
argument2 := 20.
result := receiver
   perform: (keyword1, keyword2) asSymbol
   withArguments: (Array with: argument1 with: argument2).
result := Compiler evaluate:
   ((receiver storeString), ' ', keyword1, (argument1 storeString) , ' ', keyword2, (argument2 storeString)).
result := (Message new
      setSelector: (keyword1, keyword2) asSymbol
      arguments: (Array with: argument1 with: argument2))
   sentTo: receiver.

连接

与外部系统通信

OSC

Adatper Squeak

在Squeak中, 下载 OSC : OSC-SimonHolland, 之后拖到 Squeak 桌面,加载使用即可。

1
2
3
4
5
(OSCMessage for: {'/eim/osc' . 1}) sendTo: #[127 0 0 1] port: 12361. 

(OSCMessage for: {'/eim/osc' . 0}) sendTo: #[127 0 0 1] port: 12361.

(OSCMessage for: {'/eim/osc' . -1}) sendTo: #[127 0 0 1] port: 12361. 

Scratch Client

Adapter Etoys

解析工具

参考