Πέμπτη 17 Οκτωβρίου 2024

A zip function for lists

module zip_function {
const rightjustify=3, normaljustify=0
function zip(f) {
n=stack.size
if n=0 then =(,):exit
mL=infinity
// array to save arrays
dim a(1 to n)
for i=1 to n
read a(i)
ml=min.data(len(a(i))-1, ml)
next
if ml<0 then =(,):exit
// array for return value
dim b(0 to ml)
for k=0 to ml
for j=n to 1
push a(j)(k)
next
// [] is a function. return the current stack object,
// leave an empty stack object as the current stack object
// !s where s is a stack object in a parameter list leave all
// items of that stack to the parameter list (which is a stack object)
b(k)=f(![])
next
=b()
}

add=lambda ->{
sum=0
while not empty
if isnum then
sum+=number
else
drop
end if
end while
=sum
}
// double coma ,, at print list is a new line
? (1,2,3),, (10,20,30),, (2, 2, 200)
m=zip(add, (1,2,3), (10,20,30), (2, 2, 200))
//clipboard m#str$()
? m#str$()="13 24 233"
? m#str$()

concat=lambda ->{
string all
while not empty
if isnum then
all+=""+number
else
all+=letter$
end if
end while
=all
}
? $(rightjustify), (1,2,3),, (10,20,30),, (2, "X", 200),, ("aaa",2,3,4), $(normaljustify),,
m=zip(concat, (1,2,3), (10,20,30), (2, "X", 200), ("aaa",2,3,4))
? m#str$()="1102aaa 220X2 3302003"
? m#str$()
m=zip(add, (1,2,3), (10,20,30), (2, "X", 200), ("aaa",2,3,4))
? m#str$()="13 24 236"
? m#str$()
m=zip(add, (1,2,3))
? m#str$()
m=zip(add,(1,2,3), (,))
? Len(m)=0
m=zip(add)
? Len(m)=0
}
zip_function

Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

You can feel free to write any suggestion, or idea on the subject.