【激突】関数型言語 VS オブジェクト指向言語2at TECH【激突】関数型言語 VS オブジェクト指向言語2 - 暇つぶし2ch■コピペモード□スレを通常表示□オプションモード□このスレッドのURL■項目テキスト578:デフォルトの名無しさん 12/04/28 23:11:10.96 ん? Scala知らないの? 579:デフォルトの名無しさん 12/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}} " 次ページ最新レス表示レスジャンプ類似スレ一覧スレッドの検索話題のニュースおまかせリストオプションしおりを挟むスレッドに書込スレッドの一覧暇つぶし2ch