idioms

Idiomatic match with fall-through in Rust

随声附和 提交于 2019-12-01 13:49:59
I’m new to Rust, but as a fan of Haskell, I greatly appreciate the way match works in Rust. Now I’m faced with the rare case where I do need fall-through – in the sense that I would like all matching cases of several overlapping ones to be executed. This works: fn options(stairs: i32) -> i32 { if stairs == 0 { return 1; } let mut count: i32 = 0; if stairs >= 1 { count += options(stairs - 1); } if stairs >= 2 { count += options(stairs - 2); } if stairs >= 3 { count += options(stairs - 3); } count } My question is whether this is idiomatic in Rust or whether there is a better way. Edit: The

Idiomatic implementation of the tribonacci sequence in Rust

江枫思渺然 提交于 2019-12-01 13:04:13
问题 I’m new to Rust, but as a fan of Haskell, I greatly appreciate the way match works in Rust. Now I’m faced with the rare case where I do need fall-through – in the sense that I would like all matching cases of several overlapping ones to be executed. This works: fn options(stairs: i32) -> i32 { if stairs == 0 { return 1; } let mut count: i32 = 0; if stairs >= 1 { count += options(stairs - 1); } if stairs >= 2 { count += options(stairs - 2); } if stairs >= 3 { count += options(stairs - 3); }

Python string concatenation Idiom. Need Clarification.

末鹿安然 提交于 2019-12-01 07:38:06
From http://jaynes.colorado.edu/PythonIdioms.html "Build strings as a list and use ''.join at the end. join is a string method called on the separator, not the list. Calling it from the empty string concatenates the pieces with no separator, which is a Python quirk and rather surprising at first. This is important: string building with + is quadratic time instead of linear! If you learn one idiom, learn this one. Wrong: for s in strings: result += s Right: result = ''.join(strings)" I'm not sure why this is true. If I have some strings I want to join them, for me it isn't intuitively better to

Avoid nested for-loops when searching parameter space

风流意气都作罢 提交于 2019-12-01 06:32:48
When writing unit tests I often want to invoke a function with a combination of parameters. For example, I have a function which is declared as void tester_func(int p1, double p2, std::string const& p3); and some selected parameters std::vector<int> vec_p1 = { 1, 2, 666 }; std::vector<double> vec_p2 = { 3.14159, 0.0001 }; std::vector<std::string> vec_p3 = { "Method_Smart", "Method_Silly" }; What I currently do is simply for(auto const& p1 : vec_p1) for(auto const& p2 : vec_p2) for(auto const& p3 : vec_p3) tester_func(p1, p2, p3); However, Sean Parent suggests to avoid explicit loops and use

Avoid nested for-loops when searching parameter space

对着背影说爱祢 提交于 2019-12-01 04:32:33
问题 When writing unit tests I often want to invoke a function with a combination of parameters. For example, I have a function which is declared as void tester_func(int p1, double p2, std::string const& p3); and some selected parameters std::vector<int> vec_p1 = { 1, 2, 666 }; std::vector<double> vec_p2 = { 3.14159, 0.0001 }; std::vector<std::string> vec_p3 = { "Method_Smart", "Method_Silly" }; What I currently do is simply for(auto const& p1 : vec_p1) for(auto const& p2 : vec_p2) for(auto const&

Why does such a struct contain two array fields containing only one element?

跟風遠走 提交于 2019-12-01 03:24:56
Please Note: This question is not a duplicate of ( One element array in struct ) The following code is excerpted from the Linux kernel source (version: 3.14) struct files_struct { atomic_t count; struct fdtable __rcu *fdt; struct fdtable fdtab; spinlock_t file_lock ____cacheline_aligned_in_smp; int next_fd; unsigned long close_on_exec_init[1]; unsigned long open_fds_init[1]; struct file __rcu * fd_array[NR_OPEN_DEFAULT]; }; I just wonder why close_on_exec_init and open_fds_init are defined as arrays containing one element, rather than just defined as unsigned long close_on_exec_init; and

Why and when should a comma be used at the end of a block?

醉酒当歌 提交于 2019-11-30 21:48:58
There many cases in Rust when a block of code can end with or without comma. For example: enum WithoutComma { x, y } or enum WithComma { x, y, } There are also other examples with match , etc. It seems that both variants lead to the same result. The only case I know where adding or removing a comma changes behaviour is the 1-element tuple declaration (which isn't a block): let just_int = (5); let tuple = (5,); Why can one use a comma or not at the end of a block? Why is there such dualism in thelanguage and what are the reasons for it? huon As you say, the only time a trailing comma is

jquery .val() += idiom

喜夏-厌秋 提交于 2019-11-30 18:00:16
What's the clearest commonly used idiom for this jQuery snippet? $('#someTextarea').val( $('#someTextarea').val() + someString ); It feels clunky to wrap the original code in a one-line function EDIT: So I can pass a function, which is cool... but my real intentions are for jsfiddles, where I currently do stuff like this: function lazylog (str) { $('#ta').val( $('#ta').val() + str + '\n' ); } // or function lazylogPlain (str) { document.getElementById('ta').value += str + '\n'; } // view results of little experiments lazylog( test1() ); lazylog( test2() ); lazylog( test3() ); // etc... Don't

Sentinel object and its applications?

我的未来我决定 提交于 2019-11-30 15:45:15
I know in python the builtin object() returns a sentinel object. I'm curious to what it is, but mainly its applications. object is the base class that all other classes inherit from in python 3. There's not a whole lot you can do with a plain old object. However an object's identity could be useful. For example the iter function takes a sentinel argument that signals when to stop termination. We could supply an object() to that. sentinel = object() def step(): inp = input('enter something: ') if inp == 'stop' or inp == 'exit' or inp == 'done': return sentinel return inp for inp in iter(step,

Sort a list of objects by using their attributes in Ruby

狂风中的少年 提交于 2019-11-30 15:35:04
I have a list of Fruit structs called basket . Each Fruit struct has a name (a string) and a calories (an integer). I would like to sort basket so that: The Fruit s with the highest calories appear first. For example, a fruit with 500 calories appears before a fruit with 400 calories. If two Fruit s have equal calories , the Fruit whose name comes first alphabetically comes first, ignoring case. For example, given two fruits with equal calories, one named "banana" will come before one named "Citrus". The definition of Fruit is not something I control so I'd prefer a solution which doesn't