- 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}} "
|

|