Language detail: Smalltalk

Coverage: 100.00%
number of '+' ratings
contribution for coverage

Unsolved challenges

None

codes

Feed

Used modules

next >>

LL Golf Hole 9 - トラックバックを打つ (Nested Flatten)

Squeak Smalltalk で。

1
2
3
4
| url pingDict |
url := 'http://ll.jus.or.jp/2008/blog/archives/38/trackback'.
pingDict := {#url -> #('http://ja.doukaku.org/207/')} as: Dictionary.
^(HTTPSocket httpPostDocument: url args: pingDict) contents
文字列型日時ののN秒後時間取得 (Nested Flatten)

Squeak Smalltalk では日時オブジェクト同士の演算が可能です。

1
2
3
4
5
"文字列指定した日時の場合"
'2008-09-01T12:00:00+09:00' asDateAndTime - 10 seconds   "=> 2008-09-01T11:59:50+09:00 "

"現在日時の場合"
DateAndTime now + 15 seconds
LL Golf Hole 8 - 横向きのピラミッドを作る (Nested Flatten)

Squeak Smalltalk で。

1
2
3
4
5
6
| n |
n := 4.

World findATranscript: nil.
1 to: n*2-1 do: [:m |
    Transcript cr; show: (String new: {m. n*2-m} min withAll: $*)]
LL Golf Hole 7 - バイト数を読みやすくする (Nested Flatten)

Squeak Smalltalk で。

1
2
3
4
5
6
7
| byte |
byte := 123456789012345.
^(#('' k M G T P E Z) inject: byte into: [:result :unit |
    result < 1024 ifTrue: [^result asString, unit].
    result / 1024 roundTo: 0.1]) asString, 'Y'

"=> '112.3T' "
LL Golf Hole 6 - 10進数を2進数に基数変換する (Nested Flatten)

#7293 の感動を Squeak Smalltalk で(一部を不完全ながら(^_^;))表現してみました。w

1
2
3
4
5
6
7
| b |
b := [:n |
    | c |
    c := OrderedCollection new: n * 2.
    c add: '1'; do: [:e | (c add: e, '0'; add: e, '1'; size) > n ifTrue: [^c at: n]]].

b value: 10   "=> '1010' "

Squeak Smalltalk で。

1
10 radix: 2   "=> '1010' "
LL Golf Hole 5 - 最上位の桁を数え上げる (Nested Flatten)

Squeak Smalltalk で。

1
2
3
4
5
6
7
| n m |
n := 300.
m := 0.
World findATranscript: nil.
[m <= n] whileTrue: [
    Transcript cr; show: m.
    m := m + (10 raisedTo: m asString size - 1)]
echoクライアント (Nested Flatten)

Squeak Smalltalk で。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
| serverAddress portNumber socket string |
serverAddress := NetNameResolver addressFromString: '127.0.0.1'.
portNumber := 9999.
socket := Socket newTCP.
socket connectTo: serverAddress port: portNumber.
World findATranscript: nil.
[(string := FillInTheBlank request: 'string:') notEmpty] whileTrue: [
    socket sendData: string, String crlf.
    Transcript show: socket receiveData].
socket close
LL Golf Hole 4 - 文章から単語の索引を作る (Nested Flatten)

Squeak Smalltalk で。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
| dict stream delimiters lineCount |
dict := Dictionary new.
stream := HTTPSocket httpGet: 'http://www.gnu.org/licenses/gpl.txt'.
delimiters := Character allCharacters reject: [:each | each isLetter].
lineCount := 0.
[stream atEnd] whileFalse: [
    lineCount := lineCount + 1.
    ((stream upTo: Character lf) subStrings: delimiters) do: [:word | 
        | lines |
        lines := dict at: word ifAbsentPut: [OrderedCollection new].
        lines add: lineCount]].
^dict
LL Golf Hole 3 - 13日の金曜日を数え上げる (Nested Flatten)

Squeak Smalltalk で。

1
2
3
4
5
| fridayThe13ths |
fridayThe13ths := (Date today to: 2013 asYear end) dates 
    select: [:date | date dayOfMonth = 13 and: [date dayOfWeekName = #Friday]].
World findATranscript: nil.
^fridayThe13ths do: [:each | Transcript cr; show: each]; size
tailの実装 (Nested Flatten)

Squeak Smalltalk で、それっぽい動きをするものを。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| filename file numOfLines isFollowMode lineCount savedPosition |
filename := 'in.txt'.
numOfLines := 10.
isFollowMode := false.

file := FileStream fileNamed: filename.
file setToEnd.
file binary.
lineCount := 0.
[file position > 0 and: [lineCount < numOfLines]]
    whileTrue: [file back = 13 ifTrue: [lineCount := lineCount + 1]].

file ascii.
World findATranscript: nil.
Transcript cr; show: file upToEnd.
savedPosition := file position.
file close.
isFollowMode ifTrue: [
    [Sensor keyboardPressed] whileFalse: [
        (Delay forSeconds: 5) wait.
        file reopen; position: savedPosition.
        Transcript show: file upToEnd.
        savedPosition := file position.
        file close]]
LL Golf Hole 2 - 文字列に含まれる単語の最初の文字を大文字にする (Nested Flatten)

Squeak Smalltalk で、組み込みのエディタ(a ParagraphEditor)に委譲してみる~の巻。

ゴルフに参加する(短くする)気ゼロで済みません汗。

1
2
3
4
5
| in editor |
in := 'this is a pen'.
editor := ParagraphEditor newParagraph: in asParagraph.
editor selectAll; makeCapitalized: nil.
^editor text asString   "=> 'This Is A Pen' "
GST で stdin → stdout。
1
P:=$ .stdin do:[:c|C:=c.'',{P}~'\w'or:[C:=c asUppercase].P:=C display]

Squeak Smalltalk で。

1
2
3
4
5
6
| in prev |
in := 'LL day and night'.
prev := $-.
^in collect: [:char | prev := prev isLetter ifTrue: [char] ifFalse: [char asUppercase]]

"=> 'LL Day And Night' "
lessの実装 (Nested Flatten)

Squeak Smalltalk で。

 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
31
32
33
34
35
36
37
| file font nLines cr sp goNextPage goPrevPage findNext prevLine |
file := FileStream fileNamed: 'in.txt'.

font := TextStyle defaultFont.
cr := Character cr.
sp := Character space.
nLines := Display height // (font height + font descent).

goNextPage := [nLines timesRepeat: [file nextLine]].
prevLine := [file binary. [file position = 0 or: [file back = 13]] whileFalse. file ascii].
goPrevPage := [nLines timesRepeat: [prevLine value]].
findNext := [:pattern | pattern notEmpty ifTrue: [file upToAll: pattern]. prevLine value].

[   | string |
    string := String streamContents: [:ss |
        | position |
        position := file position.
        nLines timesRepeat: [
            file nextLine ifNotNilDo: [:line | ss nextPutAll: line].
            file peek ifNotNil: [ss cr]].
        file position: position].
    Display fillWhite.
    string asDisplayText display.
    [Sensor keyboardPressed] whileFalse.
    Sensor keyboard caseOf: {
        [$q] -> [file close. ^World restoreDisplay].
        [$g] -> [file reset].
        [$G] -> [file setToEnd].
        [sp] -> [goNextPage value].
        [$f] -> [goNextPage value].
        [$b] -> [goPrevPage value].
        [cr] -> [file nextLine].
        [$e] -> [file nextLine].
        [$y] -> [prevLine value].
        [$/] -> [findNext value: (FillInTheBlank request: 'pattern:')]
    } otherwise: []
] repeat
環境変数の取得 (Nested Flatten)

Squeak Smalltalk で。

特定の値を得るには、返値に対してキーを指定してアクセス(#at:)します。

例: UnixProcess env at: #HOME

1
UnixProcess env
LL Golf Hole 1 - tinyurl.comを使ってURLを短縮する (Nested Flatten)

Squeak Smalltalk で。

1
(HTTPSocket httpGet: 'tinyurl.com/api-create.php?url=http://ll.jus.or.jp/2008/info/xgihyo') contents
2次元ランダムウォーク (Nested Flatten)

Squeak Smalltalk で。

1
2
3
4
5
6
7
8
| pen origin out time |
pen := Pen new defaultNib: 1; color: Color red; place: (origin := Display center).
out := FileStream fileNamed: 'out.txt'.
time := 0.
[(time := time + 1) < 1e3] whileTrue: [
    out nextPutAll: time printString; tab; nextPutAll: (pen location - origin) printString; cr.
    pen turn: 360 atRandom; go: 5].
out edit
コード圧縮 (Nested Flatten)

Squeak Smalltalk で。

1
2
3
4
5
6
7
| method |
method := Integer >> #factorial.
^method methodClass prettyPrinterClass
    format: method getSource
    in: method methodClass
    notifying: nil
    decorated: false.
クリップボードへの転送 (Nested Flatten)

Squeak Smalltalk で。

1
2
Clipboard clipboardText: 'クリップボードへ'.
Clipboard clipboardText  "クリップボードから"
next >>

Index

Feed

Other

Link

Pathtraq

loading...