[Swift-user] @tostring?

wilde at mcs.anl.gov wilde at mcs.anl.gov
Sat Mar 27 17:10:49 CDT 2010


Andriy,

At the moment (and maybe forever ;) "sizeof()" is a black hole. Dont go there if possible.

It depends somewhat on what you want to do with the rest of the string, but if all you want to do is process the last segment of the string, what I would do is add tiny external functions that execute on localhost to deal with things like that (I.e, just grabbing the last segment of a string).

I can provide 3 tips for doing this.

(0) note that @strcat() will also convert floats, and I presume, boolean.

(1) I started to play with using "tiny" fast external apps like echo, sed, awk to start building up a useful library of string functions. I didnt take this any further than what I needed at the time; but I believe the technique is useful.

I paste below my crude start at this. With a few entries for filters on localhost, you can do quite a bit. Ignore performance (its usually fine); think of it like doing command interpolation in the shell (`` or $(cmd) )

(2) With a single entry for a "swiftshell" in tc.data, you can compose shell filters in Swift itself. I paste that below too. Beware, though - Sarah seems to have encountered a bug or issue in which a provider (likely coasters) is wiping out a "$1" in one such shell. But I think localhost is OK, which is what youd want to use.

(3) Justin has added an @java() primitive to the development trunk; so if there's a Java method among standard or Swift Java classes that you can call to do a needed string operation, that may be useful. I cant recall if that was posted to one of the lists or not.

Hope one of these works for you; I think (2) is a pretty good starting bet. At least one Google GSoC student has indicated interest in building up a starter Swift library for the community, so please post your needs to this list as they come up.

- Mike

=== (1) - A "toy" Swift library:

// General Swift Lib Functions

app (file o) echoi (int i)     { echo i stdout=@o;}
app (file o) echof (float f)   { echo f stdout=@o;}
app (file o) echob (boolean b) { echo b stdout=@o;}
app (file o) echos (string s)  { echo s stdout=@o;}

(string s) itostr (int i)
{
  file f;
  f = echoi(i);
  s = readData(f);
}

(string s) ftostr (float n)
{
  file f;
  f = echof(n);
  s = readData(f);
}

(int n) strtoi (string s)
{
  file f;
  f = echos(s);
  n = readData(f);
}

(float n) strtof (string s)
{
  file f;
  f = echos(s);
  n = readData(f);
}

app (file o) sprintfsApp (string fmt, string e[])
{
  sprintfs fmt e stdout=@o;
}

(string s) sprintfs (string fmt, string e[])
{
  file f;
  f = sprintfsApp(fmt,e);
  s = readData(f);
}

=== (2) swiftshell:

login1$ more shelldemo.swift swiftshell
::::::::::::::
shelldemo.swift
::::::::::::::
type file;

app (file o) cat (file i)
{
  shell " ( cat " @i "; date; hostname ) | grep . " stdout=@o;
}

file data<"data.txt">;
file out<"out.txt">;
out = cat(data);
::::::::::::::
swiftshell
::::::::::::::
bash -c "$*"
login1$ grep shell tc
localhost 	shell	/home/wilde/swift/lab/swiftshell	null	null	null
mcs	 	shell	/home/wilde/swift/lab/swiftshell	null	null	null
login1$ 

=== (3) @java()

Justin previously posted this to the list:

"         If you can check out the latest Swift from trunk I've added some 
features that might help you out here.  There's a new built-in function @java() 
that allows you to call into an existing Java library.  You can call into the 
Java Platform or into your CLASSPATH.
         Here is one example:

(float result) sin(float x) {
   result = @java("java.lang.Math", "sin", x);
}

float x = 0.5;
float y = sin(x);

trace("sin", x, y);

Note that you currently have to assign the result of @java() to a variable."




----- "Andriy Fedorov" <fedorov at bwh.harvard.edu> wrote:

> On Sat, Mar 27, 2010 at 17:34, Michael Wilde <wilde at mcs.anl.gov>
> wrote:
> > Andriy, Im pretty sure @strcat() will take an int and return a
> string.
> >
> 
> Mike, yes, you are right -- there was another error in my script,
> @strcat indeed works with int.
> 
> One more basic question (sorry if I missed this in the guide): is
> there a way to get the size of an array? I would like to split a
> string, and get the last item in the array it returns. Is this
> possible?
> 
> 
> > - Mike
> >
> > ----- "Andriy Fedorov" <fedorov at bwh.harvard.edu> wrote:
> >
> >> Hi,
> >>
> >> Is it possible to convert int type to string type in Swift?
> >>
> >> I see @toint, but not @tostring, and it looks like I am not able
> to
> >> pass int to @strcat().
> >>
> >> Thanks
> >>
> >> --
> >> Andriy Fedorov, Ph.D.
> >>
> >> Research Fellow
> >> Brigham and Women's Hospital
> >> Harvard Medical School
> >> 75 Francis Street
> >> Boston, MA 02115 USA
> >> fedorov at bwh.harvard.edu
> >> _______________________________________________
> >> Swift-user mailing list
> >> Swift-user at ci.uchicago.edu
> >> http://mail.ci.uchicago.edu/mailman/listinfo/swift-user
> >
> > --
> > Michael Wilde
> > Computation Institute, University of Chicago
> > Mathematics and Computer Science Division
> > Argonne National Laboratory
> >
> >

-- 
Michael Wilde
Computation Institute, University of Chicago
Mathematics and Computer Science Division
Argonne National Laboratory




More information about the Swift-user mailing list