返回不像我期望的那样
我正在解决问题
这是我首先提出的解决方案
using namespace System.Linq
$DebugPreference = "Continue"
class kata{
static [string] FirstNonRepeatingLetter ([String]$string_){
[Enumerable]::GroupBy([char[]]$string_, [func[char, char]]{$args[0]}).Foreach{
if ($_.Count -eq 1){
Write-Debug ($_.key)
return $_.Key
}
}
return "~~~"
}
}
Write-Debug ([kata]::FirstNonRepeatingLetter("stress"))
回来了
DEBUG: t
DEBUG: r
DEBUG: e
DEBUG: ~~~
这不是我所期望的 我试试这个
using namespace System.Linq
$DebugPreference = "Continue"
class kata{
static [string] FirstNonRepeatingLetter ([String]$string_){
$groups = [Enumerable]::GroupBy([char[]]$string_, [func[char, char]]{$args[0]})
foreach ($group in $groups)
{
if ($group.Count -eq 1){
return $group.Key
}
}
return "~~~"
}
}
Write-Debug ([kata]::FirstNonRepeatingLetter("stress"))
然后是我想看的
DEBUG: t
现在我不明白为什么这些代码的工作方式不同......
回答
您正在传递给数组方法方法return的脚本块( { ... }) 中执行:.ForEach()
实际上没有直接的方法来停止枚举.ForEach()(相比之下,相关的.Where()数组方法有一个可选参数,它接受一个'First' 以在找到第一个匹配项后停止枚举)。
您有两个选择:
-
- 这样做的明显缺点是枚举总是运行到完成。
-
- 注意:如果没有虚拟循环,
break将在整个调用堆栈中查找要跳出的循环;如果没有找到,则执行将整体终止-有关更多信息,请参阅此答案。
- 注意:如果没有虚拟循环,
以下是如何实现虚拟循环方法:
using namespace System.Linq
$DebugPreference = 'Continue'
class kata{
static [string] FirstNonRepeatingLetter ([String]$string_){
$result = '~~~'
do { # dummy loop
[Enumerable]::GroupBy([char[]]$string_, [func[char, char]]{$args[0]}).Foreach{
if ($_.Count -eq 1){
Write-Debug ($_.key)
$result = $_.Key
break # break out of the dummy loop
}
}
} while ($false)
return $result
}
}
Write-Debug ([kata]::FirstNonRepeatingLetter('stress'))