newlisp with filter function (unix only)

Pondering the philosophy behind the language

newlisp with filter function (unix only)

Postby newdep » Sat May 19, 2012 8:01 pm

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))
newdep
 
Posts: 2019
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Re: newlisp with filter function (unix only)

Postby Lutz » Sun May 20, 2012 7:15 am

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"
Lutz
 
Posts: 5259
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California

Re: newlisp with filter function (unix only)

Postby newdep » Sun May 20, 2012 7:50 am

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))
newdep
 
Posts: 2019
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Re: newlisp with filter function (unix only)

Postby Lutz » Sun May 20, 2012 8:21 am

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'.
Lutz
 
Posts: 5259
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California

Re: newlisp with filter function (unix only)

Postby newdep » Sun May 20, 2012 8:35 am

Of all options i did not test read-line... how stupid of me ;-) Thanks..
-- (define? (Cornflakes))
newdep
 
Posts: 2019
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands


Return to Whither newLISP?

Who is online

Users browsing this forum: No registered users and 1 guest

cron