Operator distributivity in expressions – syntactic sugar

When writing boolean expressions out, you often have to use something of the type:

A == any of B1, ..., Bn
C < all of D1, ..., Dm

But in code you have to use expanded forms:

A == B1 or ... or A == Bn
C < D1 and ... and C < Dm

That looks, to me, like any of and all of are simply the distributed form of or and and. That made me wonder if you can apply distributivity to operators in some way that compilers/interpretors can understand. For the above example, say, something like this:

A == B1 +_or ... +_or Bn
C < D1 +_and ... +_and Dm

Essentially, taking operators as relations, it should work something like this:

R1(A, distributive_R2(B1, ... , Bn)) becomes
R2(R1(A, B1), ... , R1(A, Bn))

This could apply to more than just booleans:

X / Y1 +_+ ... +_+ Yn becomes
X/Y1 + ... + X/Yn

Of course, my +_ prefix may not be the ideal syntax; I’m sure there’s better ones out there. Nevertheless, this strikes me as being possibly quite useful and understandable. Any thoughts?

1

1. I don’t like the syntax

A == B1 or ... or A == Bn is not obvious. It is for simple forms, but those simple forms are rare. For example, there is no need to write:

A == 1 or ... or A == 10

when you can simply write:

A >= 1 and A <= 10

If you enumerate through the sequence, the syntax won’t help neither:

Set = [ 4, 7, 2, 3, 0, 1, 1 ]
A == Set[0] or ... or A == Set[Set.Length]

is too verbose.

Also, what would:

A == 1 or ... and A == 10

mean? If this is illegal, how an IDE would prevent it? Why forcing the developer to type the same word twice?

Readability should be your concern too. C < D1 +_and ... +_and Dm in your question is already difficult to understand. I can’t imagine a piece of code which combines several ranges.

2. in and ranges in SQL and Ada

The easier syntax would be the one which is already used in SQL for keywords and in Ada and Matlab for ranges:

A in 1..10

or:

A in Set

Some SQL variants also use between:

A between 1 and 10

I largely prefer Ada syntax 1..10, which is extremely explicit and shorter to write. The and keyword makes the reader think about a boolean operation, not a range. Without knowing the syntax, some developers will also be tempted to write either:

A between 1, 10

or:

A between(1, 10)

The SQL/Ada syntax is nice when it comes to searching a value in a set or a range, but not when it comes to verifying that an expression is true for any value of a sequence. I don’t know if those languages have constructs allowing that.

3. C# world, as a side note

Note that in C#, which uses some paradigms from functional programming and has lambda expressions, your suggested syntax would be written this way:

Searching for a value in a sequence:

var isInList = sequence.Contains(value);

or:

var isInList = sequence.Any(a => a == value);

Verifying that en expression is valid for any element of a sequence:

var trueForAll = sequence.All(a => a > 0);

Filtering:

var positiveOnly = sequence.Where(a => a > 0);

This is not the best syntax ever, and I largely prefer the one which is inspired by SQL/Ada, but it gives you some ideas about how your problem is solved in different languages.

You can use this syntax to create your own, with specific language keywords. Something like:

A in Set              # Verifies that Set contains A.
B in 1..10            # Verifies that 1 ≤ B ≤ 10.
C > 0 for any Set     # Verifies that all elements of Set are positive.
D <> 0 for any 1..10  # Verifies that none of the values between 1 and 10 are equal to zero.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật