Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# pipeline placeholder?

I have googlet a bit, and I haven't found what I was looking for. As expected. My question is, is it possible to define a F# pipeline placeholder? What I want is something like _ in the following:

let func a b c = 2*a + 3*b + c

2 |> func 5 _ 6

Which would evaluate to 22 (2*5 + 3*2 + 6).

For comparison, check out the magrittr R package: https://github.com/smbache/magrittr

like image 324
torbonde Avatar asked Feb 14 '14 12:02

torbonde


2 Answers

This is (unfortunately!) not supported in the F# language - while you can come up with various fancy functions and operators to emulate the behavior, I think it is usually just easier to refactor your code so that the call is outside of the pipeline. Then you can write:

let input = 2
let result = func 5 input 6

The strength of a pipeline is when you have one "main" data structure that is processed through a sequence of steps (like list processed through a sequence of List.xyz functions). In that case, pipeline makes the code nicer and readable.

However, if you have function that takes multiple inputs and no "main" input (last argument that would work with pipelines), then it is actually more readable to use a temporary variable and ordinary function calls.

like image 123
Tomas Petricek Avatar answered Nov 17 '22 16:11

Tomas Petricek


I don't think that's possible, but you could simply use a lambda expression, like

2 |> (fun b -> func 5 b 6)
like image 2
sloth Avatar answered Nov 17 '22 17:11

sloth