help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Why bash does not recoganize array type smartly?


From: Dan Douglas
Subject: Re: [Help-bash] Why bash does not recoganize array type smartly?
Date: Wed, 16 Mar 2016 18:01:17 +0000

On 3/16/16, Stephane Chazelas <address@hidden> wrote:
> 2016-03-16 01:12:18 +0000, Dan Douglas:
> [...]
>>  $ csharp -pkg:dotnet -e '
>> new[] { "foo", "bar", "baz", "bleh", "Blerg" }
>>     .Select((x, num) => new KeyValuePair<string, int>(x, num))
>>     .ToDictionary(x => x.Key, x => x.Value);'
>> {{ "foo", 0 }, { "bar", 1 }, { "baz", 2 }, { "bleh", 3 }, { "Blerg", 4 }}
> [...]
>
> For the record, what the equivalent could be in zsh:

I don't really care that much. Zsh is a giant pile of random syntax
that accomplishes a fraction of what most other languages can do with
less. I'd rather not see bash follow suit. In the C# case you can
solve your empty key problem with a few overloaded extension methods.

public static T2 GetValueOrDefault<T1, T2>(
    this IDictionary<T1, T2> d, T1 key, T2 def = default(T2)
) {
    T2 ret;
    return d.TryGetValue(key, out ret) ? ret : def;
}

public static T2 GetValueOrDefault<T1, T2>(
    this IDictionary<T1, T2> d, IEnumerable<T1> keys, T2 def = default(T2)
) {
    T2 ret = default(T2);
    return keys.Any(x => d.TryGetValue(x, out ret)) ? ret : def;
}

In python you use the `collections.defaultdict` type from the standard
library, but you could implement it yourself without that pretty
easily.

Es6 is a great example of a language that has added extensible
collections late in life, without even the need for java-like
interfaces or multiple inheritence. It gives you things equivalent to
extension methods (by modifying a built-in type's prototype). Like
bash/ksh, it has lots of implementations, legacy code, and portability
to deal with. Yet it manages to not entirely suck at data structures.
It's also not too bad with an interactive REPL.

ksh gets you part of the way with discipline functions but they aren't
powerful enough to deal with arrays properly most of the time.

I'm also not proposing you should have to sacrifice concise
command-line usage. Indeed the languages I'm talking about make
terrible shells. That doesn't mean shells have to be terrible
languages.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]