[Cuis-dev] curried blocks

ken.dickey at whidbey.com ken.dickey at whidbey.com
Sat Aug 3 11:01:58 PDT 2024


As a long-time Scheme programmer (circa 1980), I have never found a real 
use for currying in industrial applications.

A much more useful example is CPS (Continuation Passing Style) for 
flexibly chaining action sequences.

E.g. here is how CardMorphs are animated in 'Morphic-Games-Solitaire'.  
The blocks/closures do capture salient state.

Note use of `nextAction value`.

"===="

CardTableMorph>>
animateMoveFrom: aContainer to: aPile next: nextAction
	"Animate move of the top Card in aPile into aContainer."

	| topCard worldPos |
	topCard := aContainer topCard.
	topCard setProperty: #moveStart toValue: aContainer .
	worldPos := topCard morphPositionInWorld.

	"Transfer card to World"
	aContainer removeMorph: topCard.
	self world addMorph: topCard.
	topCard morphPosition: worldPos.
	topCard scale: self scale.

	"Animate the image move -- in World coordinates"
	self
	  slide: topCard
	  to: aPile morphPositionInWorld + aPile containerOffset
	  nSteps: self numberOfMoveSteps
	  delay: self moveDelay
	  next: [
		"Give the card to otherContainer."
		topCard world removeMorph: topCard.
			aPile acceptDroppingMorph: topCard event: nil.
			nextAction value
	  ].

"====="

CardTableMorph>>
slide: aMorph
to: endPoint
nSteps: numSteps
delay: milliSecondsDelay
next: nextAction

	"Slide from current to new position -- in owner's coordinates"
	"Nota Bene: Asynchronous.  When complete,  nextAction value"

	| startPoint delta stepCount |
	startPoint := aMorph morphPosition.
	delta := (endPoint - startPoint) / numSteps.
	stepCount := 0.
	aMorph
	  when: #morphicStep
	  evaluate: [ :ignoredArgument |
		stepCount := stepCount + 1.
		(stepCount < numSteps)
		ifTrue: [
			aMorph morphPosition:
				(startPoint + (stepCount  * delta)) rounded;
			redrawNeeded
		]
		ifFalse: [ "done"
			aMorph stopStepping.
			aMorph morphPosition: endPoint.
			aMorph removeActionsForEvent: #morphicStep.
			nextAction value
		]
	].
	aMorph startSteppingStepTime: milliSecondsDelay

"====="
-------------- next part --------------
A non-text attachment was scrubbed...
Name: CurriedBlock.st
Type: application/octet-stream
Size: 1499 bytes
Desc: not available
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20240803/799e212e/attachment.obj>


More information about the Cuis-dev mailing list