Julia - How to pass kwargs from a function to a macro - function

You can define a function to pass its keyword arguments to inner functions like this:
function example(data;xcol,ycol,kwargs...)
DoSomething(; spec=:EX, x=xcol, y=ycol, kwargs...)
end
Now, the function DoSomething accepts many arguments, such as color. This works for functions, but I'd like to do this with a macro from VegaLite.jl:
function example(data;xcol,ycol,kwargs...)
#vlplot(data=data,mark=:point, x=xcol, y=ycol,kwargs...)
end
example(df,xcol=:Miles_per_Gallon, ycol=:Horsepower, color=:Origin)
Note that the code above does not work.

So the answer here is... it's tricky. And in fact, in general, this isn't possible unless the macro itself supports it.
See, macros do their transformations at parse time — and often will exploit what you've actually written to mean something different and special. For example, #vlplot will specially handle and support JSON-like {} syntaxes. These aren't valid Julia code and can't be passed to a function you define (like example)!
Now, it's tempting to see this and think, ok, let's make that outer example thing into a macro, too! But it's not that easy. I'm not sure it's possible to have a general answer that will always pass the arguments appropriately and get the hygiene correct. I'm pretty sure you need to know something about how the macro you're calling handles its arguments.

you need to add ; before kwargs to signal they are kwargs not positional arguments e.g.:
DoSomething(;spec=:EX, x=xcol, y=ycol, kwargs...)
(this is the answer for DoSomething being a function as this was the original formulation of the question)

Related

So I've been Googling function arguments and I would like to understand arguments better and the use of ()

So I've been Googling function arguments and I would like to understand arguments better.
I am new to as3, to summarize arguments with my current knowledge, I would say they are like temporary variables? I don't fully get why you add parameters which are names that can be any value? Then you like call these parameters later and their order magically replace these parameters, but why? I'm missing some understanding here to fully grasp their use. Why make parameters in a function and then add the values later? If I'm even saying that right.
function name( applepie, sugar, healthyfood)
name( 1,2,3)
What was the point?
Also I haven't found a syntax book that describes what every symbol does yet that I can just search like () and it describes it, I heard some just use Google, but the results I got weren't very fruitful. Hence why I'm here asking. Personally I don't want to continue on until I fully grasps the use of (). I also tried Adobe website search but that didn't work out well either, was a good amount of searches trust me....
A function is a piece of code that can be reused many times in different contexts. You pass arguments to a function to tell the function something about the context in which it is being called; as a trivial example, when you call the print() function you must specify what you want the function to print. In your example name(applepie, sugar, healthyfood) the function should use the value supplied in place of each argument somewhere in its body, because the function doesn't know what values it will be passed, in the body of the function definition you use the names you chose (which should be descriptive) to refer to the values which will be passed in later and which will presumably be different each time it is called.
The parentheses are used for delimiting different semantic elements, in this case they are telling the interpreter where the argument list starts and stops.

Is there a specific name for a function that takes its output as input and does that parameter have a name?

I work with a BASIC programming language and have found it useful to write functions that rely on their output as a parameter. Such as
inOut = someFunction(inOut)
I'd like to call this a recursive function. but it doesn't seem right because it is not calling itself. Can someone tell me what the name of this type of function is and if the parameter/return has a special name?
Thanks!!
This is an ordinary function as any other. The thing you show is called reassingment. You can rename inOut on the left with newinOut and it will not change anything... there is absolutely nothing special about the function, it's a naming pattern, that's all.
In many languages (including VB, but not sure about classic BASIC) there's something called passing parameter by reference. It's not exactly what you posted, but rather simple
someFunction(inOut)
parameter is passed into the function, changed there and the change persists outside the function

Fortran: how to pass function name in a common block

In Fortran, is it possible to put a function in a common block as in:
COMMON /myblock/ func
(where x is some variable and func is a function).
My problem is that I would like to create a function s(x) that calls an external function func(x) but without passing func in s(x). For my project, s(x) has to be a function of only one variable, i.e., I do not want to do:
function s(x,func)
s=func(x)
Instead, I am hoping I could do:
function s(x)
common /myblock/ func
s=func(x)
Or, if someone has some other suggestion using modules or something, this will be great.
Thanks in advance for any help.
o.
and then have the same common (myblock) in the subroutine that calls s(x).
I don't believe that this is possible in any portable way. Some implementations may allow you to use some tricks to do it.
The modern way to do this is with a pointer to a function. The pointer could be passed as an argument or, for the design of this question, placed into a module. See, for example, Function pointer arrays in Fortran
I think you are not supposed to use common blocks for this, but modules. Put your function func in a module called myfunctions and then when needed insert at use myfunctions statement and thats it.
Modern fortran standards prohibit this. From 5.5.2 of Fortran 2003:
A common-block-object shall not be ... a function name, an entry name...
And at any rate, using global variables to pass around non-constant data is just a terrible, terrible idea. As ja72 points out, you could do this with modules, but I refuse to demonstrate it with sample code.

Can if statements be implemented as function calls?

One of the stylistic 'conventions' I find slightly irritating in published code, is the use of:
if(condition) {
instead of (my preference):
if (condition) {
A slight difference, and probably an unimportant one, but it occurred to me that the first style might be justified if 'if' statements were implemented as a kind of function call. Then I could stop objecting to it.
Does anyone know of a programming language where an if statement is implemented as a function call, where the argument is a single boolean expression?
EDIT: I realise the blocks following the if() are problematic, and the way I expressed my question was probably too naive, but I'm encouraged by the answers so far.
tcl is one language which implements if as a regular in built function/command which takes two parameters ; condition and the code block to execute
if {$vbl == 1} { puts "vbl is one" }
http://tmml.sourceforge.net/doc/tcl/if.html
In fact, all language constructs in tcl (for loop , while loop etc.) are implemented as commands/functions.
It's impossible for it to have a single argument since it has to decide which code path to follow, which would have to be done outside of said function. It would need at least two arguments, but three would allow an "else" condition.
Lisp's if has exactly the same syntax as any other macro in the language (it's not quite exactly a function, but the difference is minimal): (if cond then else)
Both the 'then' and 'else' clauses are left unevaluated unless the condition selects them.
In Smalltalk, an if statement is kind of a function call -- sort of, in (of course) a completely object oriented way, so it's really a method not a free function. I'm not sure how it would affect your thinking on syntax though, since the syntax is completely different, looking like:
someBoolean
ifTrue: [ do_something ]
ifFalse: [ do_something_else ]
Given that this doesn't contain any parentheses at all, you can probably interpret it as proving whatever you wanted to believe. :-)
If the if function is to be a regular function, then it can't just take the condition, it needs as its parameters the block of code to run depending on whether the condition evaluates to true or not.
A prototype for a function like that in C++ might be something along the lines of
void custom_if(bool cond, void (*block)());
This function can either call the block function, or not, depending on cond.
In some functional languages things are much easier. In Haskell, a simple function like:
if' True a _ = a
if' _ _ b = b
allows you to write code like this:
if' (1 == 1)
(putStrLn "Here")
(putStrLn "There")
which will always print Here.
I don't know of any languages where if(condition) is implemented as a regular function call, but Perl implements try { } catch { } etc.. {} as function calls.

What is the name of a function whose result depends only on its parameters?

I'm writing a toy compiler thingy which can optimise function calls if the result depends only on the values of the arguments. So functions like xor and concatenate depend only on their inputs, calling them with the same input always gives the same output. But functions like time and rand depend on "hidden" program state, and calling them with the same input may give different output. I'm just trying to figure out what the adjective is that distinguishes these two types of function, like "isomorphic" or "re-entrant" or something. Can someone tell me the word I'm looking for?
The term you are looking for is Pure
http://en.wikipedia.org/wiki/Pure_function
I think it's called Pure Function:
In computer programming, a function may be described as pure if both these statements about the function hold:
The function always evaluates the same result value given the same argument value(s). The function result value cannot depend on any hidden information or state that may change as program execution proceeds or between different executions of the program, nor can it depend on any external input from I/O devices.
Evaluation of the result does not cause any semantically observable side effect or output, such as mutation of mutable objects or output to I/O devices.
The result value need not depend on all (or any) of the argument values. However, it must depend on nothing other than the argument values.
I guess you could say the adjective is "pure" if you go by "pure function".
I always learnt that a function whose output is always the same when the arguments are always the same is called "deterministic". Personally, I feel that that is a more descriptive term. I guess a "pure function" is by definition deterministic, and it seems a pure function is also required to not have any side-effects. I assume that that need not be the case for all deterministic functions (as long as the return value is always the same for the same arguments).
Wikipedia link: http://en.wikipedia.org/wiki/Deterministic_algorithm
Quote:
Given a particular input, it will always produce the same output, and the underlying machine will always pass through the same sequence of states.