newlisp with filter function (unix only)

Pondering the philosophy behind the language
Locked
newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

newlisp with filter function (unix only)

Post by newdep »

Hi Lutz,

is it possible to extend newlisp so that its capable of beeing a 'filter'.
So it accepts input during startup.

example:

echo "(println \"hello newlisp\")" | newlisp -e '(eval $main-args)'

I know $main-args wont work here but its just an example.

I would like to use newlisp in the command stream of linux this way.

Hope its something you to concider?

Greetings :-)
-- (define? (Cornflakes))

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: newlisp with filter function (unix only)

Post by Lutz »

The following examplewould work like a filter:

Code: Select all

#!/usr/bin/newlisp
# demo filter script as pipe
# example:
#          ./filter < my-text

(while (read-line) (println (upper-case (current-line))))
(exit)
you could also use it like this:

Code: Select all

cat mytext.txt | filter | grep 'FOO BAR'
instead of 'upper-case' you could do anything else on the incoming lines.

See also here: http://www.newlisp.org/CodePatterns.html#toc-2 for a sub chapter "File filters"

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Re: newlisp with filter function (unix only)

Post by newdep »

Hi Lutz,

...yes yes i know :-), but my suggestion is more related to newlisp as being a command-line filter
and not such as being able to filter, its about 'not' creating a file/script when using newlisp but
about using newlisp on the command-line.

newlisp has functions that can be used on the regullar command-line in the OS, outside newlisp.


Just simple example on how it could be used:

Code: Select all

# assuming $IN eats from STDIN on the commandline
echo 234.234 | newlisp -e '(add $IN 24.4234)'
258.6574

#or
cat myfile | newlisp -e '(explode $IN)'

#or
firstscript.lsp | newlisp -e '(net-send-udp "host.com" 1233 $IN)' 

# or


Greetings :-)
-- (define? (Cornflakes))

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: newlisp with filter function (unix only)

Post by Lutz »

Same thing, use 'read-line' for STDIN:

Code: Select all

~> echo hello | newlisp -e '(upper-case (read-line))'
"HELLO"
~> echo 123.456 | newlisp -e '(mul 2 (float (read-line)))'
246.912
but for multiple lines you would use '(while (read-line) (do-thing-with (current-line)))'.

Probably you are looking for automatic iterating over the input without 'while'.

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Re: newlisp with filter function (unix only)

Post by newdep »

Of all options i did not test read-line... how stupid of me ;-) Thanks..
-- (define? (Cornflakes))

Locked