[表示 : 全て 最新50 1-99 101- 201- 301- 401- 501- 601- 701- 2chのread.cgiへ]
Update time : 05/14 13:02 / Filesize : 255 KB / Number-of Response : 780
[このスレッドの書き込みを削除する]
[+板 最近立ったスレ&熱いスレ一覧 : +板 最近立ったスレ/記者別一覧] [類似スレッド一覧]


↑キャッシュ検索、類似スレ動作を修正しました、ご迷惑をお掛けしました

【激突】関数型言語 VS オブジェクト指向言語2



579 名前:デフォルトの名無しさん mailto:sage [2012/04/28(土) 23:31:57.42 ]
Scala待ちの間にw、Squeak Smalltalkでセルがオブジェクト版。(要クラスブラウザ)
細かい違いを無視すれば Python の >>557 とほぼ同じ方針。

Object subclass: #Cell
   instanceVariableNames: 'neighbors next'
Cell >> setBit: int
   neighbors := OrderedCollection with: int
Cell >> collectNeighborsAt: pos in: array2D
   neighbors addAll: (#(-1 0 1) gather: [:dx | #(-1 0 1) collect: [:dy |
      (array2D atWrap: (pos y + dy)) atWrap: (pos x + dx)]]); remove: self
Cell >> calcNext
   next := (neighbors inject: 0 into: [:sum :neigh | sum + neigh value])
      caseOf: {[3]->[1]. [4]->[self value]} otherwise: [0]
Cell >> update
   neighbors at: 1 put: next
Cell >> value
   ^neighbors first
Cell >> printOn: stream
   self value printOn: stream
Cell class >> newWith: bitInt
   ^self new setBit: bitInt; yourself
Cell class >> newLatticeFrom: array2D
   | lattice |
   lattice := array2D collect: [:row | row collect: [:bit | Cell newWith: bit]].
   ^lattice doWithIndex: [:row :y | row doWithIndex: [:cell :x |
      cell collectNeighborsAt: x@y in: lattice]]; yourself
Cell class >> updateLattice: array2D
   ^array2D do: [:row | row do: #calcNext]; do: [:row | row do: #update]

| lattice |
lattice := Cell newLatticeFrom: {{0. 1. 1. 1. 0}. {0. 1. 0. 0. 0}. {0. 0. 1. 0. 0}. {0. 0. 0. 0. 0}. {0. 0. 0. 0. 0}}.
Cell updateLattice: lattice. "=> {{0. 1. 1. 0. 0}. {0. 1. 0. 1. 0}. {0. 0. 0. 0. 0}. {0. 0. 0. 0. 0}. {0. 0. 1. 0. 0}} "






[ 続きを読む ] / [ 携帯版 ]

全部読む 前100 次100 最新50 [ このスレをブックマーク! 携帯に送る ] 2chのread.cgiへ
[+板 最近立ったスレ&熱いスレ一覧 : +板 最近立ったスレ/記者別一覧]( ´∀`)<255KB

read.cgi ver5.27 [feat.BBS2 +1.6] / e.0.2 (02/09/03) / eucaly.net products.
担当:undef