Distributed Or , julia logicals operators

I asked a question on https://julialang.org/slack/.

Question was:

  • Is there any way to write something like x == (4 | 5) witch return true if x == 4 or if x == 5?

The regular answer is:

  • x in (4,5) or any other way to write this ex: x ∈ (4,5), x ∈ (4 ∪ 5)

Although if you wanna still use the operator |, @Mason propose this solution:

struct DistributedOr{L, R}
    l::L
    r::R
end

Base.:(==)(x, o::DistributedOr) = x == o.l || x == o.r

test:

let x = 4, | = DistributedOr
    x == (4 | 5)
end

true

@Sukera (I think) suggested another implementation:

struct OR{T}
        els::T
        OR(args...) = new{typeof(args)}(args)
end

Base.:(==)(a,b::OR) = any((a,) .== b.els)

test:

let x = 7, | = OR
    x == (5 | 6 | 7 | 8)
end

true

1 Like