Skip to content

Commit

Permalink
Made use of #apply* in core/standard libraries.
Browse files Browse the repository at this point in the history
  • Loading branch information
briantrice committed Mar 21, 2010
1 parent fb0ada1 commit e08393e
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 87 deletions.
44 changes: 22 additions & 22 deletions src/core/collection.slate
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ c@(Collection traits) do: block separatedBy: sepBlock
c do: [| :each |
first ifFalse: [sepBlock do].
first: False.
block applyWith: each].
block apply*, each].
c
].

Expand All @@ -117,14 +117,14 @@ and each element of a collection in turn. The result of the block is
implicitly the next value."
[| result |
result: start.
c do: [| :element | result: (block applyWith: result with: element)].
c do: [| :element | result: (block apply*, result, element)].
result
].

c@(Collection traits) collect: block into: d
"Answer a new collection resulting from mapping the block onto each element."
[
[| :result | c do: [| :each | result nextPut: (block applyWith: each)]]
[| :result | c do: [| :each | result nextPut: (block apply*, each)]]
writingAs: d
].

Expand All @@ -137,14 +137,14 @@ c@(Collection traits) count: test
"Return the number of elements of the collection satisfying the test."
[| sum |
sum: 0.
c do: [| :each | (test applyWith: each) ifTrue: [sum: sum + 1]].
c do: [| :each | (test apply*, each) ifTrue: [sum: sum + 1]].
sum
].

c@(Collection traits) detect: succeed ifNone: fail
"Find an element satisfying a test. Conditionally execute a failure block."
[
c do: [| :element | (succeed applyWith: element) ifTrue: [^ element]].
c do: [| :element | (succeed apply*, element) ifTrue: [^ element]].
fail do
].

Expand All @@ -155,21 +155,21 @@ c@(Collection traits) detect: succeed
c@(Collection traits) anySatisfy: predicate
"Answer whether any elements cause the input block to be True."
[
c do: [| :element | (predicate applyWith: element) ifTrue: [^ True]].
c do: [| :element | (predicate apply*, element) ifTrue: [^ True]].
False
].

c@(Collection traits) allSatisfy: predicate
"Answer whether all elements cause the input block to be True."
[
c do: [| :element | (predicate applyWith: element) ifFalse: [^ False]].
c do: [| :element | (predicate apply*, element) ifFalse: [^ False]].
True
].

c@(Collection traits) noneSatisfy: predicate
"Answer whether none of c's elements cause the input block to be True."
[
c do: [| :element | (predicate applyWith: element) ifTrue: [^ False]].
c do: [| :element | (predicate apply*, element) ifTrue: [^ False]].
True
].

Expand All @@ -178,7 +178,7 @@ c@(Collection traits) select: test into: result
collection."
[
[| :result |
c do: [| :each | (test applyWith: each) ifTrue: [result nextPut: each]]]
c do: [| :each | (test apply*, each) ifTrue: [result nextPut: each]]]
writingAs: result
].

Expand All @@ -191,8 +191,8 @@ c@(Collection traits) select: test collect: block
"An optimization for staged collect:'s on select: results."
[
[| :result |
c do: [| :each | (test applyWith: each)
ifTrue: [result nextPut: (block applyWith: each)]]]
c do: [| :each | (test apply*, each)
ifTrue: [result nextPut: (block apply*, each)]]]
writingAs: c
].

Expand All @@ -201,8 +201,8 @@ c@(Collection traits) collect: block select: test
[
[| :result |
c do: [| :each tmp |
tmp: (block applyWith: each).
(test applyWith: tmp) ifTrue: [result nextPut: tmp]]]
tmp: (block apply*, each).
(test apply*, tmp) ifTrue: [result nextPut: tmp]]]
writingAs: c
].

Expand All @@ -219,7 +219,7 @@ e.g. {1. 2. 3 .4} reduce: [| :a :b | a + b] returns a sum of the elements."
[| reader result |
reader: c reader.
result: (init ifNil: [reader next]).
reader do: [| :each | result: (binBlock applyTo: {result. each})].
reader do: [| :each | result: (binBlock apply*, result, each)].
result]
].

Expand All @@ -233,7 +233,7 @@ e.g. #{1. 2. 3 .4} reduce: [| :a :b | a + b] returns a sum of the elements."
reader: c reader.
result: reader next.
reader do:
[| :each | result: (binBlock applyWith: result with: each)].
[| :each | result: (binBlock apply*, result, each)].
result
].

Expand All @@ -252,7 +252,7 @@ c@(Collection traits) trace: binBlock
elem: src next.
result nextPut: elem.
src do:
[| :each | result nextPut: (each: (binBlock applyTo: {elem. each}))]]
[| :each | result nextPut: (each: (binBlock apply*, elem, each))]]
writingInto: c newSameSize
].

Expand All @@ -278,21 +278,21 @@ c@(Collection traits) pairCollect: binBlock
src: c reader.
last: src next.
src do: [| :current |
result nextPut: (binBlock applyTo: {last. current}).
result nextPut: (binBlock apply*, last, current).
last: current]].
writingInto: (c new &capacity: c size - 1)
].

c1@(Collection traits) leftCollect: c2@(Collection traits) using: binBlock
"Apply binBlock to every element of c1 using c2 as a second argument to the block."
[
c1 collect: [| :item | binBlock applyTo: {item. c2}]
c1 collect: [| :item | binBlock apply*, item, c2]
].

c1@(Collection traits) rightCollect: c2@(Collection traits) using: binBlock
"Like leftCollect, but using c1 as a left argument to binBlock and elements of c2 each as a second argument."
[
c2 collect: [| :item | binBlock applyTo: {c1. item}]
c2 collect: [| :item | binBlock apply*, c1, item]
].

c@(Collection traits) most: binBlock
Expand All @@ -301,7 +301,7 @@ all other elements in the Collection. (see also least:)"
"TODO: add an optional specifying the value if none is found, useful for when
isEmpty implies Nil returns and subsequent necessary checks are not enough."
[
c reduce: [| :a :b | (binBlock applyTo: {a. b})
c reduce: [| :a :b | (binBlock apply*, a, b)
ifTrue: [a] ifFalse: [b]]
].

Expand All @@ -314,7 +314,7 @@ other elements in the Collection. (see also most:)"
"TODO: add an optional specifying the value if none is found, useful for when
isEmpty implies Nil returns and subsequent necessary checks are not enough."
[
c reduce: [| :a :b | (binBlock applyTo: {a. b})
c reduce: [| :a :b | (binBlock apply*, a, b)
ifTrue: [b] ifFalse: [a]]
].

Expand All @@ -325,7 +325,7 @@ c@(Collection traits) reject: test
"Answer the complement of select:, a subset of the collection containing those
elements causing the input block to return False."
[
c select: [| :each | (test applyWith: each) not]
c select: [| :each | (test apply*, each) not]
].

c@(Collection traits) copyWithout: obj
Expand Down
6 changes: 3 additions & 3 deletions src/core/derivable.slate
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ d@(Derivable traits) traitsReverseDo: block
tally: d traitsTally.
[index = tally]
whileFalse:
[block applyTo: {d traitsWindow _delegates at: index}.
[block apply*, (d traitsWindow _delegates at: index).
index: index + 1]
].

Expand All @@ -21,7 +21,7 @@ d@(Derivable traits) traitsDo: block
[index = 0]
whileFalse:
[index: index - 1.
block applyTo: {d traitsWindow _delegates at: index}]
block apply*, (d traitsWindow _delegates at: index)]
].

d@(Derivable traits) orderTraits &mixins: mixins
Expand Down Expand Up @@ -192,7 +192,7 @@ x@(Root traits) define: protoName &parents: parents &slots: slotSpecs
builder ifNil:
[ns
ifNil: [(x isSameAs: Namespace) /\
[(#surroundings findOn: {x}) ifNil: [True] ifNotNilDo: [| :m | (m applyTo: {proto}) == x]]
[(#surroundings findOn: {x}) ifNil: [True] ifNotNilDo: [| :m | (m apply*, proto) == x]]
ifTrue: [[| :_ | x] asMethod: #surroundings on: {proto traits}]]
ifNotNil: [[| :_ | ns] asMethod: #surroundings on: {proto traits}].
obj ifNotNil: [[| :_ | obj] asMethod: #owner on: {proto traits}]].
Expand Down
2 changes: 1 addition & 1 deletion src/core/file.slate
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ If &mode is defined then this mode will be used for both files."
[| :file1 |
l2 sessionDo:
[| :file2 |
block applyTo: {file1. file2}] &mode: mode2] &mode: mode1
block apply*, file1, file2] &mode: mode2] &mode: mode1
].

File Locator traits define: #hostSeparator -> $:.
Expand Down
8 changes: 4 additions & 4 deletions src/core/method.slate
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,12 @@ This composition is associative, i.e. (a ** b) ** c = a ** (b ** c).
When the second method, n, does not take a *rest option or the first takes
more than one input, then the output is chunked into groups for its
consumption. E.g.:
#; `er ** #; `er applyTo: {'a'. 'b'. 'c'. 'd'} => 'abcd'
#; `er ** #name `er applyTo: {#a. #/}. => 'a/'"
#; `er ** #; `er apply*, 'a', 'b', 'c', 'd' => 'abcd'
#; `er ** #name `er apply*, #a, #/ => 'a/'"
[
n acceptsAdditionalArguments \/ [m arity = 1]
ifTrue:
[[| *args | m applyTo: {n applyTo: args}]]
[[| *args | m apply*, (n applyTo: args)]]
ifFalse:
[[| *args |
m applyTo:
Expand Down Expand Up @@ -487,7 +487,7 @@ Example: collect: takes a collection of objects and a method,
TODO: Improve the documentation, and add examples."
[
m arity = 2
ifTrue: [[| :b :a | m applyTo: {a. [| :g | g applyTo: {b}]}]]
ifTrue: [[| :b :a | m apply*, a, [| :g | g apply*, b]]]
ifFalse:
[TODO: 'Implement multi-argument swing.'
"[| *args | m applyTo: {args last} ; args allButLast reversed]"]
Expand Down
2 changes: 1 addition & 1 deletion src/core/numeric.slate
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ order)."
remaining: n.
[[(remaining \\ div) isZero]
whileTrue:
[block applyTo: {div}.
[block apply*, div.
remaining: remaining // div].
remaining = 1] whileFalse:
[div: next.
Expand Down
6 changes: 3 additions & 3 deletions src/core/root.slate
Original file line number Diff line number Diff line change
Expand Up @@ -498,9 +498,9 @@ body and assign the returned value to the slot."
for: slotName
on: {newObj. NoRole}.
[| :obj |
obj addAccessorFor: slotName.
obj addMutatorFor: slotName.
obj atSlotNamed: slotName put: (init applyTo: {obj})]
obj addAccessorFor: slotName.
obj addMutatorFor: slotName.
obj atSlotNamed: slotName put: (init apply*, obj)]
asAccessor: (x accessorNameFor: slotName)
for: slotName
on: {newObj}.
Expand Down
Loading

0 comments on commit e08393e

Please sign in to comment.