Article View: comp.lang.c
Article #2061Re: variadic min and max, (any some and none, cat and more)
From: Ross Finlayson
Date: Tue, 01 Dec 2026 16:30
Date: Tue, 01 Dec 2026 16:30
204 lines
8829 bytes
8829 bytes
On 12/01/2026 05:53 AM, David Brown wrote:
> On 01/12/2026 13:11, bartc wrote:
>> On 01/12/2026 09:31, David Brown wrote:
>>> On 30/11/2026 22:34, Lawrence D’Oliveiro wrote:
>>>> On Mon, 30 Nov 2026 20:47:38 +0100, Bonita Montero wrote:
>>>>
>>>>> Am 30.11.2026 um 19:49 schrieb Lawrence D’Oliveiro:
>>>>>
>>>>>> On Mon, 30 Nov 2026 19:44:50 +0100, Bonita Montero wrote:
>>>>>
>>>>>>> It's for sure more flexible since it can handle more variations of
>>>>>>> compatible types and it's for sure much fasger.
>>>>>
>>>>>> You betcha:
>>>>>>
>>>>>> >>> max("red", "green", "blue")
>>>>>> 'red'
>>>>>> >>> max(["red"], ["red", "green"], ["blue"])
>>>>>> ['red', 'green']
>>>>>>
>>>>>> Anything that has an ordering defined will work.
>>>>>
>>>>> In C++ you can write your own conversion to supply compatible types.
>>>>
>>>> Python does custom operator overloads, too. And multiple inheritance.
>>>> And its core definition still ends up being only a small fraction of
>>>> the size of C++.
>>>>
>>>> And unlike C++, and almost like Algol 68, it can do things that look
>>>> like custom operators, too
>>>> <https://gitlab.com/ldo/python_topics_notebooks/-/blob/master/
>>>> Defining%20(Almost)%20Custom%20Operators.ipynb>.
>>>
>>> You can do that just as well in C++ as in Python.
>>>
>>> A 3D vector type in C++ is simple :
>>>
>>> template<typename T>
>>> struct Vector {
>>> T x, y, z;
>>> };
>>>
>>> Vector v1 { 2, 3, 4 };
>>> Vector v2 { 5, 4, 3 };
>>>
>>>
>>>
>>> Making operator overloads for multiplication like your Python
>>> example, with pairwise multiplication and multiplication by a scalar
>>> is standard stuff :
>>>
>>> template<typename T>
>>> constexpr auto operator * (Vector<T> a, Vector<T> b) {
>>> return Vector(a.x * b.x, a.y * b.y, a.z * b.z);
>>> }
>>>
>>> template<typename T>
>>> constexpr auto operator * (Vector<T> a, T b) {
>>> return Vector(a.x * b, a.y * b, a.z * b);
>>> }
>>>
>>> Vector v1_times_v2 = v1 * v2;
>>> Vector v1_times_2 = v1 * 2;
>>>
>>>
>>> Making an additional named operator is just a matter of a simple
>>> wrapper struct whose type is used to change the meaning of operators.
>>> Then you have a singleton type that is used to generate the wrapper.
>>>
>>>
>>> template<typename T>
>>> struct Crosser_wrapper : Vector<T> { };
>>>
>>> struct Cross { };
>>> constexpr Cross cross;
>>>
>>> template<typename T>
>>> constexpr auto operator * (const Vector<T> & a, Cross) {
>>> return Crosser_wrapper(a);
>>> }
>>>
>>> template<typename T>
>>> constexpr auto operator * (Crosser_wrapper<T> a,
>>> Vector<T> b) {
>>> return Vector(a.y * b.z - a.z * b.y,
>>> a.z * b.x - a.x * b.z,
>>> a.x * b.y - a.y * b.x);
>>> }
>>>
>>> Vector v1cross = v1 *cross* v2;
>>>
>>>
>>> Note that this is all zero-cost at runtime. No extra space needed,
>>> no extra function calls generated, no need for the "cross" bit to
>>> hold information or track references to the vector data, etc. It's
>>> just type manipulation to change which function is called.
>>>
>>> And if you want to, you can also do :
>>>
>>> #define 𝘅 *cross*
>>>
>>> Vector v1cross2 = v1 𝘅 v2;
>>>
>>> Nice, to read, but hard to type.
>>>
>>
>>
>>>
>>> (I am not saying that this is a good idea in real code, but it
>>> demonstrates the technique. Use it if you like it. If anyone wants
>>> to discuss this in more detail, look at more generalisations, make it
>>> safer, etc., then a new thread in comp.lang.c++ would be more
>>> appropriate.)
>>>
>>
>> Here is what I was able to write, using one of my earliest scripting
>> languages (early 90s):
>>
>> v1 := (2, 3, 4)
>> v2 := (5, 4, 3)
>>
>> println v1 * v2 # vector product
>> println v1 iand v2 # dot product
>> println v1 * 5.1 # scalar product
>> println (v1 + v2) / 2
>>
>> (I can't remember why 'iand' - bitwise and - was used for a dot-
>> product.) Output is:
>>
>> (-7.00, 14.00, -7.00)
>> 34.000000
>> (10.20, 15.30, 20.40)
>> (3.50, 3.50, 3.50)
>>
>> Some more:
>>
>> v := (100, 0)
>> m := rotatematrix(15°, "Z")
>> println v * (m * m) # rotate 30 deg ac/w
>>
>> (Shows (86.60, 50.00, 0.00).)
>>
>> I found it was simplest just to build-in whatever is needed (this
>> language was used with my 3D graphics app). No imports or libraries
>> are needed, and every user had the same abilities out-of-the-box.
>>
>>
>
> When you have a domain-specific language, and full control of the
> language, then built-ins can be easier. The point of techniques like
> this, whether in Python or C++, is that the language and toolchain only
> needs to support the technique. Then the individual users or library
> vendors can use it for any operations and any types. If someone wants
> to make a type that supports 8 x 8 matrices over complex doubles with a
> binary exponential operator, and write "matrix A = B *pow* C;", then
> they can do that with pretty much identical code as this. Well, they'd
> have to write code for the exponential operator in question, and good
> luck doing that efficiently manner while keeping numeric stability. But
> the wrapping code to support the syntax is straightforward.
>
> Some other languages, like Haskell, make it easier to write your own
> operators and also let you use symbols for them. C++ lets you write
> "rotatematrix(15_deg, "Z")", but not "15°". (You'd have to find a
> Unicode letter that looks like a degree symbol.)
>
It's all "algebraizations" of "arithmetizations" eventually
"geometrizations", or vice-versa, then for example as with regards
"lambda-calculus" and "pi-calculus", the "expressitivity".
"Latin1" then its subset "ASCII" is usually considered "the limits
of the letters of the source language": we have keyboards for that.
Of course Unicode is a great account and many languages have
Unicode as allowed in identifiers.
It's like Madonna says, "express yourself".
("Mouseketeers", ..., it's a small world.)
"Give him something he can feel."
Language: it's got expressitivity in it.
I don't know anywhere where digits begin identifiers.
(... In C-like languages.)
The expressitivity and interpretability then have
that "language standards" define "interpretability",
then that's called "defined".
A "superset of C" like "OpenMP", or for example
"co_await" and "co_return" after being "a superset of C++",
it's just a wider language, where the narrower yet
remains defined.
Natural "arithmetizations" still make for what "operator overloading"
gives: just need to have a model of objects that behave their
relations just like regular integers, and the values of those
objects are integers (and so is their type).
Technical Details
Message-ID:
<M_-dnVCKCumJa5P2nZ2dnZfqnPidnZ2d@giganews.com>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!weretis.net!feeder9.news.weretis.net!border-3.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-4.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mailReferences:
<11bes8b$1gncr$1@dont-email.me> <11ek7b2$j8f6$1@raubtier-asyl.eternal-september.org> <11ekfu0$mq5n$4@dont-email.me> <11ekg71$n5p7$1@raubtier-asyl.eternal-september.org> <11ekgf8$mq5n$8@dont-email.me> <11ekjsp$pmch$2@raubtier-asyl.eternal-september.org> <11ekq4q$snda$1@dont-email.me> <11em45u$1fp0a$1@dont-email.me> <11emdhn$1ko8h$1@dont-email.me> <11emjh1$1ntap$1@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!weretis.net!feeder9.news.weretis.net!border-3.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-4.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Tue, 01 Dec 2026 16:30:12 +0000 Subject: Re: variadic min and max, (any some and none, cat and more) Newsgroups: comp.lang.c References: <11bes8b$1gncr$1@dont-email.me> <11ek7b2$j8f6$1@raubtier-asyl.eternal-september.org> <11ekfu0$mq5n$4@dont-email.me> <11ekg71$n5p7$1@raubtier-asyl.eternal-september.org> <11ekgf8$mq5n$8@dont-email.me> <11ekjsp$pmch$2@raubtier-asyl.eternal-september.org> <11ekq4q$snda$1@dont-email.me> <11em45u$1fp0a$1@dont-email.me> <11emdhn$1ko8h$1@dont-email.me> <11emjh1$1ntap$1@dont-email.me> From: Ross Finlayson <ross.a.finlayson@gmail.com> Date: Tue, 1 Dec 2026 08:30:10 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 MIME-Version: 1.0 In-Reply-To: <11emjh1$1ntap$1@dont-email.me> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Message-ID: <M_-dnVCKCumJa5P2nZ2dnZfqnPidnZ2d@giganews.com> Lines: 204 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-AOfNMm3xIh1j0GPxiVLNHB5N0n/SGC2uZtTzjcA/iYK7v/e2fE6WensdtY6ZkYCljYzYnMVIalQ9z1l!3KvfkKj+71Wc8xR/vnEaspvrGVJH3dtXB+9ns04MAR8om19RmAu+LTQNf0RoiHUEkFuW5kaJSpZO X-Complaints-To: abuse@giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40