Check if a function takes specified arguments in Rust - function

Is there a way in a where clause in Rust to check if a function passed as a parameter takes specified arguments?

If you're defining a function that takes a function argument, that argument has a very specific type associated with it that dictates the arguments. You cannot call that function with something that doesn't match, it just won't compile.
If you're thinking in terms of dynamic languages where the arguments are somewhat subjective, you're assuming you can make a mistake here and call it incorrectly. You can't. It's strictly disallowed.

Related

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

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)

Functional parameters in Go functions

Is there a way to pass a function(which can be generic) to another function?
I know that with known input types and return types we can pass a function but a generic approach is needed
When reading the Go2 proposal on generic: "Type Parameters - Draft Design", I am not sure you would be able to pass as parameter a generic function without explicitly specify the type used by said function
See "Instantiating a function"
Go normally permits you to refer to a function without passing any arguments, producing a value of function type.
You may not do this with a function that has type parameters; all type arguments must be known at compile time.
That said, you can instantiate the function, by passing type arguments, but you don't have to call the instantiation. This will produce a function value with no type parameters.
// PrintInts is type func([]int).
var PrintInts = Print[int]

What is a function prototype in Rust?

I wanted to understand the behaviour of the #[inline] attribute in Rust, so I was reading through the Attributes section of The Rust Reference. It was very helpful, but I found this part of the description confusing (emphasis mine):
The inline attribute suggests to the compiler that it should place a copy of the attributed function in the caller, rather than generating code to call the function where it is defined.
This attribute can be used on functions and function prototypes, although it does not do anything on function prototypes.
This caveat is repeated for the #[cold] attribute.
I've never heard the term "function prototype" used with respect to Rust. I know that such a concept exists in JavaScript, but JavaScript's and Rust's object and type systems are very different! What does it mean here?
Searching further, I found two mentions of function prototypes in the Error Index:
E0034
The compiler doesn't know what method to call because more than one method has the same prototype.
E0580
The main function was incorrectly declared. The main function prototype should never take arguments.
In this case, "function prototype" seems to mean something like "function signature" -- the names, arguments, and types that make up a function's external interface. This also appears to be what it means in the context of C/C++. However, that doesn't seem to match the usage above; every function definition starts with the function's signature, so it wouldn't make sense to say that putting the attribute on the signature does nothing, because that's what you're doing when you're putting the attribute on a function.
What does the term "function prototype" mean in the context of Rust?
However, that doesn't seem to match the usage above; every function definition starts with the function's signature, so it wouldn't make sense to say that putting the attribute on the signature does nothing, because that's what you're doing when you're putting the attribute on a function.
Yes, every function starts with a signature, but not every signature is part of a function definition. That is, it is possible to have a signature, but no body (in a trait for example) and that's what is meant by "prototype" in the documentation you cited. Something like this:
trait Foo {
#[inline] // This annotation does nothing
fn foo();
}

Clojure Function check

When i have 3 functions in a program, how do i check a specific function name ?
I want to know the name of those function for the sake of function selection.
Let say linear-kernel function, logistic-kernel function, and non-negative function, when i call the program, one of those function is called and i should to check whether it was linear, logistic or non-negative function, so i can execute another function related with the selected function.
I think doing function selection will save my time from repeating the base code. But doing function selection maybe is not the best design that i could use in Clojure.
FYI, at this level, i already use the "meta" keyword to access the function name, but when i create
(defn isKernel [krn]
(if (= (str (:name (meta #'krn))) "logistic-kernel") 1 0))
The compiler cannot resolve the 'krn' var
In Clojure functions are values, just like the number 4. This is a big part of the underpinnings of much of the language (and functional programming in general). Most of the time we store functions in vars though this is not required. functions as values don't have names* So rather than checking to see if the name of a passed function matches a known name, it makes more sense to ask "does this function have the same value as the function stored in the var" as #cgrand points out this can be accomplished simply by calling =.
If you are doing this kind of dispatch there is a good change that protocols are a better tool than rolling your own
*they do have names for the purpose of creating recursive function literals though thats not what I'm getting at here.

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.