[Swift-devel] Re: folding/sequential-while/whatever and accessing data in files

Ben Clifford benc at hawaga.org.uk
Fri Sep 7 15:48:49 CDT 2007


So I fiddled a little and have this code that works ok:

iterate {
  a[foo+1] = countstep(a[foo]);
 print("extract int value ", at extractint(a[foo+1]));
} until (@extractint(a[foo+1]) <= 2);

(fully program at the end of this message)

which runs countstep in sequence until a convergence criteria is met.

countstep is simple app which counts the number of bytes in its input file 
and outputs that count to its output file (thus converges rapidly to 1)

What is different about this over previous repeat/while statements is 
that:
 
 there's an implicitly defined variable 'foo' that iterate{} makes 
available for each iteration. In the first iteration, the value of foo is 
0, and the value is increased with each subsequent iteration, up to 
(Swift's version of) infinity.

 there's a function to move from file-content-space to 
swift-expression-space (the previously discussed @extractint)

This seems to be the right direction to be going for 
fold-like/iteration-like structures.

Full files follow:

$ cat fold2.swift:

type counterfile {}

(counterfile t) greeting(string m) { 
  app {
    echo m stdout=@filename(t);
  }
}

(counterfile t) countstep(counterfile i) {
  app {
    wcl @filename(i) @filename(t);
  }
}

counterfile startfile <"longstartint.txt">;

counterfile a[]  <fixed_array_mapper;files="a b c d e f g h i j k l m n o 
p q r 
s t u v w x y z">;

a[0] = greeting("793578934574893");

iterate {
  a[foo+1] = countstep(a[foo]);
 print("extract int value ", at extractint(a[foo+1]));
} until (@extractint(a[foo+1]) <= 2);


$ cat wcl
#!/bin/bash

echo $(wc -c < $1) > $2




More information about the Swift-devel mailing list