Is there any syntax in F# which allows pipelining by a list of functions in sequence concisely? For example,
x |> fun1 |> fun2 |> fun3 ...
Or is there a design pattern which makes this task unnecessary? In my case I am making a (naive) Sudoku solver and have a function that looks like this:
let reduceByRows poss =
poss
|> reduceBy (rowIndeces 1) |> reduceBy (rowIndeces 2) |> reduceBy (rowIndeces 3)
|> reduceBy (rowIndeces 4) |> reduceBy (rowIndeces 5) |> reduceBy (rowIndeces 6)
|> reduceBy (rowIndeces 7) |> reduceBy (rowIndeces 8) |> reduceBy (rowIndeces 9)
Is there any way to clean up something like this?
One way to look at this is folding on pipelining operator |>
instead of folding on data:
{1..9} |> Seq.map (rowIndices >> reduceBy)
|> Seq.fold (|>) poss
In general, if fun1
, fun2
, etc have the same signature, you can apply |>
on a sequence of functions i.e. repeated pipelining:
[
fun1;
fun2;
fun3;
//...
] |> List.fold (|>) x
Looks like a fold to me. What about
let reduceByRows poss =
Seq.fold (fun p i -> reduceBy (rowIndices i) p) poss {1..9}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With