大約有路過的功能通Cmdlet的作爲參數傳遞給函數
function pass_function([scriptblock] $func, [int] $a){
func.invoke($a)
}
你將如何通過接受管道輸入的cmdlet幾個很好的答案?我有一個貧窮的解決方案
function pass_through([scriptblock]$command){
$command.invoke()
}
1,2,3,4 | pass_through { $input | Where { $_ -gt 1} }
輸出2,3,4
技術上有足夠的工具在那裏,但他們需要實現擺弄。我寧願傳遞Where和{$ _ -gt 1}作爲單獨的參數。
如果沒有很好的支持,這種語言的解決類似問題的風格是什麼?
能不能細說你爲什麼做這種方式?元編程很有趣,但我很好奇。爲什麼你不能製作一個專用的函數來接受你想要的參數......處理並繼續處理管道。 – Matt
爲簡單起見,我們選擇了這個例子。但是假設我有一個「日誌記錄」工具,用於測試CmdLet對管道輸入的影響。因此,我們可能有 「input | remember | chosenCmdLet | difference」 其中「remember」可能以某種方式標記值,並且當流中的元素被刪除或更改時,「差異」會發出信號。如果我可以調用Test chosenCmdLet而不是在這兩個Cmdlet之間調用它,那將會很好。 – Polymer
@Polymer函數測試{param([ScriptBlock] $ Command)輸入|記住| Invoke-Command $ Command | difference}''Test {Where {$ _ -gt 1}}' – PetSerAl