数字の文字列が与えられた場合、ゼロ以外の文字を文字列内の位置にマッピングする一連のタプルが必要です。例:
IN: "000140201"
OUT: { (3, '1'); (4, '4'); (6, '2'); (8, '1') }
解決:
let tuples = source
|> Seq.mapi (fun i -> fun c -> (i, c))
|> Seq.filter (snd >> (<>) '0')
このような(fun i -> fun c -> (i, c))
単純でおそらく一般的な操作では、本来よりもはるかに多くのタイピングが行われているようです。必要な関数を宣言するのは簡単です:
let makeTuple a b = (a, b)
let tuples2 = source
|> Seq.mapi makeTuple
|> Seq.filter (snd >> (<>) '0')
But it seems to me that if the library provides the snd
function, it should also provide the makeTuple
function (and probably with a shorter name), or at least it should be relatively easy to compose. I couldn't find it; am I missing something? I tried to build something with the framework's Tuple.Create, but I couldn't figure out how to get anything other than the single-argument overload.