JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.55k stars 5.47k forks source link

documentation regarding function chaining and function composition #27879

Open drbenvincent opened 6 years ago

drbenvincent commented 6 years ago

Hi. Beginner here. I'd like to propose updates to the documentation of function chaining (with the |> operator) and composition (\cdot) operator. I've done some homework to see if I was missing something, and some folk from the Discourse forum thread here suggested I post an issue.

I propose that: a) these aspects of the language need to be highlighted in the docs, with examples, and b) be more discoverable by searching the docs.

A) Some people might not think these features are a big deal, but as as someone coming from Matlab, but who has eyed up these features in R and Python with envy, I think they deserve to be shouted about. I'm not into language war type stuff, but if someone asks about Julia, "do you even pipe, bro?" then everyone needs to know that you can.

At the moment the docs are pretty minimal (ie https://docs.julialang.org/en/latest/base/base/#Base.:|>, and https://docs.julialang.org/en/latest/base/base/#Base.:∘) Personally, I think they deserve a new section in the functions section of the docs https://docs.julialang.org/en/latest/manual/functions/.

B) I first came across the pipe operator when I saw it featured in the excellent Intro to the Queryverse YouTube video, and I was, woah! I didn't know about that. I searched the docs for pipe, piping, function piping, function chaining, etc and I didn't get relevant hits. So perhaps there's a way to funnel people into both the function chaining and function composition parts of the docs?

StefanKarpinski commented 6 years ago

Would be good to mention the .|> “elementwise pipe operator” at the same time as a non-obvious but very useful combination of |> and operator dotting.

pdeffebach commented 6 years ago

Should we maybe add a section in Control Flow for this?

Additionally, any mention of |> should include references to "pipe", "piping", and "chaining" for easy search-ability.

StefanKarpinski commented 6 years ago

Should we maybe add a section in Control Flow for this?

It's not control flow though... but it should be mentioned somewhere.

andreasdominik commented 5 years ago

Hello - also a rookie here... The thread is not fresh, but maybe someone reads and answers.

I think the docu is sufficient now, but I stumbled across something I don't understand: First idea before finding the |> function in a language with functional flavour is just to try →(x,f) = f(x)

actually, it almost does what I expect: julia> 5 → sqrt 2.23606797749979

but: julia> 5 → sqrt → abs2 ERROR: MethodError: no method matching abs2(::typeof(sqrt)) Closest candidates are: abs2(::Missing) at missing.jl:79 abs2(::Bool) at bool.jl:92 abs2(::Real) at number.jl:157 ... Stacktrace:

[2] top-level scope at none:0

In contrast both works with Base.|>

When I look up the definition of the built-in operator, I find in Base operators: 813: |>(x, f) = f(x)

exactly the same definition!

Can somebody explain this to me? I am sure, this is not a bug - but something I'd like to understand! Thank you very much!

drbenvincent commented 5 years ago

I think the docu is sufficient now,

Agree, the docs at https://docs.julialang.org/en/latest/manual/functions/#Function-composition-and-piping are quite nice

yurivish commented 5 years ago

@andreasdominik This has to do with operator associativity: a |> b |> c is parsed as (a |> b) |> c, while a → b → c is parsed as a → (b → c).

You can see more information here.

The operator is used in one of the examples, where Base.operator_associativity(:→) reports :right. I can't say why right-associativity was chosen for that operator; that may be a good question for the Julia forums.

andreasdominik commented 5 years ago

stupid me - it's obvious! Thank you very much for the elucidation!