September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,35 @@
MODULE SORTSEARCH !Genuflect towards Prof. D. Knuth.
INTERFACE FIND !Binary chop search, not indexed.
MODULE PROCEDURE
1 FINDI4, !I: of integers.
2 FINDF4,FINDF8, !F: of numbers.
3 FINDTTI2,FINDTTI4 !T: of texts.
END INTERFACE FIND
CONTAINS
INTEGER FUNCTION FINDI4(THIS,NUMB,N) !Binary chopper. Find i such that THIS = NUMB(i)
USE ASSISTANCE !Only for the trace stuff.
INTENT(IN) THIS,NUMB,N !Imply read-only, but definitely no need for any "copy-back".
INTEGER*4 THIS,NUMB(1:*) !Where is THIS in array NUMB(1:N)?
INTEGER N !The count. In other versions, it is supplied by the index.
INTEGER L,R,P !Fingers.
Chop away.
L = 0 !Establish outer bounds.
R = N + 1 !One before, and one after, the first and last.
1 P = (R - L)/2 !Probe point offset. Beware integer overflow with (L + R)/2.
IF (P.LE.0) THEN !Aha! Nowhere! And THIS follows NUMB(L).
FINDI4 = -L !Having -L rather than 0 (or other code) might be of interest.
RETURN !Finished.
END IF !So much for exhaustion.
P = P + L !Convert from offset to probe point.
IF (THIS - NUMB(P)) 3,4,2 !Compare to the probe point.
2 L = P !Shift the left bound up: THIS follows NUMB(P).
GO TO 1 !Another chop.
3 R = P !Shift the right bound down: THIS precedes NUMB(P).
GO TO 1 !Try again.
Caught it! THIS = NUMB(P)
4 FINDI4 = P !So, THIS is found, here!
END FUNCTION FINDI4 !On success, THIS = NUMB(FINDI4); no fancy index here...
END MODULE SORTSEARCH

View file

@ -0,0 +1,6 @@
TYPE STUFF
INTEGER CODE !A key number.
CHARACTER*6 NAME !Associated data.
INTEGER THIS !etc.
END TYPE STUFF
TYPE(STUFF) TABLE(600) !An array of such entries.

View file

@ -0,0 +1,24 @@
// version 1.0.6
class BinaryTree<T>(var value: T) {
var left : BinaryTree<T>? = null
var right: BinaryTree<T>? = null
fun <U> map(f: (T) -> U): BinaryTree<U> {
val tree = BinaryTree<U>(f(value))
if (left != null) tree.left = left?.map(f)
if (right != null) tree.right = right?.map(f)
return tree
}
fun showTopThree() = "(${left?.value}, $value, ${right?.value})"
}
fun main(args: Array<String>) {
val b = BinaryTree(6)
b.left = BinaryTree(5)
b.right = BinaryTree(7)
println(b.showTopThree())
val b2 = b.map { it * 10.0 }
println(b2.showTopThree())
}

View file

@ -0,0 +1 @@
?sort(shuffle({5,"oranges",6,"apples",7}))

View file

@ -0,0 +1,39 @@
enum data, left, right
function tmap(sequence tree, integer rid)
tree[data] = call_func(rid,{tree[data]})
if tree[left]!=null then tree[left] = tmap(tree[left],rid) end if
if tree[right]!=null then tree[right] = tmap(tree[right],rid) end if
return tree
end function
function newnode(object v)
return {v,null,null}
end function
function add10(atom x) return x+10 end function
procedure main()
object root = newnode(1.00)
-- Add some nodes.
root[left] = newnode(1.10)
root[left][left] = newnode(1.11)
root[left][right] = newnode(1.12)
root[right] = newnode(1.20)
root[right][left] = newnode(1.21)
root[right][right] = newnode(1.22)
-- Now the tree has seven nodes.
-- Show the whole tree.
ppOpt({pp_Nest,2})
pp(root)
-- Modify the whole tree.
root = tmap(root,routine_id("add10"))
-- Show the whole tree again.
pp(root)
end procedure
main()

View file

@ -1,35 +1,35 @@
/*REXX program demonstrates a method of parametric polymorphism in REXX.*/
call newRoot 1.00, 3 /*new root and indicate 3 stems. */
/* [↓] no need to label the stems*/
call addStem 1.10 /*new stem and its initial value.*/
call addStem 1.11 /* " " " " " " */
call addStem 1.12 /* " " " " " " */
call addStem 1.20 /* " " " " " " */
call addStem 1.21 /* " " " " " " */
call addStem 1.22 /* " " " " " " */
call sayNodes /*display nicely formatted values*/
call modRoot 50 /*MOD will add 50 to all stems. */
call sayNodes /*display nicely formatted values*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────MODROOT─────────────────────────────*/
modRoot: do j=1 for nodes /*traipse through all the nodes. */
do k=1 for stems; _=root.j.k; ?=datatype(_,'N')
if ? then root.j.k=_+arg(1) /*can stem be added to?*/
end /*k*/ /* [↑] only add if it's numeric.*/
end /*j*/
return
/*──────────────────────────────────NEWROOT─────────────────────────────*/
newRoot: stems=arg(2); nodes=-1; /*set NODES to a kind of "null". */
call addStem copies('',9); call addStem arg(1)
return
/*──────────────────────────────────SAYNODES────────────────────────────*/
sayNodes: say; do j=0 to nodes; _= /*each node gets shown*/
do k=1 for stems; _=_ right(root.j.k,9); end /*k*/
say substr(_,2) /*ignore the 1st blank*/
/*REXX program demonstrates (with displays) a method of parametric polymorphism in REXX.*/
call newRoot 1.00, 3 /*new root, and also indicate 3 stems.*/
/* [↓] no need to label the stems. */
call addStem 1.10 /*a new stem and its initial value. */
call addStem 1.11 /*" " " " " " " */
call addStem 1.12 /*" " " " " " " */
call addStem 1.20 /*" " " " " " " */
call addStem 1.21 /*" " " " " " " */
call addStem 1.22 /*" " " " " " " */
call sayNodes /*display some nicely formatted values.*/
call modRoot 50 /*modRoot will add fifty to all stems. */
call sayNodes /*display some nicely formatted values.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
addStem: nodes=nodes+1; do j=1 for stems; root.nodes.j=arg(1); end; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
modRoot: do j=1 for nodes /*traipse through all the defined nodes*/
do k=1 for stems
if datatype(root.j.k, 'N') then root.j.k=root.j.k + arg(1) /*bias.*/
end /*k*/ /* [↑] only add if numeric stem value.*/
end /*j*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
newRoot: stems=arg(2); nodes= -1 /*set NODES to a kind of "null". */
call addStem copies('', 9); call addStem arg(1)
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
sayNodes: say; do j=0 to nodes; _= /*ensure each of the nodes gets shown. */
do k=1 for stems; _=_ right(root.j.k, 9)
end /*k*/
say substr(_, 2) /*ignore the first (leading) blank. */
end /*j*/
say left('', stems*9+stems) || '('nodes" nodes)" /*also show # of nodes*/
return
/*──────────────────────────────────ADDSTEM─────────────────────────────*/
addStem: nodes=nodes+1; do j=1 for stems; root.nodes.j=arg(1); end /*j*/
return
say left('', stems*11) || '('nodes" nodes)" /*also show number of nodes.*/
return