Partial function application: Difference between revisions

m (→‎{{header|Phix}}: syntax coloured, extended partial arguments note into a complete runnable example)
Line 2,437:
fsf2([2..8,2]); //-->L(4,16,36,64)</lang>
 
 
=={{header|Z80 Assembly}}==
First, the implementation of the functions.
<lang z80>func_fs:
;input
;hl = function to call
;ix = data range to operate over
;de = output area
;b = length of data range
 
push bc
ld (smc_fs+1),hl
ld a,(ix+0)
smc_fs:
call 0 ;overwritten with the function address passed in HL
 
ld (de),a
inc ix
inc de
pop bc
djnz func_fs
ret
 
f: ;dummy function - returns input as-is
ret
 
f1:
;returns A times 2
sla a
ret
 
f2:
;returns A squared
ld b,a
jp square_small
;ret
 
data1:
db 0,1,2,3
 
data2
db 2,4,6,8
 
output:
ds 4
 
;these libraries allow us to write the output to the screen
read "\SrcCPC\winape_monitor.asm"
read "\SrcCPC\winape_stringop.asm"
read "\SrcCPC\winape_showhex.asm"
 
square_small:
;returns a*a into a
LD C,B
mul8_small:
;multiplies two 8-bit regs, product is also 8 bit.
;no overflow protection!
;computes A = c * b
ld a,c
or a
ret z
djnz skip_return_C
;ADVANCED TRICKERY:
; we need to decrement B anyway.
; also if B = 1, then A = C.
; C is already in A, which we need for the multiplication regardless.
; This does the job for us in one instruction!
; Most DJNZs are backward but this one is FORWARD!
ret
skip_return_C:
add C
djnz skip_return_C
ret</lang>
 
{{out}}
 
And this is the unit test showing the results. Output is in hexadecimal but is otherwise correct.
<lang z80>;;;;;;;;;;;;;;;;;;; HEADER ;;;;;;;;;;;;;;;;;;;
read "\SrcCPC\winape_macros.asm"
read "\SrcCPC\MemoryMap.asm"
read "\SrcALL\winapeBuildCompat.asm"
;;;;;;;;;;;;;;;;;;; PROGRAM ;;;;;;;;;;;;;;;;;;;
 
org &1000
 
ld hl,f1
ld ix,data1
ld de,output
ld b,4
call func_fs ;execute f1f(s) on data set 1
 
 
call monitor_memdump ;display the output
db 4
dw output
 
call newline
 
ld hl,f1
ld ix,data2
ld de,output
ld b,4
call func_fs ;;execute f1f(s) on data set 2
 
 
call monitor_memdump ;display the output
db 4
dw output
 
call newline
 
ld hl,f2
ld ix,data1
ld de,output
ld b,4
call func_fs
 
 
call monitor_memdump
db 4
dw output
 
call newline
 
ld hl,f2
ld ix,data2
ld de,output
ld b,4
call func_fs
 
 
call monitor_memdump
db 4
dw output
 
ret ;return to basic</lang>
 
<pre>;107D = address of output data buffer
107D:
00 02 04 06 ....
 
107D:
04 08 0C 10 ....
 
107D:
00 01 04 09 ....
 
107D:
04 10 24 40 ..$@</pre>
 
{{omit from|Euphoria}}
1,489

edits