idiomatic

How do you rotate (circular shift) of a Scala collection

蹲街弑〆低调 提交于 2019-12-05 08:12:48
I can do this quite easily, and cleanly, using a for loop. For instance, if I wanted to traverse a Seq from every element back to itself I would do the following: val seq = Seq(1,2,3,4,5) for (i <- seq.indices) { for (j <- seq.indices) { print(seq(i + j % seq.length)) } } But as I'm looking to fold over the collection, I'm wondering if there is a more idiomatic approach. A recursive approach would allow me to avoid any var s. But basically, I'm wondering if something like the following is possible: seq.rotatedView(i) Which would create a rotated view, like rotating bits (or circular shift).

How to write a shortest and most idiomatic CLI calculator in Clojure

一个人想着一个人 提交于 2019-12-05 05:55:15
问题 I like to learn a new language by making small tool like calculator. Although I already searched a lot idiomatic examples about specific cases(such as idiomatic usage of array and list), I have no idea how to put those together to write this small calculator in an idiomatic way. So here is my code: (defn pre-process [s] "Seperate operands with operators and replace ( with l, ) with r" (re-seq #"\d+|[\+\-\*\/lr]" (clojure.string/replace s #"\(|\)" {"(" "l" ")" "r"}))) (defn calc-once [stk]

HOWTO: Idiomatic Rust for callbacks with gtk (rust-gnome)

这一生的挚爱 提交于 2019-12-05 02:07:59
I am currently learning Rust and looking to use it for developing a GUI based application with GTK+. My problem relates to registering callbacks to respond to GTK events/signals and mutating state within those callbacks. I have a working but inelegant solution, so I would like to ask if there is a cleaner, more idiomatic solution. I have implemented my code as a struct with method implementations, where the struct maintains references to the GTK widgets along with other state that it needs. It constructs a closure that is passed to the GtkWidget::connect* functions in order to receive events,

When is `new Error()` better than `Error()`?

妖精的绣舞 提交于 2019-12-04 23:11:27
The ES5 language spec clearly states that Error(foo) does the same thing as new Error(foo) . But I notice that in the wild, the longer new Error(foo) form is much more common. Is there some reason for this? Is there any situation where using new Error(foo) is preferable to using Error(foo) ? Is there some reason for this? It's simply the habit of always calling constructors with new . Consistency rules! It's a good practice to do even when they work without new , and recommended by several style guides and related tooling. Btw, since ES6 Error is subclassible , and its subclasses will require

Using nnet for prediction, am i doing it right?

若如初见. 提交于 2019-12-04 07:44:58
问题 I'm still pretty new to R and AI / ML techniques. I would like to use a neural net for prediction, and since I'm new I would just like to see if this is how it should be done. As a test case, I'm predicting values of sin() , based on 2 previous values. For training I create a data frame with y = sin(x) , x1 = sin(x-1) , x2 = sin(x-2) , then use the formula y ~ x1 + x2 . It seems to work, but I am just wondering if this is the right way to do it, or if there is a more idiomatic way. This is

In C++ why can't we compare iterators using > and <? [duplicate]

一世执手 提交于 2019-12-04 03:48:09
This question already has answers here : Closed 3 years ago . Why is “!=” used with iterators instead of “<”? (2 answers) I have been asked this question which I do not really know why. If you have a pointer int * x; You can compare pointers with > and < because it stands for the memory location something like 0x0000 0x0004 0x0008 , etc. I know iterators and pointers are different, but they act in a very similar way. For example: vector<int> myVector; for(int i = 1; i < 101; i++) { myVector.push_back(i); } vector<int>::iterator it = myVector.begin(); while(it != myVector.end()) //Why can't we

What is the idiomatic way to get the index of a maximum or minimum floating point value in a slice or Vec in Rust?

痞子三分冷 提交于 2019-12-04 03:33:25
问题 Assumption -- The Vec<f32> does not have any NaN values or exhibit any NaN behavior. Take the following sample set: 0.28 0.3102 0.9856 0.3679 0.3697 0.46 0.4311 0.9781 0.9891 0.5052 0.9173 0.932 0.8365 0.5822 0.9981 0.9977 What is the neatest and most stable way to get the index of the highest value in the above list (values can be negative)? My initial attempts were along the following lines: let _tmp = *nets.iter().max_by(|i, j| i.partial_cmp(j).unwrap()).unwrap(); let _i = nets.iter()

Should I be trying to create a reversible enum in Java or is there a better way?

断了今生、忘了曾经 提交于 2019-12-04 03:03:52
I seem to have faced this problem many times and I wanted to ask the community whether I am just barking up the wrong tree. Basically my question can be distilled down to this: if I have an enum (in Java) for which the values are important, should I be using an enum at all or is there a better way, and if I do use an enum then what is the best way to reverse the lookup? Here's an example. Suppose I want to create a bean representing a specific month and year. I might create something like the following: public interface MonthAndYear { Month getMonth(); void setMonth(Month month); int getYear()

python idiomatic python for loop if else statement

给你一囗甜甜゛ 提交于 2019-12-04 00:57:48
问题 How can I use else statement in an idiomatic Python for loop? Without else I can write e.g.: res = [i for i in [1,2,3,4,5] if i < 4] The result is: [1, 2, 3] The normal form of the above code is: res = [] for i in [1,2,3,4,5]: if i < 4: res.append(i) The result is the same as in idiomatic form: [1, 2, 3] And I want this: res = [i for i in [1,2,3,4,5] if i < 4 else 0] I get SyntaxError: invalid syntax . The result should be: [1, 2, 3, 0, 0] The normal code of this is: res = [] for i in [1,2,3

How to write a shortest and most idiomatic CLI calculator in Clojure

早过忘川 提交于 2019-12-03 20:29:17
I like to learn a new language by making small tool like calculator. Although I already searched a lot idiomatic examples about specific cases(such as idiomatic usage of array and list), I have no idea how to put those together to write this small calculator in an idiomatic way. So here is my code: (defn pre-process [s] "Seperate operands with operators and replace ( with l, ) with r" (re-seq #"\d+|[\+\-\*\/lr]" (clojure.string/replace s #"\(|\)" {"(" "l" ")" "r"}))) (defn calc-once [stk] "Take one operator from operator stack and apply it to top two numbers in operand stack" (let [opt (:opt