Changeset 5088 for lang/smalltalk

Show
Ignore:
Timestamp:
01/20/08 19:37:32 (10 months ago)
Author:
murky-satyr
Message:

lang/smalltalk/misc/gost.st: Corresponds roughly to [goruby rev15046] (the flagitious patch).

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/smalltalk/misc/gost.st

    r4539 r5088  
    1010Object subclass: #Golf 
    1111  instanceVariableNames: '' 
    12   classVariableNames: 'Dic Global' 
     12  classVariableNames: 'Dic' 
    1313  poolDictionaries: '' 
    1414  category: 'Golf'! 
    15 Golf.Dic    := Dictionary new. 
    16 Golf.Global := Dictionary new 
     15Golf.Dic := Dictionary new 
     16  add: $S -> Smalltalk; 
     17  add: $X -> thisContext; 
    1718  add: $< -> stdin; 
    1819  add: $> -> stdout; 
    1920  add: $* -> Smalltalk arguments; 
    20   add: $S -> Smalltalk; 
    21   add: $X -> thisContext; 
    22   add: $/ -> Character nl; 
     21  add: $/ -> $<10>; 
    2322  add: $\ -> nil; 
    2423  yourself! 
    2524!Golf class methodsFor: 'golf'! 
    2625printOn: s 
    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! 
     27find: 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! 
     34abbreviate: 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! 
     45narrow: 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 ]! 
    3254 
    3355!ClassDescription methodsFor: 'golf'! 
    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   
    4468!Object methodsFor: 'golf'! 
    4569doesNotUnderstand: m 
    46   ^m selector: self class ?? m selector; sendTo: self! 
     70  ^m selector: self class ? m selector; sendTo: self! 
    4771H 
    48   ^'Hello, world!' displayNl! 
    49 H: args 
    50   ^('%1ello, %2orld%3' bindWithArguments: args) displayNl! 
     72  ^'Hello, world!' P! 
    5173Q 
    5274  | s | 
     
    5577  ^(s method methodSourceString, '!') display! 
    5678P 
    57   ^self displayNl! 
    58 P: f 
    59   self displayOn: f. f nl! 
    60 |> f 
    61   self displayOn: f! 
     79  self P: (Golf.Dic at: $>)! 
     80P: s 
     81  self displayOn: s. (Golf.Dic at: $/) displayOn: s! 
     82|> s 
     83  self P: s! 
    6284 
    6385!Symbol methodsFor: 'golf'! 
    6486doesNotUnderstand: m 
    6587  ^m sendTo: (self C ifNil:[ ^super doesNotUnderstand: m ])! 
     88C 
     89  ^Smalltalk at: ((Golf find: self from: Smalltalk keys) 
     90    at: 1 ifAbsent: [ ^nil ])! 
    6691 
    6792!Character methodsFor: 'golf'! 
    6893doesNotUnderstand: m 
    69   ^m sendTo: (Golf.Global at: self ifAbsent: [ ^super doesNotUnderstand: m ])! 
     94  ^m sendTo: (Golf.Dic at: self ifAbsent: [ ^super doesNotUnderstand: m ])! 
    7095, o 
    7196  ^self asString, (o isCharacter ifTrue: [ {o} ] ifFalse: [ o ])! 
    7297+ n 
    7398  ^(codePoint + n) asCharacter! 
     99- n 
     100  ^(codePoint - n) asCharacter! 
    74101* n 
    75102  ^String new: n withAll: self! 
    76103~ c 
    77   ^((codePoint ~ c value) inject: '' writeStream into: 
    78     [:s :v| s << v asCharacter ]) contents! 
     104  ^'', ((codePoint ~ c value) collect: [:v| v asCharacter ])! 
    79105<- o 
    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'! 
     109H 
     110  ^('%1ello, %2orld%3' bindWithArguments: ((1 to: 3) collect: 
     111    [:x| self at: x ifAbsent: [ 'Hw!' at: x ] ])) P! 
    90112E 
    91113  ^Behavior evaluate: self! 
     
    102124  ] ifFalse: [ super / p ]! 
    103125* o 
    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 ]! 
    110138>< d 
    111139  "*Experimental*  e.g. 'Hello' >< {$H->$G. $l->$t} => 'Getto'" 
     
    113141displayOn: s 
    114142  | j | 
    115   (j := Golf.Global at: $\) isNil 
     143  (j := Golf.Dic at: $\) isNil 
    116144    ifTrue:  [ self do: [:x| x displayOn: s ] ] 
    117145    ifFalse: [ self do: [:x| x displayOn: s ] separatedBy: [ j displayOn: s ] ]! 
    118  
    119 !! 
    120  
     146   
    121147!False methodsFor: 'golf'! 
    122148displayOn: s! 
     
    170196    ifTrue:  [ o collect: [:i| self at: i ] ] 
    171197    ifFalse: [ self at: o ]! 
     198+ o 
     199  (o isKindOf: BlockClosure) and: [ ^self fold: o ]. 
     200  o isSymbol and: [ ^self fold: [:a :b| a perform: o with: b ] ]! 
    172201/ n 
    173202  | r s | 
     
    182211<< o 
    183212  self add: o! 
    184 sum 
    185   ^self fold: [:a :b| a + b ]! 
    186 max 
    187   ^self fold: [:a :b| a < b ifTrue: [ b ] ifFalse: [ a ] ]! 
    188 min 
    189   ^self fold: [:a :b| a > b ifTrue: [ b ] ifFalse: [ a ] ]! 
    190213 
    191214!BlockClosure methodsFor: 'golf'! 
     
    194217< a 
    195218  ^self valueWithArguments: a! 
     219displayOn: s 
     220  ^(self value) displayOn: s! 
    196221 
    197222!FileStream methodsFor: 'golf'! 
    198223\ b 
    199   ^(self contents subStrings: Character nl) \ b! 
    200  
     224  ^(self contents subStrings: (Golf.Dic at: $/)) \ b! 
    201225 
    202226"[ Not-so-golf Additions ]" 
     227 
     228!Collection methodsFor: 'arithmetic'! 
     229combinations: 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 ])]! 
     235X 
     236  "Generates a cartesian-product." 
     237  ^self reverse inject: #(#()) into: [:arrs :colln| 
     238    colln inject: #() into: [:rs :elem| rs, (arrs collect: [:arr | {elem}, arr ]) ] ]! 
     239sum 
     240  ^self fold: [:x :y| x + y ]! 
     241concat 
     242  ^self fold: [:x :y| x , y ]! 
     243max 
     244  ^self fold: [:a :b| a < b ifTrue: [ b ] ifFalse: [ a ] ]! 
     245min 
     246  ^self fold: [:a :b| a > b ifTrue: [ b ] ifFalse: [ a ] ]! 
    203247 
    204248!Object methodsFor: 'rubylike-case'! 
     
    223267=? o 
    224268  ^true! 
    225  
    226269!BlockClosure methodsFor: 'rubylike-case'! 
    227270=? o