File misc_util.icn

Summary

General purpose routines contributed by various people. This is one of several files contributing to the util package.

Author: Steve Wampler (sbw@tapestry.tucson.az.us)

This file is in the public domain.

Procedures:
breadthwalk, buildStackTrace, deepcopy, defaultGenChildren, depthwalk, evenResults, getClassField, getMethod, initRandom, invertTable, nthResults, oddResults, permuteList, skipFirst, time, weave

This file is part of the util package.

Source code.

Details
Procedures:

breadthwalk(root, getChildren)

Parameters:
root
node of graph to start walk at
getChildren
Generates:
the nodes in sequence that are reachable from the root

Breadth-first walk of a connected graph (nodes are visited exactly once). Note that the graph can be a tree.

The second argument should be a function that, when given a node, generates the links from that node. If omitted, it defaults to:

     procedure defaultGenChildren(node)
        suspend !\node[2]
     end
function that generates a node's children


buildStackTrace(n:0, ce)

Parameters:
n
starting distance from this call
ce
Generates:
the stacktrace from current call back to first in the co-expression

Compute the current stack trace. Starting at level n above the current procedure. Here, n defaults to 0, which will include this procedure in the stack trace. ce defaults to ¤t. This only works with newer versions of Unicon!

co-expr to trace stack in [¤t]


deepcopy(A, cache)

Parameter:
A
- structure to copy
Returns:
a deep copy of any structure, including cyclic ones

deepcopy(A) produces a full copy of the structure A. All structures are handled, including all dags and cycylic graphs.


defaultGenChildren(node)

Parameter:
node
graph node to produce children from
Generates:
child nodes of node

Given node represented by an indexable structure where the 2nd element is a list of links from that node, generate those links. Used as default in breadthwalk() and depthwalk().


depthwalk(root, getChildren, visited)

Parameters:
root
node of graph to start walk at
getChildren
function that generates a node's children
visited
Generates:
the nodes in sequence that are reachable from the root

Depth-first walk of a connected graph (nodes are visited exactly once). Note that the graph can be a tree.

The second argument should be a function that, when given a node, generates the links from that node. If omitted, it defaults to:

     procedure defaultGenChildren(node)
        suspend !\node[2]
     end

The third argument is used internally and should not be given by the user.

ignore, used internally.


evenResults(L)

A PDCO that produces only the even results from its argument. <[/p> First argument must be a co-expression.


getClassField(c, s:string)

Parameters:
c
instance of a class
s
name of field to produce from c
Returns:
field named s

Returns the field named s from the class instance c. The field is not dereferenced and thus can be assigned to.


getMethod(c, s:string)

Parameters:
c
instance of a class
s
name of method to produce from c
Returns:
method named s

Returns the method named s of the class instance c. The method is not dereferenced and thus can be assigned to. Methods of superclasses may be accessed by making s a period delimited list, for example getMethod(foo, "A.b") returns the method b of the superclass of type A that foo inherits from.


initRandom()

Returns:
the intial seed

Initialize the random number seed to a 'random' value, so a different random sequence is generated on each run of the program. Meant to be called once, in procedure main(). . Unicon now initializes the seed by default, so this is largely moot.


invertTable(tbl)

Returns:
a copy of the inverted table tbl

Invert a table (will lose data unless tbl is a 1-1 mapping).

Table to invert


nthResults(L)

Parameters:
n
(first coexpression in L) produce every nth result
sq
(second coexpression in L) sequence to produce results from
Generates:
every nth result from sq

A PDCO that wraps a result sequence sq and produces every nth result. n defaults to 1.

first arg is n, second is sq.


oddResults(L)

A PDCO that produces only the odd results from its argument. <[/p> First argument must be a co-expression


permuteList(L)

Generates:
all permutations of the elements in list L

List to permute


skipFirst(L)

Parameters:
n
(first coexpression in L) produce results after first n results
sq
(second coexpression in L) sequence to produce results from
Generates:
all results after the first n from sq

A PDCO that wraps a result sequence sq and produces all results after the first n results. n defaults to 0.

first arg is n, second is sq.


time(L)

Returns:
the time in milliseconds to execute its argument.

A PDCO that can be used to time expressions. (Only accurate to the millisecond level.)

Examples:

   write(" [",time { writes("fib(",n,") = ",fib(n)) }, "ms]" )

   write(" [", time { {every 1 to limit do {
                      f := fib(n)
                      }
                  writes("fib(",n,") = ",f, " run ",limit," times.")
                 }   # Note extra braces for compound expression!
               }
        , "ms]")

First arg is expression to time


weave(L)

Parameter:
L
- arbitrary number of co-expressions
Generates:
results from all the co-expressions in L by interleaving

A PDCO that interleaves results from its arguments. For example, the following code produces a (very fast) 'spinner':

   every writes(weave{!|"-\\|/", !|"\b"})

Expressions whose results are to be woven together



This page produced by UniDoc on 2021/04/15 @ 23:59:54.