What does the syntax "|>" do in Julia? - julia

I am looking at someone's code that has "|>" in it. What does that syntax do in Julia and can someone link the Julia Docs for it?

Commonly denoted as "pipe syntax", this syntax:
Applies a function to the preceding argument. This allows for easy
function chaining.
An example from the official docs:
julia> [1:5;] |> x->x.^2 |> sum |> inv
0.01818181818181818
And the official Julia docs page where you can find the |> operator is here.

Related

Functions as infix operators without pipe?

Reading a book on Julia I found the following code example:
The author creates a type
type Student
name::String
end
creates an instance of this type
antony = Student("Antony Miller")
and then tests the type of antony in two different ways
isa(antony, Student)
true
antony isa Student
true
This is an awesome way of using syntax to make code readable. However, how is the function isa() enabled to be used as an infix operator, here? I would have guessed that the following is possible...
antony |> isa(Student)
true
...as the pipe (|>) uses the object to the left as the first argument in the function to the right. But why is it possible to omit the pipe in the example further up? And can I use the same behavior in my own functions as well to make the code more readable (I would really like to)?
As explained in this answer: User-defined infix operator, Julia has a fixed set of infix operators. You can overload the the operators, but you are not allowed to define new infix operators.
isa is in this predefined set of infix operators.
You can, however, simulate infix operators with macros (also pointed out in the linked thread). You can see an example in the DiffEq docs.
It's possible to make the piping semantics you asked for work. Suppose we have some function of two arguments
rel_diff(x, y) = (x - y)/(x + y)
We can define
rel_diff(y) = x -> rel_diff(x, y)
so that
julia> 1 |> rel_diff(2)
-0.3333333333333333
I don't think this is very aesthetically pleasing, but you might.
Another alternative would be this trick:
struct Infixed{X, F <: Function}
x::X
f::F
end
(|)(args...) = Base.:(|)(args...)
(|)(x, f::Function) = Infixed(x, f)
(|)(xf::Infixed, y) = xf.f(xf.x, y)
and now we can do
julia> 1 |rel_diff| 2
-0.3333333333333333
Note that this relies on shadowing the base definition of | so that we don't commit type piracy. This won't work in the global scope REPL if you've already used |, but it'll work if you make a new local scope like with let or inside a function body.

Elixir #spec annotation for atoms

The function below is written in Elixir:
def count(:comments, id), do 0
When I want to get its documentation using 'h' in iex, this is printed out:
iex> h Module.count
def count(atom, id)
But I want ':comments' to appear on the documentation using 'h'.
iex> h Module.count
def count(:comments, id)
How can I do that?
Thanks in advance!
You want to declare the function with a bodyless clause. See the Elixir docs on documenting function arguments, or the Writing Documentation doc as a whole.
EDIT:
Looking at it further, while it's not exactly what you're looking for adding
#doc"""
count(:comments, id)
"""
before your function definition will get you closer to what you're looking for.

How to display the definition of a function

This is probably a newbie question... but is it possible to show the definition of a (user defined) function? While debugging/optimizing it is convenient to quickly see how a certain function was programmed.
Thanks in advance.
You can use the #edit macro, which is supposed to take you to the definition of a method, similarly to how the #which macro which shows the file and line # where that particular method was defined, for example:
julia> #which push!(CDFBuf(),"foo")
push!{T<:CDF.CDFBuf}(buff::T, x) at /d/base/DA/DA.jl:105
julia> #which search("foobar","foo")
search(s::AbstractString, t::AbstractString) at strings/search.jl:146
Note that methods that are part of Julia will show a path relative to the julia source directory "base".
While this is not an automatic feature available with Julia in general (as pointed out by Stefan), if you add docstrings when you define your initial function, you can always use the help?> prompt to query this docstring. For example
julia> """mytestfunction(a::Int, b)""" function mytestfunction(a::Int, b)
return true
This attaches the docstring "mytestfunction(a::Int, b)" to the function mytestfunction(a::Int, b). Once this is defined, you can then use the Julia help prompt (by typing ? at the REPL), to query this documentation.
help?> mytestfunction
mytestfunction(a::Int, b)

Equivalent of Python's 'with' in Julia?

Does Julia have an equivalent of Python's with? Maybe as a macro? This is very useful, for example, to automatically close opened files.
Use a do block. Docs on do blocks are here.
And here is an example of how to do the usual with open(filename) as my_file of Python in Julia:
open("sherlock-holmes.txt") do filehandle
for line in eachline(filehandle)
println(line)
end
end
The above example is from the Julia wikibooks too.
Although the do block syntax does have certain similarities to Python's with statement, there is no exact equivalent. This is discussed in further detail in the GitHub issue "with for deterministic destruction". The issue concludes that this structure should be added to Julia, although no syntax or plan for such is established.

Finding ? operator in AST

I'm doing some exercise in Rascal. When I try to determine the Cyclomatic complexity of a Java method getting methods from an AST. I would like to evaluate the ? operator.
As it is not determined by '/if(_, _, )', I tried to determine it using postfix(, _); (infix works fine finding || or &&)
Still no success.
Anybody who can unhide this secret to me?
Thanks in Advance
Use the force; the source for the AST definition is here https://github.com/cwi-swat/rascal/blob/master/src/org/rascalmpl/library/lang/java/m3/AST.rsc, also mentioning the conditional constructor Mark pointed out in his comment.
If you use iprintln on the AST for a small example, like iprintln(ast) or import util::ValueUI; and then text(ast) for an editor with the formatted AST it's easy to find out what ASTs look like.

Resources