我有以下代码:-
extension Collection {
// EZSE : A parralelized map for collections, operation is non blocking
public func pmap<R>(_ each: (Self.Iterator.Element) -> R) -> [R?] {
let indices = indicesArray()
var res = [R?](repeating: nil, count: indices.count)
DispatchQueue.concurrentPerform(iterations: indices.count) { (index) in
let elementIndex = indices[index]
res[index] = each(self[elementIndex])
}
// Above code is non blocking so partial exec on most runs
return res
}
/// EZSE : Helper method to get an array of collection indices
private func indicesArray() …Run Code Online (Sandbox Code Playgroud) 所以我有以下函数定义。
def partition[A, B, C](
partitionF: A => Either[B, C])
Run Code Online (Sandbox Code Playgroud)
其中A,B和C是任意类型。
现在,我正在定义要传递的函数
sealed trait Response
case object ThisResponse extends Response
case object ThatResponse extends Response
case object ThisDirection
case object ThatDirection
def responsePartition(response: Response):
Either[ThisDirection, ThatDirection] = response match {
case ThisResponse => Left(ThisDirection)
case ThatResponse => Right(ThatDirection)
}
Run Code Online (Sandbox Code Playgroud)
然后我们将其传递如下
partition(responsePartition)
Run Code Online (Sandbox Code Playgroud)
在业务逻辑中。
现在,我试图单独获取responsePartition中定义的A => B和A => C方法
所以我要找的是
val partitionFonB : A => B = ??? // This is case of this example would be ThisResponse => ThisDirection
Run Code Online (Sandbox Code Playgroud)
和
val …Run Code Online (Sandbox Code Playgroud) 我可以访问
com.amazonaws.services.lambda.runtime.Context;
Run Code Online (Sandbox Code Playgroud)
对象和扩展调用函数Arn.arn包含lambda所在的帐户ID.
我的问题很简单,我希望以最简洁的方式从中提取帐户ID.
我看了看
com.amazon.arn.ARN;
它有很多东西,但没有帐户ID(我认为这是因为并非所有的arns都有帐户ID?)
我想干净地提取帐户ID,而不需要解析字符串.
我是 Rust 新手,正在了解所有权和借用检查的工作原理
我写了这个小函数
pub fn count_words(file_path: &str) -> Result<usize, Error> {
return File::open(file_path).map(|file| {
let reader = BufReader::new(file);
return reader
.lines()
.map(|line| line.unwrap())
.flat_map(|line| line.split_whitespace())
.count();
});
}
Run Code Online (Sandbox Code Playgroud)
我收到这个错误
error[E0515]: cannot return value referencing function parameter `line`
--> src/lib.rs:19:30
|
19 | .flat_map(|line| line.split_whitespace())
| ----^^^^^^^^^^^^^^^^^^^
| |
| returns a value referencing data owned by the current function
| `line` is borrowed here
|
= help: use `.collect()` to allocate the iterator
Run Code Online (Sandbox Code Playgroud)
我不确定我是否理解这里发生的事情(因为哪个实体正在借用行,所以我不能调用分割空白)。
我设法让这个版本运行
pub fn count_words(file_path: &str) …Run Code Online (Sandbox Code Playgroud) 假设我有一堆逻辑上不同的 java 枚举,它们无法合并在一起,因为某些先前的操作依赖于每个独立的枚举。
enum Enum1 {
ENUM1VAL1,
ENUM1VAL2
}
enum Enum2 {
ENUM2VAL1,
ENUM2VAL2
}
void existingOperationOnEnum1(Enum1 enum1);
void existingOperationOnEnum2(Enum2 enum2);
Run Code Online (Sandbox Code Playgroud)
现在假设我希望定义一个新操作来检查字符串是否可以转换为任何这些枚举
void isValidStringConversionForEnums(final String input) {
ENUM1.valueOf(input); // Throws IllegalArgumentException if conversion not possible
ENUM2.valueOf(input); // Throws IllegalArgumentException if conversion not possible
}
Run Code Online (Sandbox Code Playgroud)
所以这很棒,但它不可扩展。业务逻辑是这样的,我们可以在将来创建新的枚举(具有自己的一组枚举特定操作),但是每次添加新枚举时都需要更新 isValidStringConversionForEnums,程序员可能很容易忘记这一点。
所以我的问题是如何修改 isValidStringConversionForEnums 以便它可以与未来的扩展一起使用。
我想到的一种方法是创建一个接口 EnumBase,让 Enum 扩展它,然后使用反射来获取所有 Enum impl,但除非必须,否则我不希望在运行时使用反射。
是否有另一种方法可以编写它,以便我添加一个新的枚举,并以某种方式 isValidStringConversionForEnums 处理该新的枚举。