Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,16 @@
procedure main() #: demonstrate various ways to sort a list and string
demosort(bubblesort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty")
end
procedure bubblesort(X,op) #: return sorted list
local i,swapped
op := sortop(op,X) # select how and what we sort
swapped := 1
while \swapped := &null do # the sort
every i := 2 to *X do
if op(X[i],X[i-1]) then
X[i-1] :=: X[swapped := i]
return X
end

View file

@ -0,0 +1,59 @@
invocable all # for op
procedure sortop(op,X) #: select how to sort
op := case op of {
"string": "<<"
"numeric": "<"
&null: if type(!X) == "string" then "<<" else "<"
default: op
}
return proc(op, 2) | runerr(123, image(op))
end
procedure cmp(a,b) #: example custom comparison procedure
return a < b # Imagine a complex comparison test here!
end
procedure demosort(sortproc,L,s) # demonstrate sort on L and s
write("Sorting Demo using ",image(sortproc))
writes(" on list : ")
writex(L)
displaysort(sortproc,L) # default string sort
displaysort(sortproc,L,"numeric") # explicit numeric sort
displaysort(sortproc,L,"string") # explicit string sort
displaysort(sortproc,L,">>") # descending string sort
displaysort(sortproc,L,">") # descending numeric sort
displaysort(sortproc,L,cmp) # ascending custom comparison
displaysort(sortproc,L,"cmp") # ascending custom comparison
writes(" on string : ")
writex(s)
displaysort(sortproc,s) # sort characters in a string
write()
return
end
procedure displaysort(sortproc,X,op) #: helper to show sort behavior
local t,SX
writes(" with op = ",left(image(op)||":",15))
X := copy(X)
t := &time
SX := sortproc(X,op)
writex(SX," (",&time - t," ms)")
return
end
procedure writex(X,suf[]) #: helper for displaysort
if type(X) == "string" then
writes(image(X))
else {
writes("[")
every writes(" ",image(!X))
writes(" ]")
}
every writes(!suf)
write()
return
end