Given a string of numbers, I would like to have a sequence of tuples displaying non-zero characters with their position in the string. Example:
IN: "000140201" OUT: { (3, '1'); (4, '4'); (6, '2'); (8, '1') }
Decision:
let tuples = source |> Seq.mapi (fun i -> fun c -> (i, c)) |> Seq.filter (snd >> (<>) '0')
It seems that (fun i -> fun c -> (i, c)) is much more typed than it should be for such a simple and supposedly general operation. It is easy to declare the desired function:
let makeTuple ab = (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 a shorter name), or at least it should be relatively easy to create. I could not find him; Am I missing something? I tried to create something with a Tuple.Create framework, but I couldn't figure out how to get anything other than one-factor overload.
source share