2 people like it.

Pipeline list processing

An example showing how to process list in a pipeline. We first use List.filter to return only even numbers and then use List.map to format them as strings.

Filtering and projection

1: 
2: 
3: 
4: 
5: 
6: 
7: 
let isEven n = n%2 = 0
let formatInt n = (string n) + "N"

let res = 
  [ 1 .. 10 ] 
  |> List.filter isEven
  |> List.map formatInt
val isEven : n:int -> bool
val n : int
val formatInt : n:'a -> string
val n : 'a
Multiple items
val string : value:'T -> string

--------------------
type string = System.String
val res : string list
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) 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
    ...
val filter : predicate:('T -> bool) -> list:'T list -> 'T list
val map : mapping:('T -> 'U) -> list:'T list -> 'U list

More information

Link:http://fssnip.net/u
Posted:2 months ago
Author:Tomas Petricek
Tags: pipeline , list , filter , map