Bert Freudenberg uploaded a new version of Help-Squeak-TerseGuide to project The Inbox:
http://source.squeak.org/inbox/Help-Squeak-TerseGuide-dhn.4.mcz ==================== Summary ==================== Name: Help-Squeak-TerseGuide-dhn.4 Author: dhn Time: 4 October 2014, 9:25:48.387 pm UUID: 5622b076-0185-a54e-aae1-7a8f158d1e1f Ancestors: Help-Squeak-TerseGuide-dhn.3 Correct comment for detect: (several places). Add findFirst: where applicable. =============== Diff against Help-Squeak-TerseGuide-dtl.2 =============== Item was changed: ----- Method: TerseGuideHelp class>>array (in category 'pages') ----- array ^HelpTopic title: 'Array' contents: '"************************************************************************ * Array: Fixed length collection * * ByteArray: Array limited to byte elements (0-255) * * WordArray: Array limited to word elements (0-2^32) * ************************************************************************" | b x y sum max | x := #(4 3 2 1). "constant array" x := Array with: 5 with: 4 with: 3 with: 2. "create array with up to 4 elements" x := Array new: 4. "allocate an array with specified size" x "set array elements" at: 1 put: 5; at: 2 put: 4; at: 3 put: 3; at: 4 put: 2. b := x isEmpty. "test if array is empty" y := x size. "array size" y := x at: 4. "get array element at index" b := x includes: 3. "test if element is in array" y := x copyFrom: 2 to: 4. "subarray" y := x indexOf: 3 ifAbsent: [0]. "first position of element within array" y := x occurrencesOf: 3. "number of times object in collection" x do: [:a | Transcript show: a printString; cr]. "iterate over the array" b := x allSatisfy: [:a | (a >= 1) & (a <= 4)]. "test if all elements meet condition" y := x select: [:a | a > 2]. "return collection of elements that pass test" y := x reject: [:a | a < 2]. "return collection of elements that fail test" y := x collect: [:a | a + a]. "transform each element for new collection" + y := x detect: [:a | a > 3] ifNone: []. "return first element that passes test" + y := x findFirst: [:a | a < 3]. "find position of first element that passes test" - y := x detect: [:a | a > 3] ifNone: []. "find position of first element that passes test" sum := 0. x do: [:a | sum := sum + a]. sum. "sum array elements" sum := 0. 1 to: (x size) do: [:a | sum := sum + (x at: a)]. "sum array elements" sum := x inject: 0 into: [:a :c | a + c]. "sum array elements" max := x inject: 0 into: [:a :c | (a > c) "find max element in array" ifTrue: [a] ifFalse: [c]]. y := x shuffled. "randomly shuffle collection" y := x asArray. "convert to array" y := x asByteArray. "convert to byte array" y := x asWordArray. "convert to word array" y := x asOrderedCollection. "convert to ordered collection" y := x asSortedCollection. "convert to sorted collection" y := x asBag. "convert to bag collection" y := x asSet. "convert to set collection" '! Item was changed: ----- Method: TerseGuideHelp class>>bag (in category 'pages') ----- bag ^HelpTopic title: 'Bag' contents: '"************************************************************************ * Bag: like OrderedCollection except elements are in no * * particular order * ************************************************************************" | b x y sum max | x := Bag with: 4 with: 3 with: 2 with: 1. "create collection with up to 4 elements" x := Bag new. "allocate collection" x add: 4; add: 3; add: 1; add: 2; yourself. "add element to collection" x add: 3 withOccurrences: 2. "add multiple copies to collection" y := x addAll: #(7 8 9). "add multiple elements to collection" y := x removeAll: #(7 8 9). "remove multiple elements from collection" y := x remove: 4 ifAbsent: []. "remove element from collection" b := x isEmpty. "test if empty" y := x size. "number of elements" b := x includes: 3. "test if element is in collection" y := x occurrencesOf: 3. "number of times object in collection" x do: [:a | Transcript show: a printString; cr]. "iterate over the collection" b := x allSatisfy: [:a | (a >= 1) & (a <= 4)]. "test if all elements meet condition" y := x select: [:a | a > 2]. "return collection of elements that pass test" y := x reject: [:a | a < 2]. "return collection of elements that fail test" y := x collect: [:a | a + a]. "transform each element for new collection" + y := x detect: [:a | a > 3] ifNone: []. "return first element that passes test" - y := x detect: [:a | a > 3] ifNone: []. "find position of first element that passes test" sum := 0. x do: [:a | sum := sum + a]. sum. "sum elements" sum := x inject: 0 into: [:a :c | a + c]. "sum elements" max := x inject: 0 into: [:a :c | (a > c) "find max element in collection" ifTrue: [a] ifFalse: [c]]. y := x asOrderedCollection. "convert to ordered collection" y := x asSortedCollection. "convert to sorted collection" y := x asBag. "convert to bag collection" y := x asSet. "convert to set collection" '! Item was changed: ----- Method: TerseGuideHelp class>>dictionary (in category 'pages') ----- dictionary ^HelpTopic title: 'Dictionary' contents: '"************************************************************************ * Dictionary: * * IdentityDictionary: uses identity test (== rather than =) * ************************************************************************" | b x y sum max | x := Dictionary new. "allocate collection" x add: #a->4; add: #b->3; add: #c->1; add: #d->2; yourself. "add element to collection" x at: #e put: 3. "set element at index" b := x isEmpty. "test if empty" y := x size. "number of elements" y := x at: #a ifAbsent: []. "retrieve element at index" y := x keyAtValue: 3 ifAbsent: []. "retrieve key for given value with error block" y := x removeKey: #e ifAbsent: []. "remove element from collection" b := x includes: 3. "test if element is in values collection" b := x includesKey: #a. "test if element is in keys collection" y := x occurrencesOf: 3. "number of times object in collection" y := x keys. "set of keys" y := x values. "bag of values" x do: [:a | Transcript show: a printString; cr]. "iterate over the values collection" x keysDo: [:a | Transcript show: a printString; cr]. "iterate over the keys collection" x associationsDo: [:a | Transcript show: a printString; cr]. "iterate over the associations" x keysAndValuesDo: [:aKey :aValue | Transcript "iterate over keys and values" show: aKey printString; space; show: aValue printString; cr]. b := x allSatisfy: [:a | (a >= 1) & (a <= 4)]. "test if all elements meet condition" y := x select: [:a | a > 2]. "return collection of elements that pass test" y := x reject: [:a | a < 2]. "return collection of elements that fail test" y := x collect: [:a | a + a]. "transform each element for new collection" + y := x detect: [:a | a > 3] ifNone: []. "return first element that passes test" - y := x detect: [:a | a > 3] ifNone: []. "find position of first element that passes test" sum := 0. x do: [:a | sum := sum + a]. sum. "sum elements" sum := x inject: 0 into: [:a :c | a + c]. "sum elements" max := x inject: 0 into: [:a :c | (a > c) "find max element in collection" ifTrue: [a] ifFalse: [c]]. y := x asArray. "convert to array" y := x asOrderedCollection. "convert to ordered collection" y := x asSortedCollection. "convert to sorted collection" y := x asBag. "convert to bag collection" y := x asSet. "convert to set collection" Smalltalk at: #CMRGlobal put: ''CMR entry''. "put global in Smalltalk Dictionary" x := Smalltalk at: #CMRGlobal. "read global from Smalltalk Dictionary" Transcript show: (CMRGlobal printString). "entries are directly accessible by name" Smalltalk keys do: [ :k | "print out all classes" ((Smalltalk at: k) isKindOf: Class) ifFalse: [Transcript show: k printString; cr]]. Smalltalk at: #CMRDictionary put: (Dictionary new). "set up user defined dictionary" CMRDictionary at: #MyVar1 put: ''hello1''. "put entry in dictionary" CMRDictionary add: #MyVar2->''hello2''. "add entry to dictionary use key->value combo" CMRDictionary size. "dictionary size" CMRDictionary keys do: [ :k | "print out keys in dictionary" Transcript show: k printString; cr]. CMRDictionary values do: [ :k | "print out values in dictionary" Transcript show: k printString; cr]. CMRDictionary keysAndValuesDo: [:aKey :aValue | "print out keys and values" Transcript show: aKey printString; space; show: aValue printString; cr]. CMRDictionary associationsDo: [:aKeyValue | "another iterator for printing key values" Transcript show: aKeyValue printString; cr]. Smalltalk removeKey: #CMRGlobal ifAbsent: []. "remove entry from Smalltalk dictionary" Smalltalk removeKey: #CMRDictionary ifAbsent: []. "remove user dictionary from Smalltalk dictionary" '! Item was changed: ----- Method: TerseGuideHelp class>>interval (in category 'pages') ----- interval ^HelpTopic title: 'Interval' contents: '"************************************************************************ * Interval: * ************************************************************************" | b x y sum max | x := Interval from: 5 to: 10. "create interval object" x := 5 to: 10. x := Interval from: 5 to: 10 by: 2. "create interval object with specified increment" x := 5 to: 10 by: 2. b := x isEmpty. "test if empty" y := x size. "number of elements" x includes: 9. "test if element is in collection" x do: [:k | Transcript show: k printString; cr]. "iterate over interval" b := x allSatisfy: [:a | (a >= 1) & (a <= 4)]. "test if all elements meet condition" y := x select: [:a | a > 7]. "return collection of elements that pass test" y := x reject: [:a | a < 2]. "return collection of elements that fail test" y := x collect: [:a | a + a]. "transform each element for new collection" + y := x detect: [:a | a > 3] ifNone: []. "return first element that passes test" + y := x findFirst: [:a | a > 6]. "find position of first element that passes test" - y := x detect: [:a | a > 3] ifNone: []. "find position of first element that passes test" sum := 0. x do: [:a | sum := sum + a]. sum. "sum elements" sum := 0. 1 to: (x size) do: [:a | sum := sum + (x at: a)]. "sum elements" sum := x inject: 0 into: [:a :c | a + c]. "sum elements" max := x inject: 0 into: [:a :c | (a > c) "find max element in collection" ifTrue: [a] ifFalse: [c]]. y := x asArray. "convert to array" y := x asOrderedCollection. "convert to ordered collection" y := x asSortedCollection. "convert to sorted collection" y := x asBag. "convert to bag collection" y := x asSet. "convert to set collection" '! Item was changed: ----- Method: TerseGuideHelp class>>orderedCollection (in category 'pages') ----- orderedCollection ^HelpTopic title: 'Ordered Collection' contents: '"************************************************************************ * OrderedCollection: acts like an expandable array * ************************************************************************" | b x y sum max | x := OrderedCollection with: 4 with: 3 with: 2 with: 1. "create collection with up to 4 elements" x := OrderedCollection new. "allocate collection" x add: 3; add: 2; add: 1; add: 4; yourself. "add element to collection" y := x addFirst: 5. "add element at beginning of collection" y := x removeFirst. "remove first element in collection" y := x addLast: 6. "add element at end of collection" y := x removeLast. "remove last element in collection" y := x addAll: #(7 8 9). "add multiple elements to collection" y := x removeAll: #(7 8 9). "remove multiple elements from collection" x at: 2 put: 3. "set element at index" y := x remove: 5 ifAbsent: []. "remove element from collection" b := x isEmpty. "test if empty" y := x size. "number of elements" y := x at: 2. "retrieve element at index" y := x first. "retrieve first element in collection" y := x last. "retrieve last element in collection" b := x includes: 5. "test if element is in collection" y := x copyFrom: 2 to: 3. "subcollection" y := x indexOf: 3 ifAbsent: [0]. "first position of element within collection" y := x occurrencesOf: 3. "number of times object in collection" x do: [:a | Transcript show: a printString; cr]. "iterate over the collection" b := x allSatisfy: [:a | (a >= 1) & (a <= 4)]. "test if all elements meet condition" y := x select: [:a | a > 2]. "return collection of elements that pass test" y := x reject: [:a | a < 2]. "return collection of elements that fail test" y := x collect: [:a | a + a]. "transform each element for new collection" + y := x detect: [:a | a > 3] ifNone: []. "return first element that passes test" + y := x findFirst: [:a | a < 2]. "find position of first element that passes test" - y := x detect: [:a | a > 3] ifNone: []. "find position of first element that passes test" sum := 0. x do: [:a | sum := sum + a]. sum. "sum elements" sum := 0. 1 to: (x size) do: [:a | sum := sum + (x at: a)]. "sum elements" sum := x inject: 0 into: [:a :c | a + c]. "sum elements" max := x inject: 0 into: [:a :c | (a > c) "find max element in collection" ifTrue: [a] ifFalse: [c]]. y := x shuffled. "randomly shuffle collection" y := x asArray. "convert to array" y := x asOrderedCollection. "convert to ordered collection" y := x asSortedCollection. "convert to sorted collection" y := x asBag. "convert to bag collection" y := x asSet. "convert to set collection" '! Item was changed: ----- Method: TerseGuideHelp class>>set (in category 'pages') ----- set ^HelpTopic title: 'Set' contents: '"************************************************************************ * Set: like Bag except duplicates not allowed * * IdentitySet: uses identity test (== rather than =) * ************************************************************************" | b x y sum max | x := Set with: 4 with: 3 with: 2 with: 1. "create collection with up to 4 elements" x := Set new. "allocate collection" x add: 4; add: 3; add: 1; add: 2; yourself. "add element to collection" y := x addAll: #(7 8 9). "add multiple elements to collection" y := x removeAll: #(7 8 9). "remove multiple elements from collection" y := x remove: 4 ifAbsent: []. "remove element from collection" b := x isEmpty. "test if empty" y := x size. "number of elements" x includes: 4. "test if element is in collection" x do: [:a | Transcript show: a printString; cr]. "iterate over the collection" b := x allSatisfy: [:a | (a >= 1) & (a <= 4)]. "test if all elements meet condition" y := x select: [:a | a > 2]. "return collection of elements that pass test" y := x reject: [:a | a < 2]. "return collection of elements that fail test" y := x collect: [:a | a + a]. "transform each element for new collection" + y := x detect: [:a | a > 3] ifNone: []. "return first element that passes test" - y := x detect: [:a | a > 3] ifNone: []. "find position of first element that passes test" sum := 0. x do: [:a | sum := sum + a]. sum. "sum elements" sum := x inject: 0 into: [:a :c | a + c]. "sum elements" max := x inject: 0 into: [:a :c | (a > c) "find max element in collection" ifTrue: [a] ifFalse: [c]]. y := x asArray. "convert to array" y := x asOrderedCollection. "convert to ordered collection" y := x asSortedCollection. "convert to sorted collection" y := x asBag. "convert to bag collection" y := x asSet. "convert to set collection" '! Item was changed: ----- Method: TerseGuideHelp class>>sortedCollection (in category 'pages') ----- sortedCollection ^HelpTopic title: 'Sorted Collection' contents: '"************************************************************************ * SortedCollection: like OrderedCollection except order of elements * * determined by sorting criteria * ************************************************************************" | b x y sum max | x := SortedCollection with: 4 with: 3 with: 2 with: 1. "create collection with up to 4 elements" x := SortedCollection new. "allocate collection" x := SortedCollection sortBlock: [:a :c | a > c]. "set sort criteria" x add: 3; add: 2; add: 1; add: 4; yourself. "add element to collection" "y := x addFirst: 5." "add element at beginning of collection" y := x removeFirst. "remove first element in collection" y := x addLast: 6. "add element at end of collection" y := x removeLast. "remove last element in collection" y := x addAll: #(7 8 9). "add multiple elements to collection" y := x removeAll: #(7 8 9). "remove multiple elements from collection" y := x remove: 5 ifAbsent: []. "remove element from collection" b := x isEmpty. "test if empty" y := x size. "number of elements" y := x at: 2. "retrieve element at index" y := x first. "retrieve first element in collection" y := x last. "retrieve last element in collection" b := x includes: 4. "test if element is in collection" y := x copyFrom: 2 to: 3. "subcollection" y := x indexOf: 3 ifAbsent: [0]. "first position of element within collection" y := x occurrencesOf: 3. "number of times object in collection" x do: [:a | Transcript show: a printString; cr]. "iterate over the collection" b := x allSatisfy: [:a | (a >= 1) & (a <= 4)]. "test if all elements meet condition" y := x select: [:a | a > 2]. "return collection of elements that pass test" y := x reject: [:a | a < 2]. "return collection of elements that fail test" y := x collect: [:a | a + a]. "transform each element for new collection" + y := x detect: [:a | a > 3] ifNone: []. "return first element that passes test" + y := x findFirst: [:a | a < 3]. "find position of first element that passes test" - y := x detect: [:a | a > 3] ifNone: []. "find position of first element that passes test" sum := 0. x do: [:a | sum := sum + a]. sum. "sum elements" sum := 0. 1 to: (x size) do: [:a | sum := sum + (x at: a)]. "sum elements" sum := x inject: 0 into: [:a :c | a + c]. "sum elements" max := x inject: 0 into: [:a :c | (a > c) "find max element in collection" ifTrue: [a] ifFalse: [c]]. y := x asArray. "convert to array" y := x asOrderedCollection. "convert to ordered collection" y := x asSortedCollection. "convert to sorted collection" y := x asBag. "convert to bag collection" y := x asSet. "convert to set collection" '! |
Free forum by Nabble | Edit this page |