December 17, 2013
On 12/16/13 5:02 PM, Chris Cain wrote:
> On Monday, 16 December 2013 at 20:38:52 UTC, Andrei Alexandrescu wrote:
>> bool between(T, U1, U2)(T v, U1 lo, U2 hi)
>> {
>>     return v >= lo && v <= hi;
>> }
>
> I realize this is, at this point, being retracted. But it would open up
> some interesting ideas. It seems a lot like iota, but it could look
> pretty cool for some purposes. Consider an alternative API where you
> could do things like this:
>
> ---
> if(x in between(2, 7)) {
>     //...
> }
> ---

That's problematic. "Between" is a preposition. Naming a function as a preposition is fine as long as a verb is implied (e.g. "a in b" really means "a _is_ in b", or "a.between(b, c)" really means "a _is_ between b and c" etc).

Reifying "between" to the status of object is weird. One constructs a "between" object and then what are its primitives? How can one even talk about it? "Yeah I have a between here and I copy it to another between"...

"x in between(2, 7)" is cute but just that - it's a lucky strike that relies on word ordering in a particular phrase and is unlikely to work in many other places.


Andrei

December 17, 2013
On 12/17/13 6:36 AM, Marco Leise wrote:
> Now similarly I would prefer if among() could do anything else
> but shift indexes by +1.

The function as defined can be used with if (often), or can be used as an integral (sometimes). I like that idiom very much.

Andrei


December 17, 2013
On 12/17/13 6:38 AM, Marco Leise wrote:
> Compare:
>
> a) import std.something; x.between!(">", "<")(y, z);
> b) y < x && x < z
>
> now which would you use? :-)

x.between!("><")(y, z);

would have the advantage of the South Park emoticon.

Andrei

December 17, 2013
On 12/17/13 7:52 AM, Simen Kjærås wrote:
> On 17.12.2013 15:36, Marco Leise wrote:
>> Now similarly I would prefer if among() could do anything else
>> but shift indexes by +1. Indexes in D are 0 based. Since we
>> are talking about a verbatim set of options here instead of a
>> linear string, I could live with -1 as "not in list" and a
>> zero based index, but then among would have to be renamed to
>> something that doesn't imply a result that converts to boolean.
>> "optionIndex" or something.
>
> findAmong? I find 'among' ambiguous - both a boolean result and an index
> result would fit the name, IMO.

I prefer "among". It's a convenience function. The longer the less convenient.

Andrei

December 17, 2013
On 12/17/13 7:59 AM, Jerry wrote:
> Andrei Alexandrescu <SeeWebsiteForEmail@erdani.org> writes:
>
>> bool between(T, U1, U2)(T v, U1 lo, U2 hi)
>> {
>>      return v >= lo && v <= hi;
>> }
>
> +1
>
>> uint among(T, Us...)(T v, Us vals)
>> {
>>      foreach (i, U; Us)
>>      {
>>          if (v == vals[i]) return i + 1;
>>      }
>>      return 0;
>> }
>
> This seems less useful to me.  What was the example where you found it
> useful?
>
> Jerry

I gave a long litany of examples taken from real code in a subsequent post. In fact I couldn't find motivating examples for "between", or even significant evidence that it would be dramatically useful; I'd included it only for completion, and I have since retracted it.

Andrei

December 17, 2013
On 12/17/13 8:26 AM, H. S. Teoh wrote:
> Maybe this should be a community effort. Let each of us come up with a
> new iota, and we can compare and incorporate each other's ideas to
> produce the best implementation. How's that?

I think the deal with "a in iota(b, c)" gets odd quite fast if e.g. those are floating-point numbers and the questions shows up whether a must be exactly a value obtained by iterating from b to c.

Andrei

December 17, 2013
On 12/17/13 8:43 AM, Byron wrote:
> I don't know why we can do this instead:
>
> if(foo in ["alpha", "beta", "delta"] ) {
>
> }

It's a good idea.

> basically have an opIn operator  x in y -> y.opIn(x)
> U* opIn(T, U)(T key)
> can define in runtime for arrays, and allow custom ones to be defines

No, that should only work for literal arrays.

> and
>
> if(1 < x <= 10) {
>
> }
>
> I remember this being shot down before...

"Don't change semantics of C code".

> Both of these are easier to read, and are more natural.  They also cover
> more cases.

No - "among" could take a custom predicate.


Andrei

December 17, 2013
On 12/17/13 9:06 AM, Jerry wrote:
> Would it make sense to create a Set object in std.container?

Yes.

Andrei


December 17, 2013
On 12/17/13 9:17 AM, Andrej Mitrovic wrote:
> On 12/17/13, Byron <byron.heads@gmail.com> wrote:
>> I don't know why we can't do this instead:
>>
>> if (foo in ["alpha", "beta", "delta"] ) {
>
> You can come pretty close with:
>
> if (foo in A["alpha", "beta", "delta"] )

Wouldn't

if (foo in LiteralSet("alpha", "beta", "delta"))

be a ton faster?


Andrei

December 17, 2013
On 17/12/13 19:08, Andrei Alexandrescu wrote:
> x.between!("><")(y, z);
>
> would have the advantage of the South Park emoticon.

To be defined in the std.unclefucker module? :-)