syntactic-sugar

Custom data structure syntax in Prolog

萝らか妹 提交于 2019-12-07 14:21:08
问题 In Prolog, [H|T] is the list that begins with H and where the remaining elements are in the list T (internally represented with '.'(H, '.'(…)) ). Is it possible to define new syntax in a similar fashion? For example, is it possible to define that [T~H] is the list that ends with H and where the remaining elements are in the list T , and then use it as freely as [H|T] in heads and bodies of predicates? Is it also possible to define e.g. <H|T> to be a different structure than lists? 回答1: One

2D matrix and overloading operator() / ugly syntax

社会主义新天地 提交于 2019-12-07 10:34:35
问题 I'm using a 2D matrix in one of my projects. It's something like it is suggested at C++ FAQ Lite. The neat thing is that you can use it like this: int main() { Matrix m(10,10); m(5,8) = 106.15; std::cout << m(5,8); ... } Now, I have a graph composed of vertices and each vertex has a public (just for simplicity of the example) pointer to 2D matrix like above. Now I do have a pretty ugly syntax to access it. (*sampleVertex.some2DTable)(0,0) = 0; //bad sampleVertex.some2DTable->operator()(0,0) =

Would it make sense to have a 'constify' operation in C++?

拟墨画扇 提交于 2019-12-07 02:39:56
问题 Would it make sense to have a " constify " operation in C/C++ that makes a variable const ? Here is an example where it could be useful, where obviously we don't want to declare it const yet in the first line: std::vector<int> v; v.push_back(5); constify v; // now it's const Currently, without such a possibility, you'd have to introduce another variable to get the same effect: std::vector<int> v0; v0.push_back(5); const std::vector<int>& v = v0; That's more confusing since it adds a new name

Syntactic sugar vs. feature

拜拜、爱过 提交于 2019-12-06 19:10:40
问题 In C# (and Java) a string is little more than a char array with a stored length and a few methods tacked on. Likewise, (reference vs. value stuff aside) objects are little more than glorified structs with inheritance and interfaces added. On one level, these additions feel like clear features and enhancements unto themselves. On another level, they feel like a marginal upgrade from the status of "syntactic sugar." To take this idea further, consider (I may have some details wrong, but the

How does the method syntax “public function direct(){}” work in PHP?

三世轮回 提交于 2019-12-06 04:39:22
I'm learning Zend Framework at the moment and came across the following syntax. class Zend_Controller_Action_Helper_Redirector extends Zend_Controller_Action_Helper_Abstract { /** * Perform a redirect to an action/controller/module with params * * @param string $action * @param string $controller * @param string $module * @param array $params * @return void */ public function gotoSimple($action, $controller = null, $module = null, array $params = array()) { $this->setGotoSimple($action, $controller, $module, $params); if ($this->getExit()) { $this->redirectAndExit(); } } /** * direct():

Custom data structure syntax in Prolog

拥有回忆 提交于 2019-12-06 02:39:32
In Prolog, [H|T] is the list that begins with H and where the remaining elements are in the list T (internally represented with '.'(H, '.'(…)) ). Is it possible to define new syntax in a similar fashion? For example, is it possible to define that [T~H] is the list that ends with H and where the remaining elements are in the list T , and then use it as freely as [H|T] in heads and bodies of predicates? Is it also possible to define e.g. <H|T> to be a different structure than lists? One can interpret your question literally. A list-like data structure, where accessing the tail can be expressed

implementing apply function in Rcpp

我怕爱的太早我们不能终老 提交于 2019-12-05 18:43:23
I have been trying to implement apply function in Rcpp so far the code looks like this //[[Rcpp::export]] NumericVector apply(NumericMatrix x,int dim,Function f){ NumericVector output; if(dim==1){ for(int i=0;i<x.nrow();i++){ output[i]=f(x(i,_)); } } else if(dim==2){ for(int i=0;i<x.ncol();i++){ output[i]=f(x(_,i)); } } return(output); } but i'm getting an error "cannot convert SEXP to double in assignment" in line 6 and 11. Is there any way to convert the value returned by an arbitrary function to double? also is there a sugar function for the apply function. There is no sugar function for

Would it make sense to have a 'constify' operation in C++?

女生的网名这么多〃 提交于 2019-12-05 06:31:05
Would it make sense to have a " constify " operation in C/C++ that makes a variable const ? Here is an example where it could be useful, where obviously we don't want to declare it const yet in the first line: std::vector<int> v; v.push_back(5); constify v; // now it's const Currently, without such a possibility, you'd have to introduce another variable to get the same effect: std::vector<int> v0; v0.push_back(5); const std::vector<int>& v = v0; That's more confusing since it adds a new name into the scope and you need to make it a reference to avoid copying the whole vector (or use swap ?).

About Scala's assignments and setter methods

牧云@^-^@ 提交于 2019-12-05 05:09:57
问题 Edit: The bug which prompted this question has now been fixed. In the Scala Reference, I can read (p. 86): The interpretation of an assignment to a simple variable x = e depends on the definition of x. If x denotes a mutable variable, then the assignment changes the current value of x to be the result of evaluating the expression e. The type of e is expected to conform to the type of x. If x is a parameterless function defined in some template, and the same template contains a setter function

Is there a “one-liner” way to get a list of keys from a dictionary in sorted order?

落爺英雄遲暮 提交于 2019-12-05 02:32:55
问题 The list sort() method is a modifier function that returns None . So if I want to iterate through all of the keys in a dictionary I cannot do: for k in somedictionary.keys().sort(): dosomething() Instead, I must: keys = somedictionary.keys() keys.sort() for k in keys: dosomething() Is there a pretty way to iterate through these keys in sorted order without having to break it up in to multiple steps? 回答1: for k in sorted(somedictionary.keys()): doSomething(k) Note that you can also get all of