/* Stream Exchange Sort In an application not all algorithms need to be parallel. The elegance of programming with a process model over conventional programming comes from the useful composition of processes. This example illustrates a single, sequential process, acting upon a Stream context. The program is an implementation of simple sort by exchange, but this example servers as an illustration of resource use and the passing of contexts as first class data structures. */ type STRING'STREAM context stream string type FILE'STORE context resource STRING'STREAM reply STRING'STREAM let file'system := FILE'STORE procedure exchange'sort (STRING'STREAM strings) /* sort a non-empty string context */ let s := _ -> STRING let t := _ -> STRING let swaps := true let end'marker = "A unique string" : { while swaps : { strings ! end'marker strings ? s strings ? t swaps := false enumerate s', t' let l := t' : while s != end'marker and t != end'marker : test s < t : { strings ! s strings ? s test fin = s' : swaps := true else fin := s' . } t < s : { strings ! t strings ? t test fin = t' : swaps := true fin = s' : fin := t' . } s = t : test fin = t' : { strings ! s strings ? s } fin = s' : { strings ! t strings ? t } . . /* leave end marker *after* fin "sorted" item */ test s = end'marker : { strings ! s strings ? t } t = end'marker : { strings ! t strings ? s } . } strings ? s } : || users() || let data := STRING'STREAM : while true : resource get data from file'system : exchange'sort (data) put data . .