Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: virtual files?



"Benjamin R. Haskell" <zsh@xxxxxxxxxx> writes:

> Using the `=()` substitution ZyX mentions:
>
> inflation () {
>   local usd=${1:-10}
>   # year
>   local then=${2:-1950}
>   local now=`date +"%Y"`
>   local link="
> http://data.bls.gov/cgi-bin/cpicalc.pl?cost1=$usd&year1=$then&year2=$now";
>   () {
>     local tmp=$1
>     wget -q $link -O $tmp
>     echo -n \$
>     grep \"answer\" $tmp | cut -d \$ -f 2 | cut -d \< -f 1
>   } =(:)
> }

Great!

If was something like that I had in mind. Tho the
mental image was more like the Lisp syntax for doing
scopes, e.g. `let' and `with-selected-frame' in:

    (defun new-frame-show-grep ()
      (let ((new-frame (make-frame-command)))
        (setq *grep-frame* new-frame)
        (with-selected-frame new-frame
          (switch-to-buffer "*grep*") )))

Actually the zsh anonymous function syntax doesn't
remind me of anything I've seen, save for perhaps some
stuff in zsh itself. The parameter list and body
without a name make sense, but what comes after the
closing curly bracket looks like a ghost, wearing
a tie...

What is the colon in =(:) - an "anonymous function
qualifier"?

Only remark is, if you do all this trouble to get
clean code, and then name the local "tmp", it is
almost comical. But I get it, you did that for me (all
readers), not the code per se...

> Also, I prefer `curl` over `wget`, and if you're
> curious, here's how I might write the entire
> function:

I have used them both and never reflected on which was
superior, but taking a look at your code:

> inflation() {
>   curl -s -d cost1=${1:-10} -d year1=${2:-1950} -d year2=$(date +%Y) \
>     http://data.bls.gov/cgi-bin/cpicalc.pl |
>   awk '/"answer"/' |
>   tr -d -c '$0-9.,\n'
> }

I agree that using the --data's to set up the
CGI-to-be-Perl-input arguments is less hackish than
putting together the URL manually (fine, just once,
but anyway is much less clear). Ironic thing is, doing
it manually might actually be faster since you can
then just copy-paste and modify the URL. So anyway
I changed to curl, only I somewhat didn't use your
compact style.

awk instead of grep in this context should be strange
to many who are more familiar with grep, but grep has
the problem that many like to put colorization to it
which can screw up parsing. If the configuration
is in an environmental, I suppose not even giving the
full path to the binary to bypass an alias would get
them away. One can supply a specification, of course,
telling grep not to use colors, but rather than doing
that, I switched to awk as you suggested.

I also changed the two cut:s for tr, and date +%Y from
the `one` syntax form into $(another).
Those I consider even more minor improvements, however
they are still better so I changed them as well. If it
is meaningless to do, I might as well do it!

So here is the latest version:

inflation () {
    local url=http://data.bls.gov/cgi-bin/cpicalc.pl
    curl -s                   \
         -d cost1=${1:-10}    \
         -d year1=${2:-1950}  \
         -d year2=$(date +%Y) $url |
        awk '/"answer"/'           |
        tr -d -c '$0-9.,\n'
}

One interesting thing is tho I changed three programs
out of three used, I don't think this function is
anything "remote" to the one I originally wrote.
A small homage to the diversity of the Linux/Unix
tools, I suppose. But come to think of it there are
many players in the NHL that score some ~15 goals
every season, but that doesn't mean they cannot also
be completely different players, physical aspects as
well as those of skill and style...

> (Though the create-a-tempfile problem is certainly
> interesting in its own right.)

Yes.

It cannot be like this for too many steps, is my take:

    a | b | ... | n

But this looks like some TI-82 stuff I kid would do

    let f=file_path
    # do things to f
    rm $f

As for now, what I'll do is to keep it short, stupid!
(KISS - Thatcher quote, I think) and so be able to put
together parsings, and when that doesn't work as in
longer pieces of software, I'll consider getting som
order by use of the anonymous software.

http://user.it.uu.se/~embe8573/conf/.zsh/money

-- 
underground experts united .... http://user.it.uu.se/~embe8573
Emacs Gnus Blogomatic ......... http://user.it.uu.se/~embe8573/blogomatic
                   - so far: 26 Blogomatic articles -                   



Messages sorted by: Reverse Date, Date, Thread, Author