Julia equivalent to "not" in python - julia

I am new to Julia. I cannot find how to use a "not" operator as in python.
What I want to do: run a while loop as long as a function returns false.
In python I would do:
while not function(foo, bar):
do_something
How would you achieve this effect in Julia?

It's the standard !:
while !func(foo, bar)
do_something
end

Related

pyarrow compute expression in reticulate

How do I use a filter in a reticulate pyarrow compute expression.
At base I have a pyarrow dataset (in this case called woodcan) that I want to turn into a table with a filter.
tab <- woodcan$to_table(ds$field('Region')=='Canada')
The above gets Error in py_compare_impl(a, b, op) : ValueError: An Expression cannot be evaluated to python True or False. If you are using the 'and', 'or' or 'not' operators, use '&', '|' or '~' instead.
How is that syntax supposed to look?
You could generate the expression running python code with py_run_string or py_run_file and pass it to filter argument of to_table:
library(reticulate)
run.py <- py_run_string('
import pyarrow.dataset as ds
expr = ds.field("Region") == "Canada"
')
woodcan$to_table(filter=run.py$expr)
Above code needs previous installation of py_arrow in conjuction with reticulate:
virtualenv_create("arrow-env")
arrow::install_pyarrow("arrow-env")
use_virtualenv("arrow-env")
With #Waldi's answer I came up with another way that doesn't rely on python strings.
From that answer we generate run.py$expr which is a <pyarrow.compute.Expression (Region == "Canada")> object.
From there it begs the question, how else can we generate that? Since it's a pyarrow.compute.Expression that tells us we need, of course, pyarrow.compute
so then it's just replacing "==" with equal
pc <- import('pyarrow.compute')
filter=pc$equal(pc$field("Region"), pc$scalar("Canada"))
and finally
woodcan$to_table(filter=filter)

plotting a python function that uses an array

In sagemath, I would like to plot the following function foo (Coef is an array that is big enough) :
def foo(x):
x_approx = floor (x*4)
return Coef[x_approx]
I wanted to use the command plot(foo(x), (x,0,0.1)).
But I got the error unable to convert floor(4*x) to an integer.
Whereas when `foo is not using an array, it works:
def foo(x):
x_approx = floor (x*4)
return 4*x_approx
Use plot(foo, (x, 0, 0.1)) instead (that is, replace foo(x) with foo). If you use foo(x), then Sage tries to evaluate foo(x) first, in which case it treats x as a symbolic variable and can't turn it into a number to plot. If you use foo, then it knows to treat it as a plottable/callable function, and it does the right thing.
Edit: I think the issue is that for plotting, Sage requires a certain type of function, a symbolic function, and using a Python construct like Coef[...] doesn't fit into that framework.

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.

how to print the passed argument variable in R

I want to print every argument passed to function read.table. My idea was to write some decorator that is easy in Python. But for R, I don't know how to do it, what I learned was to use trace(). However, I don't know how to print variables inside trace.
Example:
trace(f)
a <- "123"
f(a)
untrace(f)
trace() will only output f(a), but I want to know the evaluation of a.
thanks for your guys' help, I find the answer.
Simply use the following code:
trace(f, tracer = quote(print(lapply(as.list(match.call()),eval))))
d<-1
f(d)
untrace(f)

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)

Resources