我有一个函数,它期望实数(整数或浮点数)作为它的输入,我试图在对它进行数学运算之前验证这个输入.
我的第一直觉是将输入作为浮点数从try-except块中转换出来.
try:
myinput = float(input)
except:
raise ValueError("input is not a well-formed number")
Run Code Online (Sandbox Code Playgroud)
我也可以打电话,isinstance(mydata, (float, int, long) )但"所有这些可能是数字"的清单对我来说似乎有点不合适.
最狡猾的方式是什么?我忽略了另一种选择吗?
据我所知,迭代STL集合的习惯看起来像这样:
int a[] = { 1,2,3 };
std::vector<int> v(a, a+3);
std::for_each(a.begin(), a.end(), some_function);
Run Code Online (Sandbox Code Playgroud)
如果我只想处理集合的某个范围,或者做一些更有创意的事情,那么指定第一个和最后一个迭代器是很有用的,但大多数时候,我怀疑我们实际上是想要使用整个集合.所以,我想知道为什么人们在那种情况下烦恼指定迭代器(因为它们总是相同的),并且不只是沿着这些方向使用便利函数:
namespace easy
{
template <class T, class F>
F for_each(T& collection, F function)
{
std::for_each(collection.begin(), collection.end(), function);
return function;
}
}
Run Code Online (Sandbox Code Playgroud)
(当然,这是可能的,这是做事的惯用方式,我从来没有注意到!我是新的C++,虽然).
PIMPL Idiom是一种实现隐藏的技术,其中公共类包装公共类所属的库外部无法看到的结构或类.这会隐藏来自库用户的内部实现细节和数据.
但是可以实现相同的参考使用吗?
MCanvasFont.h
namespace Impl {
class FontDelegate;
}
class MCanvasFont
{
public:
MCanvasFont();
virtual ~MCanvasFont();
protected:
// Reference count
long m_cRef;
// agg font delegate
const Impl::FontDelegate& m_font;
}
Run Code Online (Sandbox Code Playgroud)
MCanvasFont.cpp
// helpers
#include "ImplHelpers/FontDelegate.h"
MCanvasFont::MCanvasFont()
: m_cRef(1),
m_font(Impl::FontDelegate() )
{
// constructor's body
}
Run Code Online (Sandbox Code Playgroud)
PS此代码编译时没有任何G ++问题.
我正在阅读Go的compress/flate包,我发现了这段奇怪的代码[1]:
n := int32(len(list))
list = list[0 : n+1]
list[n] = maxNode()
Run Code Online (Sandbox Code Playgroud)
在上下文中,list保证指向具有更多数据的数组.这是一个私有函数,因此不能在库外滥用.
对我来说,这似乎是一个可怕的黑客应该是运行时异常.例如,以下D代码生成RangeError:
auto x = [1, 2, 3];
auto y = x[0 .. 2];
y = y[0 .. 3];
Run Code Online (Sandbox Code Playgroud)
使用以下内容可以更简单地(并且看起来更安全)进行滥用切片:
x := []int{1, 2, 3}
y = x[:2]
y = append(y, 4) // x is now [1, 2, 4] because of how append works
Run Code Online (Sandbox Code Playgroud)
但是这两种解决方案看起来都非常骇人和可怕,恕我直言,不应该像他们那样工作.这种事情被认为是惯用的Go代码吗?如果是这样,上面哪一个更惯用?
[1] - http://golang.org/src/pkg/compress/flate/huffman_code.go#L136
在Common Lisp中,检查元素是否在列表中的更惯用的方法是什么?目前我正在检查"get"是否属于这样的列表:
(some #'(lambda (x) (string= x "get")) '("get" "update" "delete" "replace"))
Run Code Online (Sandbox Code Playgroud) 你如何将c风格的循环转换为python?
for (int i = m; i >= lowest; i--)
Run Code Online (Sandbox Code Playgroud)
我想出的最好的是:
i = mid
for i in range(i, low,-1):
Run Code Online (Sandbox Code Playgroud) 以下显然不起作用:
fn main() {
for i in range(1i, 101) {
println!("{}", if i % 15 == 0 {
"Fizzbuzz"
} else if i % 5 == 0 {
"Buzz"
} else if i % 3 == 0 {
"Fizz"
} else {
i
});
};
}
Run Code Online (Sandbox Code Playgroud)
它可以像这样工作:
fn main() {
for i in range(1i, 101) {
println!("{}", if i % 15 == 0 {
"Fizzbuzz".to_string()
} else if i % 5 == 0 {
"Buzz".to_string()
} else if i % …Run Code Online (Sandbox Code Playgroud) 假设我有一个
template <typename T>
class A :
class_with_long_name<T, and_many_other, template_arguments, oh_my_thats_long>,
anotherclass_with_long_name<and_many_other, template_arguments_that, are_also_annoying, including_also, T> { ... }
Run Code Online (Sandbox Code Playgroud)
现在,A类的定义,和/或它的方法,我需要参考两个超类(例如,要在超成员访问,或者在它等定义的类型),但我想避免重复超的名字.目前,我正在做的是:
template<typename T>
class A :
class_with_long_name<T, and_many_other, template_arguments, oh_my_thats_long>,
anotherclass_with_long_name<and_many_other, template_arguments_that, are_also_annoying, including_also, T>
{
using parent1 = class_with_long_name<T, and_many_other, template_arguments, oh_my_thats_long>;
using parent2 = anotherclass_with_long_name<and_many_other, template_arguments_that, are_also_annoying, including_also, T>;
...
}
Run Code Online (Sandbox Code Playgroud)
显然,这有效,并将重复次数减少到2; 但如果可能的话,我宁愿避免这种重复.有合理的方法吗?
笔记:
我无法在for 循环和std::any_of之间做出决定,例如:
QString fileName = "Whatever"; // might contain a key-string which is part of sMyStringlist()
auto anyTypeContains = [&](const QString& categoryStr)
{
for(auto const &keyStr : sMyStringlist()[categoryStr])
if(fileName.contains(keyStr, Qt::CaseInsensitive))
return true;
return false;
};
if(anyTypeContains("myCategory"))
...
Run Code Online (Sandbox Code Playgroud)
或者
QString fileName = "Whatever"; // might contain a key-string which is part of sMyStringlist()
auto anyTypeContains = [&](const QString& categoryStr)
{
return std::any_of(
sMyStringlist()[categoryStr].begin(),
sMyStringlist()[categoryStr].end(),
[&](const QString& keyStr){
return fileName.contains(keyStr, Qt::CaseInsensitive);
});
};
if(anyTypeContains("myCategory"))
...
Run Code Online (Sandbox Code Playgroud)
我认为for 循环 …
考虑以下代码:
fn foo(x: i32) -> Result<i32, Error> {
//...
}
fn bar(x: Result<i32,Error>) -> Result<i32, Error> {
//...
}
fn main() {
let y = bar(foo(2)).unwrap();
}
Run Code Online (Sandbox Code Playgroud)
这是惯用的,传递Result类型吗?或者你应该bar()在i32直接传递之前处理错误或解开结果。