Clojure Function check - function

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.

Related

Get all the functions (factory function included) in the current scope of lua

I am seeking a way to acquire all the functions which have been defined in the current scope from lua. Is there a quick way to implement it directly from lua, not from C? The factory functions are preferred to be included.
You can use a hybrid approach:
(1) to get all local variables, you can use debug.getlocal and get the names and values of the variables (see for example the logic in this answer). All function values will have type of the value equal to function (type(value) == 'function'), so you can easily filter based on that condition. The name of the variable will give you the name you are looking for (keep in mind that several names may refer to the same function).
(2) to get all global variables you can iterate over fields in _ENV or _G tables and apply the same filtering logic as in 1.
Note that neither of these methods gives you access to functions stored in table fields or available indirectly through metamethods.

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 term for a function that modifies its arguments?

I'm not really classically educated in CS or mathematics. I'm just thinking there should be a term for this in pass-by-reference laungauges such as php. IE. functions that return a result (like how you'd want most functions to work) versus functions that modify an in-parameter.
Is there such a term?
The only term that I have heard relating to what you are talking about is: parameter idempotence
Simply put this type of function guarantees that the arguments remain untouched.
As far as a function that changes parameters I haven't heard any particular terms but I just say: parameter mutating.
From my experience any function that takes a reference to an object, it's a fairly safe bet that it will be mutating that parameter in some way.

Recursive Functions - Two Functions or Last Optional Parameter

When writing recursive functions, it sometimes happens that something should happen only on the first pass of the recursive algorithm. When this is true, I have two options,
Have an optional parameter called "first run" which is set to true by default but when called recursively, the argument is false
Have two functions
Which option is preferable? If it is the latter, what should I name these functions? (e.g. if its a flood fill algorithm would I choose FloodFill and FloodFillRecursive?)
Thanks in advance, ell.
I might use two functions, and I would say that the function that will be called should be named FloodFill : the user doesn't need to know how that function is implemented, so it should not be named FloodFillRecursive.
Actually, FloodFillRecursive could be the name of the inner function : the one that contains the implementation, the on that is called by the one called by the user -- as it is that second function that is recursive.
Ideally, that function should not be visible from the users : it should be kind of hidden in your library (be it trully hidden, or using some naming-convention that informs users they should not call it directly).
And, this way, if you change implementation, you will not have your users call a FloodFillRecursive function that might no be recursive anymore.
It would depend really if the function is intended to be usable by 3rd party developers. If it is it might be preferable to use the two functions approach for neatness's sake, with the second function (FloodFillRecursive) private/internal to your library.
If it's not then the optional parameter approach is fine.
Option 2 is better in every case I can think of. This depends on the language you're using, but you're probably going to see significantly more (entirely avoidable) overhead by passing an additional argument every time.
For the naming convention, use a normal name for the outer function (eg FloodFill). For the inner function I'd say FloodFillRecursive or FloodFillInner are good choices.
If the language allows it then in my opinion the best is to have one function with the official "clean" interface, and the using a local function (not visible outside) for the recursion.
For example in Common Lisp
(defun n-queens (n)
(let ((result (list)))
(labels ((place-queen (row free-cols free-diagonals free-counter-diagonals)
...))
(place-queen 0 ...)
result)))
or Python
def n_queens(n):
result = []
def place_queen(row, free_cols, free_diags, free_counter_diags):
...
place_queen(0, ...)
return result
in the above example the recursive functions requires many parameters (e.g. the still free columns, diagonals and counter-diagonals) but the official public function only accepts a parameter and the recursion is handled internally.

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.