FSharp.Core.Fluent is a collection of inlined methods allowing fluent access to all FSharp.Core functions for List, Array, Array2D, Array3D, Seq, Option, String, Event and Observable.
For example, this library adds .map
, .filter
and many other methods for lists, arrays and sequences:
open FSharp.Core.Fluent
let xs = [ 1 .. 10 ]
xs.map(fun x -> x + 1).filter(fun x -> x > 4).sort()
xs.map(fun x -> x + 1)
.filter(fun x -> x > 4)
.sort()
Comparison with non-Fluent style
F# code normally uses curried module functions to access functionality for collections, composed in pipelines:
xs
|> List.map (fun x -> x + 1)
|> List.filter (fun x -> x > 4)
There are reasons F# uses this style of programming by default:
for example, module functions can compose nicely (e.g. xs |> List.map (List.map f)
).
However "fluent" access can be convenient, especially in rapid investigative programming
against existing data. For this reason, this option makes fluent notation an option.
In almost all case, xs.OP(arg)
is equivalent to the pipelined xs |> Coll.OP arg
. So
you can freely interconvert betweeen
xs
|> List.map (fun x -> x + 1)
|> List.filter (fun x -> x > 4)
and
xs.map(fun x -> x + 1)
.filter(fun x -> x > 4)
You can also use pipeline operations after fluent operations:
xs
.map(fun x -> x + 1)
|> List.filter(fun x -> x > 4)
|> Array.ofList
Or use the generic .pipe method to simulate |>
(4.0).pipe(sqrt)
You can't mix pipelining followed by fluent, and attempting to do so can give obscure errors:
xs
|> List.map(fun x -> x + 1)
.filter(fun x -> x > 4) // ERROR: The field or constructor "filter" is not defined
NOTE: append
does the natural thing in fluent form
In the the case of xs.append(ys)
, the result is "xs
then ys
" - as expected.
However this is different to xs |> List.append ys
, which is actually ys
then xs
due to the way
pipelining and currying works.
Usage examples
See this documentation for examples of using a wide range of the functions.
Contributing and copyright
The project is hosted on GitHub where you can report issues, fork the project and submit pull requests.
The library is available under MIT license. For more information see the License file in the GitHub repository.
namespace FSharp
--------------------
namespace Microsoft.FSharp
namespace FSharp.Core
--------------------
namespace Microsoft.FSharp.Core
member List.map: mapping: ('T -> 'a) -> 'a list
module List from Microsoft.FSharp.Collections
--------------------
type List<'T> = | op_Nil | op_ColonColon of Head: 'T * Tail: 'T list interface IReadOnlyList<'T> interface IReadOnlyCollection<'T> interface IEnumerable interface IEnumerable<'T> member GetReverseIndex: rank: int * offset: int -> int member GetSlice: startIndex: int option * endIndex: int option -> 'T list static member Cons: head: 'T * tail: 'T list -> 'T list member Head: 'T member IsEmpty: bool member Item: index: int -> 'T with get ...