Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine functionality and Pipeline operator in F#

Tags:

f#

pipeline

I'm working on a project and I want to create a really compact method for creating Entities and Attributes.

I want to do this with the pipeline operator. But I want to add extra functionality to this operator.

Like for example :

let entity = (entity "name")
                 |>> (attribute "attr" String)
                 |>> (attribute "two"  String)

In this example |>> would be a pipeline operator together with the functionality to add an attribute to the entity.

I know that this works:

let entity = (entity "name")
             |> addAttr (attribute "attr" String)

So what I want to know is, if it's possible to replace

|> addAttr

with

|>> 

Thanks for the help

(I don't know if this is even possible)

like image 907
BrechtL Avatar asked Nov 07 '16 15:11

BrechtL


2 Answers

You can simply define it like this:

let (|>>) e a = e |> addAttr a
like image 121
Tarmil Avatar answered Oct 25 '22 12:10

Tarmil


For readability, I would strongly discourage adding custom operators when a simple function will do. You could change the way addAttr is written to make it easier to use in a pipeline:

let addAttr name attrType entity = () // return an updated entity

let e =
    entity "name"
    |> addAttr "attr" String
    |> addAttr "two"  String
like image 24
TheQuickBrownFox Avatar answered Oct 25 '22 13:10

TheQuickBrownFox