| 27 | | s nextPutAll: self D; nl; nextPutAll: self G! |
| 28 | | D |
| 29 | | ^'Golf', Dic displayString! |
| 30 | | G |
| 31 | | ^String streamContents: [:s| Global associationsDo: [:a| s << a; nl ] ]! |
| | 26 | s nextPutAll: 'Golf'; nextPutAll: Dic printString! |
| | 27 | find: abbr from: list |
| | 28 | | m | |
| | 29 | m := String streamContents: [:s| |
| | 30 | abbr do: [:c| s nextPut: c. c = $: or: [ s nextPut: $* ] ] ]. |
| | 31 | ^(SortedCollection sortBlock: |
| | 32 | [:a :b| a size = b size ifTrue: [ a <= b ] ifFalse: [ a size < b size ] ]) |
| | 33 | addAll: (list select: [:x| m match: x ignoreCase: false ]); yourself! |
| | 34 | abbreviate: sym from: list |
| | 35 | | s | |
| | 36 | s := ((sym subStrings: $:) collect: [:k| |
| | 37 | | h | |
| | 38 | {h := String with: k first}, ((1 to: k size - 1) collect: [:i| |
| | 39 | ((k copyFrom: 2) combinations: i) collect: [:t| h, t ] ]) concat ]) X. |
| | 40 | ^(((sym includes: $:) |
| | 41 | ifTrue: [ s collect: [:ks| (ks collect: [:k| k, ':' ]) concat ] ] |
| | 42 | ifFalse: [ s concat ]) |
| | 43 | detect: [:x| ((Golf find: x from: list) at: 1 ifAbsent: []) = sym ] |
| | 44 | ifNone: [ ^nil ]) asSymbol! |
| | 45 | narrow: methods downTo: selector |
| | 46 | | a | |
| | 47 | a := selector occurrencesOf: $:. |
| | 48 | ^methods select: [:s| (s occurrencesOf: $:) = a ]! |
| | 49 | |
| | 50 | ?? sym |
| | 51 | ^(Smalltalk includesKey: sym) |
| | 52 | ifTrue: [ Golf abbreviate: sym from: Smalltalk keys ] |
| | 53 | ifFalse: [ nil ]! |
| 34 | | ?? abbr |
| 35 | | | a s | |
| 36 | | a := abbr occurrencesOf: $:. |
| 37 | | s := ((abbr inject: '' writeStream into: |
| 38 | | [:s :c| c <= $Z and: [ s << $* ]. s << c ]) << $*) contents. |
| 39 | | ^Golf.Dic at: {abbr asSymbol. self} ifAbsentPut: [ |
| 40 | | self allSelectors asSortedCollection |
| 41 | | detect: [:x| (x occurrencesOf: $:) = a and: [ s match: x ignoreCase: false ] ] |
| 42 | | ifNone: [ self error: Golf D, ' "', abbr, '" not found.' ] ]! |
| 43 | | |
| | 56 | ? abbr |
| | 57 | ^Golf.Dic at: self -> abbr asSymbol ifAbsentPut: [ |
| | 58 | (Golf find: abbr from: (Golf narrow: self allSelectors downTo: abbr)) at: 1 ifAbsent: |
| | 59 | [ self error: ('%1 %2 not found.' bindWith: Golf with: abbr) ] ]! |
| | 60 | ?? s |
| | 61 | ^(self canUnderstand: s) |
| | 62 | ifTrue: [ Golf abbreviate: s from: (Golf narrow: self allSelectors downTo: s) ] |
| | 63 | ifFalse: [ nil ]! |
| | 64 | >? abbr |
| | 65 | | s | |
| | 66 | ^(self whichClassIncludesSelector: (s := self ? abbr)) >> s! |
| | 67 | |
| 80 | | ^Golf.Global at: self put: o! |
| 81 | | |
| 82 | | !CharacterArray methodsFor: 'golf'! |
| 83 | | C |
| 84 | | | s | |
| 85 | | s := (((self copyFrom: 2) inject: '' writeStream << self first into: |
| 86 | | [:s :c| c <= $Z and: [ s << $* ]. s << c ]) << $*) contents. |
| 87 | | ^Smalltalk at: (##(Smalltalk keys asSortedCollection) |
| 88 | | detect: [:k| s match: k ignoreCase: false ] |
| 89 | | ifNone: [ ^nil ])! |
| | 106 | ^Golf.Dic at: self put: o! |
| | 107 | |
| | 108 | !String methodsFor: 'golf'! |
| | 109 | H |
| | 110 | ^('%1ello, %2orld%3' bindWithArguments: ((1 to: 3) collect: |
| | 111 | [:x| self at: x ifAbsent: [ 'Hw!' at: x ] ])) P! |
| 104 | | ^o isNumber |
| 105 | | ifTrue: [ self species streamContents: [:s| o timesRepeat: [ s nextPutAll: self ] ] ] |
| 106 | | ifFalse: [ |
| 107 | | | j | |
| 108 | | j := (o isKindOf: SequenceableCollection) ifTrue: [ o ] ifFalse: [ {o} ]. |
| 109 | | self fold: [:a :b| a, j, b ]]! |
| | 126 | ^(o isKindOf: Integer) |
| | 127 | ifTrue: [ | r s i | |
| | 128 | r := self species new: (s := self size) * o. i := 1. |
| | 129 | o timesRepeat: [ r replaceFrom: i to: (i := i + s) - 1 with: self ]. |
| | 130 | r ] |
| | 131 | ifFalse: [ | j s t r i | |
| | 132 | s := (j := o size = 0 ifTrue: [ self first species with: o ] ifFalse: [ o ]) size. |
| | 133 | t := self inject: 0 into: [:n :x| n + x size ]. |
| | 134 | r := self first species new: self size - (i := 1) * s + t. |
| | 135 | self do: [:x| r replaceFrom: i to: (i := i + x size) - 1 with: x ] |
| | 136 | separatedBy: [ r replaceFrom: i to: (i := i + s) - 1 with: j ]. |
| | 137 | r ]! |
| | 227 | |
| | 228 | !Collection methodsFor: 'arithmetic'! |
| | 229 | combinations: n |
| | 230 | n <= 1 and: [ ^self asArray collect: [:x| self species with: x ] ]. |
| | 231 | n >= self size and: [ ^{self copy} ]. |
| | 232 | ^(1 to: self size - n + 1) inject: {} into: |
| | 233 | [:c :i| c, (((self copyFrom: i + 1) combinations: n - 1) collect: |
| | 234 | [:comb| (self species with: (self at: i)), comb ])]! |
| | 235 | X |
| | 236 | "Generates a cartesian-product." |
| | 237 | ^self reverse inject: #(#()) into: [:arrs :colln| |
| | 238 | colln inject: #() into: [:rs :elem| rs, (arrs collect: [:arr | {elem}, arr ]) ] ]! |
| | 239 | sum |
| | 240 | ^self fold: [:x :y| x + y ]! |
| | 241 | concat |
| | 242 | ^self fold: [:x :y| x , y ]! |
| | 243 | max |
| | 244 | ^self fold: [:a :b| a < b ifTrue: [ b ] ifFalse: [ a ] ]! |
| | 245 | min |
| | 246 | ^self fold: [:a :b| a > b ifTrue: [ b ] ifFalse: [ a ] ]! |