digitalmars.D.learn - Compile-time variadic equality
- =?UTF-8?B?Tm9yZGzDtnc=?= (9/9) Mar 09 2018 I'm looking for a function (that probably should be placed in
- =?UTF-8?B?Tm9yZGzDtnc=?= (28/37) Mar 09 2018 My current own implementation is
- Simen =?UTF-8?B?S2rDpnLDpXM=?= (4/13) Mar 09 2018 NoDuplicates!V.length == 1
- =?UTF-8?B?Tm9yZGzDtnc=?= (49/54) Mar 10 2018 BTW, `NoDuplicates` can handle both types and values (mixed)
- =?UTF-8?B?Tm9yZGzDtnc=?= (2/4) Mar 10 2018 I found the privately defined `isSame` in std.meta that does this.
I'm looking for a function (that probably should be placed in std.meta) named something like `areEqual` that checks whether all it's arguments are equal or not. Is there such as function already in Phobos? My usage is static if (allEqual!(staticMap!(ElementEncodingType, Rs))) { // compare Rs byCodeUnit }
Mar 09 2018
On Friday, 9 March 2018 at 19:24:03 UTC, Nordlöw wrote:I'm looking for a function (that probably should be placed in std.meta) named something like `areEqual` that checks whether all it's arguments are equal or not. Is there such as function already in Phobos? My usage is static if (allEqual!(staticMap!(ElementEncodingType, Rs))) { // compare Rs byCodeUnit }My current own implementation is /** Returns: true iff all values $(D V) are the same. See also: http://forum.dlang.org/post/iflpslqgrixdjwrlqqvn forum.dlang.org See also: http://forum.dlang.org/post/mheumktihihfsxxxapff forum.dlang.org */ template allSame(V ...) if (isExpressions!V) { static if (V.length <= 1) { enum allSame = true; } else static if (V.length & 1) { enum allSame = (V[$ - 1] == V[0] && V[0 .. $/2] == V[$/2 .. $-1] && allSame!(V[0 .. $/2])); } else { enum allSame = (V[0 .. $/2] == V[$/2 .. $] && allSame!(V[0 .. $/2])); } } Should this go into std.meta?
Mar 09 2018
On Friday, 9 March 2018 at 19:24:03 UTC, Nordlöw wrote:I'm looking for a function (that probably should be placed in std.meta) named something like `areEqual` that checks whether all it's arguments are equal or not. Is there such as function already in Phobos? My usage is static if (allEqual!(staticMap!(ElementEncodingType, Rs))) { // compare Rs byCodeUnit }NoDuplicates!V.length == 1 -- Simen
Mar 09 2018
On Friday, 9 March 2018 at 22:13:39 UTC, Simen Kjærås wrote:BTW, `NoDuplicates` can handle both types and values (mixed) opposite to my `allSame` and `allSameType` defined as: template allSame(V...) if (isExpressions!V) { static if (V.length <= 1) { enum allSame = true; } else static if (V.length & 1) // odd count { enum allSame = (V[0] == V[$ - 1] && // first equals last V[0 .. $/2] == V[$/2 .. $-1] && // (first half) equals (second half minus last element) allSame!(V[0 .. $/2])); } else // event count { enum allSame = (V[0 .. $/2] == V[$/2 .. $] && // (first half) equals (second half) allSame!(V[0 .. $/2])); } } and template allSameType(V...) // TODO should have template restriction on `V` { static if (V.length <= 1) { enum allSameType = true; } else static if (V.length & 1) // odd count { enum allSameType = (is(V[0] == V[$ - 1]) && // first equals last is(V[0 .. $/2] == V[$/2 .. $-1]) && // (first half) equals (second half minus last element) allSameType!(V[0 .. $/2])); } else // even count { enum allSameType = (is(V[0 .. $/2] == V[$/2 .. $]) && // (first half) equals (second half) allSameType!(V[0 .. $/2])); } } How do I most conveniently merge these into one single `allSame` that can operate on mixtures of values and types?static if (allEqual!(staticMap!(ElementEncodingType, Rs))) { // compare Rs byCodeUnit }NoDuplicates!V.length == 1
Mar 10 2018
On Saturday, 10 March 2018 at 08:49:19 UTC, Nordlöw wrote:How do I most conveniently merge these into one single `allSame` that can operate on mixtures of values and types?I found the privately defined `isSame` in std.meta that does this.
Mar 10 2018