Why do most programming languages only have binary equality comparison operators?

前端 未结 24 1011
一个人的身影
一个人的身影 2020-12-08 14:20

In natural languages, we would say \"some color is a primary color if the color is red, blue, or yellow.\"

In every programming language I\'ve seen, that translates

24条回答
  •  盖世英雄少女心
    2020-12-08 14:43

    This can be replicated in Lua with some metatable magic :D

    local function operator(func)
        return setmetatable({},
            {__sub = function(a, _)
                return setmetatable({a},
                    {__sub = function(self, b)
                        return f(self[1], b)
                    end}
                )
            end}
        )
    end
    
    
    local smartOr = operator(function(a, b)
        for i = 1, #b do
            if a == b[i] then
                return true
            end
        end
        return false
    end)
    
    
    local isPrimaryColor = someColor -smartOr- {"Red", "Blue", "Either"}
    

    Note: You can change the name of -smartOr- to something like -isEither- to make it even MORE readable.

提交回复
热议问题