I'm trying to use timeit to time an algorthm (reversing the string)
My question is about the signature of timeit.repeat
here is what i'm using, it is working fine
timeit.repeat(lambda: reverseString_perf(s), setup='pass', timer=timeit.default_timer,number=3)
my questions is - why can i not pass the function directly (as shown below)
timeit.repeat(reverseString_perf(s), setup='pass', timer=timeit.default_timer,number=3) #does not work
instead i'm having to pass lambda:reverseString_perf(s)
to get it to work.
there is no anonymous function here, so this is confusing usage.
reverseString_perf(s) evaluates to the return value. If you passed in just reverseString_perf, it would work; however, you wouldn't be able to include the parameter in this case.
lambda: reverseString_perf(s) works because it is still a function, unlike reverseString_perf(s), which is the return value.
Related
I have a program that calls a function using
Enum.flat_map(text, &lex_raw_tokens/1)
but, i changed the function "lex_raw_tokens" to have 2 arguments, which means i can't use flat_map since flat_map/3 doesn't exist as far as i know.
I'm not sure what function to use then, so far just calling the function as
lex_raw_tokens(text,line)
didn't quite work as i'm given an argument error. I think it's because the arguments given are lists and the function need separate list items, which flat_map takes care of. Any ideas?
Perhaps the easiest way is to just to wrap &lex_raw_tokens/2 in an anonymous 1-arity function
# depending what `text` looks like
Enum.flat_map(text, &lex_raw_tokens(&1, line))
# or
Enum.flat_map(text, fn {text, line} -> lex_raw_tokens(text, line) end)
I'm doing a simple tutorial based around threads. In this exercise I am supposed to get threads to wait for each other.
I have copied the example code verbatim with the exception that I'm not using namespace std; and am instead writing in std::each time I need it.
The error in question corresponds to the line:
if (condition.wait_for(std::unique_lock<std::mutex>(mut), std::chrono::seconds(3)))
Intellisense tells me that "the expression must have bool type (or be convertible to bool)" but I looked up the documentation on condition.wait_for and it can return std::cv_status::timeout, std::cv_status::no_timeout as well as true and false. When I go to build, it thinks it can only return std::cv_status.
It should work right? Does it require the third parameter? The example I'm following doesn't use one.
As Bo says, there are 2 versions of the function. I'm going to assume there was a typo in the example and it meant to use the version of the function with three parameters, here's some working code, I don't know if it works in the same way as the example intended.
if (condition.wait_for(std::unique_lock<std::mutex>(mut), std::chrono::seconds(3), [] {return true; }))
Based on a poorly stated and recently deleted SO question ("Is it possible to call a function without calling it?") I have a similar question, hopefully put in a more logical perspective.
Is it possible / what are the best practices, to disable a function call from a codebase ? By disabling I don't mean greping through the whole code to manually comment out the function (which is a valid but somewhat tedious task). The only ways I can think of are
Returning as soon as entering function
ret_type foo()
{
return ret_type();
// actual implementation is not allowed to run
}
which would be a bit dangerous when the return code is used by caller functions.
Replace the declaration with an idle macro
ret_type foo();
#define foo() do { void; } while (0);
Is there a standard way, maybe a compiler hook, a pragma directive to do this and if not what are some other ways?
Is there a standard way, maybe a compiler hook, a pragma directive to do this and if not what are some other ways?
Let's just think for a minute, together. Let's consider two main cases:
the function returns void
the function returns something
In the first case you can simply take the body of the function and comment it out. BOOM: disabled.
In the second case you have a return value. Let's consider other two cases:
the returned value is used
the returned value is not used
In the first case you should ask yourself: can I return a dummy value and get away with it? If the answer is yes, then do so. If not, then you can't do anything about it except refactor your entire code.
In the second case you can comment it out, but why you are returning a value in the first place.
I have the following code in Lua:
ABC:
test (X)
The test function is implemented in C + +. My problem is this: I need to know what the variable name passed as parameter (in this case X). In C + + only have access to the value of this variable, but I must know her name.
Help please
Functions are not passed variables; they are passed values. Variables are just locations that store values.
When you say X somewhere in your Lua code, that means to get the value from the variable X (note: it's actually more complicated than that, but I won't get into that here).
So when you say test(X), you're saying, "Get the value from the variable X and pass that value as the first parameter to the function test."
What it seems like you want to do is change the contents of X, right? You want to have the test function modify X in some way. Well, you can't really do that directly in Lua. Nor should you.
See, in Lua, you can return values from functions. And you can return multiple values. Even from C++ code, you can return multiple values. So whatever it is you wanted to store in X can just be returned:
X = test(X)
This way, the caller of the function decides what to do with the value, not the function itself. If the caller wants to modify the variable, that's fine. If the caller wants to stick it somewhere else, that's also fine. Your function should not care one way or the other.
Also, this allows the user to do things like test(5). Here, there is no variable; you just pass a value directly. That's one reason why functions cannot modify the "variable" that is passed; because it doesn't have to be a variable. Only values are passed, so the user could simply pass a literal value rather than one stored in a variable.
In short: you can't do it, and you shouldn't want to.
The correct answer is that Lua doesn't really support this, but there is the debug interface. See this question for the solution you're looking for. If you can't get a call to debug to work directly from C++, then wrap your function call with a Lua function that first extracts the debug results and then calls your C++ function.
If what you're after is a string representation of the argument, then you're kind of stuck in lua.
I'm thinking something like in C:
assert( x==y );
Which generates a nice message on failure. In C this is done through macros.
Something like this (untested and probably broken).
#define assert(X) if(!(X)) { printf("ASSERION FAILED: %s\n", #X ); abort(); }
Here #X means the string form of the arguments. In the example above that is "x==y". Note that this is subtly different from a variable name - its just the string used in the parser when expanding the macro.
Unfortunately there's no such corresponding functionality in lua. For my lua testing libraries I end up passing the stringified version as part of the expression, so in lua my code looks something like this:
assert( x==y, "x==y")
There may be ways to make this work as assert("x==y") using some kind of string evaluation and closure mechanism, but it seemed to tricky to be worth doing to me.
EDIT:
While this doesn't appear to be possible in pure lua, there's a patched version that does seem to support macros: http://lua-users.org/wiki/LuaMacros . They even have an example of a nicer assert.
I'm using Boost to embed Python in my application. For example, I want to check that the following function receives an integer and a string as the first and second parameters (the function is defined in C++).
someFunction(123, 'words')
If I find that the parameters are incorrect, how can I notify the scripter about which line they need to correct, for example?
If you wrap the function using usual def("someFunction",someFunction,...), caller will get automatically notified about c++ signature which could not be matched with objects passed from python, like this (the method takes one dictionary argument, is called with 3 numbers instead):
>>> scene.updateAttrs(1,2,3)
ArgumentError: Python argument types in
Serializable.updateAttrs(Scene, int, int, int)
did not match C++ signature:
updateAttrs(Serializable {lvalue}, boost::python::dict)
Can you post some code to see what is your problem?
Raise an exception with all the information you want them to know, just like you would in Python. In fact, that answer seems so obvious, it makes me think I'm missing something in your question.