syntactic-sugar

People keep telling me I'm writing “C style iterators” in ruby, and that I'm supposed to do it this other way, without the i++ thing

心已入冬 提交于 2019-12-24 08:57:44
问题 The details here aren't important, it's just an example, all that matters is the i=0, i+=1 action def sortAndIndex #sorting keys @disco = Hash[@disco.sort] #basic setup for both input types @years = @disco.keys @albums = @disco.values sum = @years.count #setup for "albums" input @allalbums = [] i = 0 sum.times do thatyear = @years[i] + ", " + @albums[i] @allalbums << thatyear i += 1 end end Now this does work, as should any other "i++" type ("C-style") iterator in ruby or most other languages

Do macros make naturally chained comparisons possible in Scala?

混江龙づ霸主 提交于 2019-12-24 00:26:00
问题 Scala does not provide chained comparisons as Python does: // Python: 0 < x <= 3 // Scala: 0 < x && x <= 3 Will Scala 2.10 with the new macro feature enable the programmer write a library that adds this feature? Or is this beyond the scope of Scala's macros? Macros seem to be the right choice for the implementation of such syntactic sugar as they do not complicate the parser/compiler. 回答1: You don't need macros for it: class ChainedComparisons[T : Ordering](val res: Boolean, right: T) { def <

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

霸气de小男生 提交于 2019-12-22 22:02:08
问题 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,

java, is there a way we can import a class under another name

青春壹個敷衍的年華 提交于 2019-12-22 01:23:10
问题 is there a way we can import a class under another name? Like if i have a class called javax.C and another class called java.C i can import javax.C under the name C1 and import java.C under the name C2. We can do something like this in C#: using Sys=System; or Vb: Imports Sys=System 回答1: No, there is nothing like that in Java. You can only import classes under their original name, and have to use the fully qualified name for all that you don't import (except those in java.lang and the current

Ignore an element while building list in python

泪湿孤枕 提交于 2019-12-21 07:13:28
问题 I need to build a list from a string in python using the [f(char) for char in string] syntax and I would like to be able to ignore (not insert in the list) the values of f(x) which are equal to None . How can I do that ? 回答1: We could create a "subquery". [r for r in (f(char) for char in string) if r is not None] If you allow all False values (0, False, None, etc.) to be ignored as well, filter could be used: filter(None, (f(char) for char in string) ) # or, using itertools.imap, filter(None,

Boolean assignment operators in PHP

纵饮孤独 提交于 2019-12-21 03:32:36
问题 I find myself doing this kind of thing somewhat often: $foo = true; $foo = $foo && false; // bool(false) With bitwise operators, you can use the &= and |= shorthand: $foo = 1; $foo &= 0; // int(0) Given that bitwise operations on 1 and 0 are functionally equivalent to boolean operations on true and false , we can rely on type-casting and do something like this: $foo = true; $foo &= false; // int(0) $foo = (bool)$foo; // bool(false) ...but that's pretty ugly and defeats the purpose of using a

How can I use collection initializer syntax with ExpandoObject?

徘徊边缘 提交于 2019-12-20 18:49:07
问题 I've noticed that the new ExpandoObject implements IDictionary<string,object> which has the requisite IEnumerable<KeyValuePair<string, object>> and Add(string, object) methods and so it should be possible to use the collection initialiser syntax to add properties to the expando object in the same way as you add items to a dictionary. Dictionary<string,object> dict = new Dictionary<string,object>() { { "Hello", "World" } }; dynamic obj = new ExpandoObject() { { "foo", "hello" }, { "bar", 42 },

How to initialize the dynamic array of chars with a string literal in C++?

北慕城南 提交于 2019-12-20 02:52:55
问题 I want to do the following: std::unique_ptr<char[]> buffer = new char[ /* ... */ ] { "/tmp/file-XXXXXX" }; Obviously, it doesn't work because I haven't specified the size of a new array. What is an appropriate way to achieve my goal without counting symbols in a string literal? Usage of std::array is also welcome. Update #1: even if I put the size of array, it won't work either. Update #2: it's vital to have a non-const access to the array as a simple char* pointer. 回答1: Here's a solution

Are arrays in C a syntactic sugar for pointers?

删除回忆录丶 提交于 2019-12-18 18:28:52
问题 Let's take a look at the following code: int arr[n]; // s.t. i<n arr[i] = 12; // s.t. i<n *(arr + i) = 12; Is arr[i] is a syntatic sugar for *(arr+ i) ? 回答1: Yes you can say that- array subscript access is identical to pointer access with * dereference. From 6.5.2.1p2 C11 standard N1570 A postfix expression followed by an expression in square brackets [] is a subscripted designation of an element of an array object. The definition of the subscript operator [] is that E1[E2] is identical to (*

Are arrays in C a syntactic sugar for pointers?

筅森魡賤 提交于 2019-12-18 18:28:18
问题 Let's take a look at the following code: int arr[n]; // s.t. i<n arr[i] = 12; // s.t. i<n *(arr + i) = 12; Is arr[i] is a syntatic sugar for *(arr+ i) ? 回答1: Yes you can say that- array subscript access is identical to pointer access with * dereference. From 6.5.2.1p2 C11 standard N1570 A postfix expression followed by an expression in square brackets [] is a subscripted designation of an element of an array object. The definition of the subscript operator [] is that E1[E2] is identical to (*