evaluation

How do you evaluate a string as a clojure expression?

℡╲_俬逩灬. 提交于 2019-11-28 10:41:19
How would I get something similar to the following?: (evaluate-text "(+ 1 2)") ; resolves to 3 (load-string "(+ 1 2)") user> (eval (read-string "(+ 1 2)")) 3 You probably shouldn't ever need to do this. Macros and fns make this kind of thing unnecessary 99% of the time. This is quite brittle, and can be unsafe if these strings are coming from user input, and so on. How similar does it have to be? Clojure's eval works on lists, so: (eval (list + 1 2)) #=> 3 来源: https://stackoverflow.com/questions/1885448/how-do-you-evaluate-a-string-as-a-clojure-expression

Why do F# functions evaluate before they are called?

蹲街弑〆低调 提交于 2019-11-28 06:06:21
问题 If I define a module as such: module Module1 open System let foo = Console.WriteLine("bar") Then, in interactive do #load "Library1.fs" //where the module is defined open Module1 I see a [Loading c:\users\jj\documents\visual studio 2015\Projects\Library1\Library1\Library1.fs] bar Indicating that the foo function ran without me even calling it! How/why does this happen? Is there any way to prevent it? I'm aware that the return value of foo is whatever (Console.Writeline("bar")) evaluates to,

When Java evaluates a conjunction (<boolean exp1> && <boolean exp2>), does it eval exp2 if exp1 is false?

六眼飞鱼酱① 提交于 2019-11-28 02:05:36
I'm wondering if it's guaranteed that in a Java program, the boolean expression on the right of a conjunction (exp2 above) will NOT be evaluated as long as the expression on the left (exp1) evaluated to false. I'm wondering because I have an expression like the following: if (var != null && var.somePredicate()) // do something If Java is not guaranteed to stop evaluating (var != null && var.somePredicate()) after it sees that var is null, then it may try to evaluate var.somePredicate() which would throw a NullPointerException. So my question is, does Java guarantee a certain behavior when it

Forcing a constant expression to be evaluated during compile-time?

我只是一个虾纸丫 提交于 2019-11-27 22:23:18
A few days ago I asked by which criteria the compiler decides whether or not, to compute a constexpr function during compile time. When does a constexpr function get evaluated at compile time? As it turns out, a constexpr is only evaluated during compile-time, if all parameters are constant expressions and the variable you are assigning it to is are constant expression as well. template<typename base_t, typename expo_t> constexpr base_t POW(base_t base, expo_t expo) { return (expo != 0 )? base * POW(base, expo -1) : 1; } template<typename T> void foobar(T val) { std::cout << val << std::endl;

How to write an R function that evaluates an expression within a data-frame

折月煮酒 提交于 2019-11-27 20:51:13
Puzzle for the R cognoscenti: Say we have a data-frame: df <- data.frame( a = 1:5, b = 1:5 ) I know we can do things like with(df, a) to get a vector of results. But how do I write a function that takes an expression (such as a or a > 3 ) and does the same thing inside. I.e. I want to write a function fn that takes a data-frame and an expression as arguments and returns the result of evaluating the expression "within" the data-frame as an environment. Never mind that this sounds contrived (I could just use with as above), but this is just a simplified version of a more complex function I am

Javascript parser for simple expression

亡梦爱人 提交于 2019-11-27 19:12:11
I would like to find a javascript parser that can handle and evaluate simple expressions. The parser should be able to evaluate the regular mathematical expressions, and support custom functions with parameters. It also has to support strings handling. String concatenation with || operator support is preferred, but it is okay if + will do the trick. Examples of an expression that should be handled by the parser: 3 * (2 + 1) - 1 2 * func(2, 2) func('hello world', 0, 5) || ' you' Has anyone implemented such a thing or where can I find something similar? I have a modified version of an

Does all(list) use short circuit evaluation? [duplicate]

回眸只為那壹抹淺笑 提交于 2019-11-27 16:10:12
This question already has an answer here: Is the shortcircuit behaviour of Python's any/all explicit? 4 answers I wish to use the Python all() function to help me compute something, but this something could take substantially longer if the all() does not evaluate as soon as it hits a False . I'm thinking it probably is short-circuit evaluated, but I just wanted to make sure. Also, is there a way to tell in Python how the function gets evaluated? TerryA Yes, it short-circuits: >>> def test(): ... yield True ... print('one') ... yield False ... print('two') ... yield True ... print('three') ...

C : is there “lazy evaluation” when using && operator, as in C++?

不问归期 提交于 2019-11-27 15:30:45
I would like to know if this looks correct : while((next !=NULL) && (strcmp(next->name, some_string) < 0) { //some process } I mean, if next is NULL , then the second part of the expression won't be ever tested by the compiler? I have heard that in C++ it's the case (but I'm not even sure of it). Can someone confirm me that I won't get strange errors on some compilers with that? Yes && is short circuited and you are using it correctly. If next is NULL string compare will never happen. Yes, in C++ short circuit and and or operators are available. Here 's a question answered in the C-faq on the

Dynamic source code in C++ [closed]

懵懂的女人 提交于 2019-11-27 14:33:01
How to process dynamic source code in C++? Is it possible to use something like eval("foo")? I have some functions that need to be called depending on user's choice: void function1 (); void function2 (); ... void functionN (); int main (int argv, char * argv []) { char * myString = new char [100]; ... myString = "1" //user input cout << eval("function"+myString)(); } How is it usually done? UPD : Based on slacy's and clinisbut's answers I think I need to make a function registry. I suppose it should be made as an array of pointers to functions. Here's the question, how do I declare an array of

Bash: evaluate a mathematical term?

不羁岁月 提交于 2019-11-27 13:03:11
echo 3+3 How can I evaluate such expressions in Bash, in this case to 6? in shells such as zsh/ksh, you can use floats for maths. If you need more maths power, use tools like bc/awk/dc eg var=$(echo "scale=2;3.4+43.1" | bc) var=$(awk 'BEGIN{print 3.4*43.1}') looking at what you are trying to do awk '{printf "%.2f\n",$0/59.5}' ball_dropping_times >bull_velocities echo $(( 3+3 )) expr is the standard way, but it only handles integers. bash has a couple of extensions, which only handle integers as well: $((3+3)) returns 6 ((3+3)) used in conditionals, returns 0 for true (non-zero) and 1 for false