zsh-users
 help / color / mirror / code / Atom feed
From: ZyX <kp-pav@yandex.ru>
To: Ray Andrews <rayandrews@eastlink.ca>,
	"zsh-users@zsh.org" <zsh-users@zsh.org>
Subject: Re: wheels within wheels
Date: Wed, 30 Sep 2015 11:01:20 +0300	[thread overview]
Message-ID: <357101443600080@web28g.yandex.ru> (raw)
In-Reply-To: <560B61C5.2080001@eastlink.ca>

30.09.2015, 07:16, "Ray Andrews" <rayandrews@eastlink.ca>:
> On 09/29/2015 08:40 PM, Bart Schaefer wrote:
>>  }
>>  } I just learned that it's possible to declare a function within another
>>  } function. A strange liberty.
>>
>>  Interpreted language.
>
> Yes. It's so easy to forget that.
>>       function foo {
>>         echo defining bar
>>         function bar {
>>          echo hello from bar
>>         }
>>         echo redefining foo
>>         function foo {
>>          echo hello from new foo
>>          bar
>>         }
>>         # this looks like infinite recursion,
>>         # but it is not, because foo was redefined
>>         foo
>>       }
>
> Now that is mind expanding. If you tried that in C you'd collapse the
> universe.
> Interpreted .... sure, it can saw off the branch it's sitting on because
> there is no
> tree. Each command is past when it's past so that must mean that the
> address of
> first foo ... is there ... yes of course there is, the thing is in
> memory ... just aborts
> when second foo comes along. It will expect grammatical completion of
> first foo
> for the sake of etiquette. And when foo calls itself, it calls new foo
> which is
> not recursive. I am not yet able to snatch the pebble out of your hand,
> but that's
> a zsh koan.

Nested *same* function definition is rather useful for initialization. Or when you need to alter the implementation based on some condition, and want to cache this to not check this condition again:

    # This will parse json array passed as argument to a shell array $reply.
    # Depending on whether zpython module is available it will either use built-in python interpreter support
    # which is faster or call python in a subshell.
    parse_json_array() {
        if zmodload libzpython 2>/dev/null ; then
            parse_json_array() {
                zpython 'import zsh, json; zsh.setvalue("reply", json.loads(zsh.getvalue("1")))'
            }
        else
            parse_json_array() {
                reply=( "${(@0)"$(python -c 'import sys, json; sys.stdout.write("\0".join(json.loads(sys.argv[1])))' "$1")"}" )
            }
        fi
        parse_json_array "$@"
    }

. Also this is a feature sometimes useful for metaprogramming: you can create a function which defines a function through eval. Will be needed if you for some reason want something like bash `export -f` feature.

And this is *not* a zsh koan. I have been using the same technique for the same reasons in Python: e.g. in https://github.com/ZyX-I/powerline/blob/5ebf0875b85f85e7cd4c1e07aa8cdebbcc3bd17e/powerline/segments/common/bat.py#L163. Many interpreted languages allow this. Specifically *all* POSIX-like shells I know allow this: https://github.com/ZyX-I/powerline/blob/5ebf0875b85f85e7cd4c1e07aa8cdebbcc3bd17e/powerline/bindings/shell/powerline.sh#L74-L99 is known to work in dash, [m]ksh, busybox ash and bash.

In C you may do something similar if you use function pointer as a public interface and store different address, but this makes call indirect. If you are OK with binding yourself to GCC you can even have nested definitions.


  parent reply	other threads:[~2015-09-30  8:08 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-17 15:54 autoload Ray Andrews
2015-09-17 17:34 ` autoload Bart Schaefer
2015-09-17 19:22   ` autoload Ray Andrews
2015-09-17 20:40     ` autoload Bart Schaefer
2015-09-17 23:06       ` autoload Ray Andrews
2015-09-17 23:20         ` autoload Bart Schaefer
2015-09-18  1:20           ` autoload Ray Andrews
2015-09-18  4:04             ` autoload Bart Schaefer
2015-09-18  5:00               ` autoload Ray Andrews
2015-09-18  5:52                 ` autoload Bart Schaefer
2015-09-18 15:49                   ` autoload Ray Andrews
2015-09-18 16:52                     ` autoload Bart Schaefer
2015-09-18 18:29                       ` autoload Ray Andrews
2015-09-18 19:02                         ` autoload Bart Schaefer
2015-09-18 22:57                           ` autoload Ray Andrews
2015-09-19  0:14   ` autoload Bart Schaefer
2015-09-19 15:04     ` autoload Ray Andrews
2015-09-19 16:29       ` autoload Bart Schaefer
2015-09-19 18:13         ` autoload Ray Andrews
2015-09-19 21:22           ` autoload Bart Schaefer
2015-09-19 22:12             ` autoload Ray Andrews
2015-09-20  5:53               ` autoload Bart Schaefer
2015-09-20 15:37                 ` autoload Ray Andrews
2015-09-20 15:59                   ` autoload Bart Schaefer
2015-09-20  0:58             ` autoload Ray Andrews
2015-09-20  5:41               ` autoload Bart Schaefer
2015-09-20 23:21                 ` autoload Ray Andrews
2015-09-21  4:18                   ` autoload Bart Schaefer
2015-09-21 17:03                     ` autoload Ray Andrews
2015-09-21 18:17                       ` autoload Bart Schaefer
2015-09-21 20:09                         ` autoload Ray Andrews
2015-09-22  3:19                           ` autoload Bart Schaefer
2015-09-22 17:33                             ` autoload Ray Andrews
2015-09-23  4:39                               ` autoload Bart Schaefer
2015-09-23 15:06                                 ` autoload Ray Andrews
2015-09-29 23:16                             ` wheels within wheels Ray Andrews
2015-09-30  2:55                               ` Kurtis Rader
2015-09-30  3:24                                 ` Ray Andrews
2015-09-30  3:40                                 ` Bart Schaefer
2015-09-30  4:03                                   ` Mikael Magnusson
2015-09-30  4:15                                   ` Ray Andrews
2015-09-30  7:05                                     ` Bart Schaefer
2015-09-30 15:06                                       ` Ray Andrews
2015-09-30  8:01                                     ` ZyX [this message]
2015-09-30 15:18                                       ` Ray Andrews

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=357101443600080@web28g.yandex.ru \
    --to=kp-pav@yandex.ru \
    --cc=rayandrews@eastlink.ca \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).