# What do you think of \`(f1 & f2)(x)\`?

**URL:** <https://discourse.julialang.org/t/what-do-you-think-of-f1-f2-x/49459>\
**Category:** General Usage\
**Created:** [November 2, 2020, 2:43pm UTC](https://discourse.julialang.org/t/what-do-you-think-of-f1-f2-x/49459 "2020-11-02T14:43:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MarcMush](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcmush/32/18006_2.png) [@MarcMush](https://discourse.julialang.org/u/MarcMush)\
**Post date:** [November 2, 2020, 2:43pm UTC](https://discourse.julialang.org/t/what-do-you-think-of-f1-f2-x/49459/1 "2020-11-02T14:43:21Z")

</div>

There might be a thread or a github issue about that but it’s really hard to search for `or` and `and` terms, please redirect me if I missed it

I’d like to use a syntax that looks like that:

```julia
f = <(5) & >(1)
g = isnan | ismissing

```

I can pirate them but obviously I’d rather not:

```julia
import Base: &, |
(&)(f1::Function, f2::Function) = x -> f1(x) & f2(x)
(|)(f1::Function, f2::Function) = x -> f1(x) | f2(x)

```

I looked in [here](https://github.com/JuliaLang/julia/blob/master/src/julia-parser.scm) for alternative characters but none looks good to me

(I always mix `∧` and `∨` so I don’t want to use them)

was there already a discussion about this? Could it be added in Base Julia or is there a reason why it’s not defined?

edit: changed `&&` to `&` and `||` to `|` (doesn’t matter in my case)

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [November 2, 2020, 3:23pm UTC](https://discourse.julialang.org/t/what-do-you-think-of-f1-f2-x/49459/2 "2020-11-02T15:23:43Z")

</div>

It’s not a bad idea, but one reason _not_ to do this in Julia base is that it uses the `&` operator which normally means _bitwise_ AND to mean _logical_ AND. Or, in other words, if you wanted:

```julia
(<some_operator>)(f1, f2) = x -> f1(x) & f2(x)

```

what operator would you use? The `&` operator seems like the logical and obvious choice, but your proposal would use that to mean something different.

On the other hand, in your own code you could use `and` and `or` as function names without needing to pirate anything. Alas, you can’t use them as infix operators, but that may be ok if it makes your code a bit more obvious and free of type piracy.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [November 2, 2020, 3:55pm UTC](https://discourse.julialang.org/t/what-do-you-think-of-f1-f2-x/49459/3 "2020-11-02T15:55:27Z")

</div>

In the past when I’ve “needed” this (you never really _need_ this though, do you…) I implemented \vee and \wedge like the Base implementation of `∘`

```julia
"""
    f ∧ g ∧ ...

Create an anonymous function that evaluates to `f(x) && g(x) && ...`
"""
function ∧ end
∧(f) = f
∧(f, g) = (x...) -> f(x...) && g(x...)
∧(f, g, h...) = ∧((f ∧ g), h...)

function ∨ end
∨(f) = f
∨(f, g) = (x...) -> f(x...) || g(x...)
∨(f, g, h...) = ∨((f ∨ g), h...)

```

```julia
julia> map(isascii ∧ isuppercase ∨ isdigit, ('A', '1', 'Σ'))
(true, true, false)

```

Note that evaluation is “kind-of-short-circuiting”; you can’t replicate `&&` with a function, since it’s control flow, but the evaluation of the function chain will exit early if it hits a `false` (or `true`, depending). Also, all arguments to `∨` and `∧` are evaluated when the anonymous function is created (like with `ifelse`).  
As an exercise a while back I also wrote a macro version of this to overcome the mentioned limitations, but I still haven’t been able to think of a single case where using it would be worthwhile…

```julia
macro and(args...)
    ex = Expr(:&&, first(args))
    ex′ = ex
    for i in 2:length(args)
        push!(ex′.args, Expr(:&&, args[i]))
        ex′ = last(ex′.args)
    end
    esc(ex)
end

```

edit: sorry, I didn’t find the right version of the macro, so the above doesn’t do what I said it does. Oh well…
