|
Class: CurryingBlock
Object
|
+--ExecutableFunction
|
+--CompiledCode
|
+--Block
|
+--CurryingBlock
- Package:
- stx:libbasic2
- Category:
- Kernel-Methods
- Version:
- rev:
1.4
date: 2024/02/09 11:16:47
- user: stefan
- file: CurryingBlock.st directory: libbasic2
- module: stx stc-classLibrary: libbasic2
Curried functions are functions which, if invoked with less-than-expected
arguments, return a function which expects the remaining arguments.
For example, given a function add(a,b),
which expects two arguments, and returns their sum,
a curried add(N) would return a function, which expects one argument (b)
and computes add(N+b).
I.e.
(add(a)) (b) -> add(a,b)
Here, a CurryingBlock, if evaluated with less-than-required arguments,
returns a block which expects the remaining args, and provides the initially
provided arguments.
I.e.
( [:a :b | a + b] beCurryingBlock ) value:10
returns a block, which expects 1 argument (b) and
adds it to 10.
This is an experimental (academic ?) goody for demonstration purposes.
copyrightCOPYRIGHT (c) 2003 by eXept Software AG
All Rights Reserved
This software is furnished under a license and may be used
only in accordance with the terms of that license and with the
inclusion of the above copyright notice. This software may not
be provided or otherwise made available to, or used by, any
other person. No title to or ownership of the software is
hereby transferred.
error handling
-
wrongNumberOfArguments: numArgsGiven
-
more args given than requested
printing
-
printBlockBracketsOn: aStream
-
|b blockAdding1 blockAdding10|
b := [:a :b | a + b] beCurryingBlock.
blockAdding1 := b value:1.
blockAdding10 := b value:10.
blockAdding1 value:2.
blockAdding10 value:2.
b value:10 value:30.
(b value:10) value:30.
|
|b b1 b2 b3 result|
b := [:a :b :c | a + b + c] beCurryingBlock.
Transcript showCR: e'b is expecting {b numArgs} arguments'.
result := b value:1 value:2 value:3.
Transcript showCR: e'calling b with 3 args (1,2,3), gets {result}.'; cr.
Transcript showCR: e'but if called with only one arg (10), we get b1...'.
b1 := b value:10.
Transcript showCR: e'which expects {b1 numArgs} arguments'.
result := b1 value:2 value:3.
Transcript showCR: e'calling this b1 with 2 args (2,3), gets {result}.'; cr.
Transcript showCR: e'and if called with (10,10), we get b2...'.
b2 := b value:10 value:20.
Transcript showCR: e'which expects {b2 numArgs} arguments'.
result := b2 value:3.
Transcript showCR: e'calling b2 with 1 arg (3), gets {result}.'; cr.
Transcript showCR: e'we can also call b1 with another arg, to get b3'.
b3 := b1 value:20.
Transcript showCR: e'which expects {b3 numArgs} arguments'.
result := b3 value:3.
Transcript showCR: e'calling b3 with 1 arg, gets {result}.'.
|
|