[英]Is it possible to choose where the pipe output is inserted into Elixir function args?
[英]Elixir: pipe more then one variable into a function
Elixir可以通過管道將輸入傳遞到函數中,這使得代碼更易於閱讀。
例如這樣的事情
sentence |> String.split(@wordSplitter, trim: true)
它將String sentence
通過管道String.split
到String.split
的第一個參數中。
現在考慮我還想將第二個參數傳遞給String.split
。 有可能在Elixir中這樣做嗎? 我的意思是這樣的:
sentence, @wordSplitter |> String.split(trim: true)
謝謝!
正如@Dogbert指出的那樣,這是不可能的。 但是,該助手非常簡單:
defmodule MultiApplier do
def pipe(params, mod, fun, args \\ []) do
apply(mod, fun, List.foldr(params, args, &List.insert_at(&2, 0, &1)))
end
end
iex> ["a b c", " "]
...> |> MultiApplier.pipe(String, :split, [[trim: true]])
#⇒ ["a", "b", "c"]
iex> ["a b c", " ", [trim: true]]
...> |> MultiApplier.pipe(String, :split, [])
#⇒ ["a", "b", "c"]
iex> ["a b c"]
...> |> MultiApplier.pipe(String, :split, [" ", [trim: true]])
#⇒ ["a", "b", "c"]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.