Data structures that can be traversed from left to right, performing an action on each element.
traverse f x
| sequence x
static member Traverse (t: 'Traversable<'T>, f: 'T -> 'Applicative<'U>) : 'Applicative<'Traversable<'U>>
static member Sequence (t: 'Traversable<'Applicative<'T>>) : 'Applicative<'Traversable<'T>>
gather f x
| transpose x
(same as traverse
and sequence
but operating on ZipApplicatives )
static member Gather (t: 'Traversable<'T>, f: 'T -> 'ZipApplicative<'U>) : 'ZipApplicative<'Traversable<'U>>
static member Transpose (t: 'Traversable<'ZipApplicative<'T>>) : 'ZipApplicative<'Traversable<'T>>
t << traverse f = traverse (t << f)
traverse Identity = Identity
traverse (Compose << map g << f) = Compose << map (traverse g) << traverse f
- Functor: A traversable is generic on the Traversable type parameter and the (Applicative) Functor inner type parameter.
- Applicative: An applicative is a functor whose
map
operation can be splitted in return
and (<*>)
operations.
- Foldable : All traversables are foldables.
From .Net/F#
seq<'T>
list<'T>
array<'T>
option<'T>
voption<'T>
ResizeArray<'T>
Map<'K, 'T>
Result<'T, 'Error>
Choice<'T, 'Error>
From F#+
Suggest another concrete implementation
open FSharpPlus
// Some functions
let getLine = async { return System.Console.ReadLine () }
let f x = if x < 200 then [3 - x] else []
let g x = if x < 200 then Some (3 - x) else None
// traverse
let resSomeminus100 = traverse f (Some 103)
let resLstOfNull = traverse f None
let res210 = traverse f [1; 2; 3]
let resSome210 = traverse g [1; 2; 3]
let resEmptyList = traverse f [1000; 2000; 3000]
let resEListOfElist = traverse f []
// sequence
let resSome321 = sequence [Some 3; Some 2; Some 1]
let resNone = sequence [Some 3; None ; Some 1]
let res654 = (sequence [(+) 3; (+) 2; (+) 1]) 3
let resCombined = sequence [ [1; 2; 3] ; [4; 5; 6] ]
let resLstOfArr = sequence [|[1; 2; 3] ; [4; 5; 6] |] // <- Uses the default method.
let resArrOfLst = sequence [[|1; 2; 3|]; [|4; 5; 6 |]]
// This computation will ask for three user inputs
// try Async.RunSynchronously get3strings
let get3strings = sequence [getLine; getLine; getLine]
-
Highly recommended Matt Thornton's blog Grokking Traversable.
It contains examples using F#+ and an explanation from scratch.
namespace FSharpPlus
val getLine: Async<string>
val async: AsyncBuilder
namespace System
type Console =
static member Beep: unit -> unit + 1 overload
static member Clear: unit -> unit
static member GetCursorPosition: unit -> struct (int * int)
static member MoveBufferArea: sourceLeft: int * sourceTop: int * sourceWidth: int * sourceHeight: int * targetLeft: int * targetTop: int -> unit + 1 overload
static member OpenStandardError: unit -> Stream + 1 overload
static member OpenStandardInput: unit -> Stream + 1 overload
static member OpenStandardOutput: unit -> Stream + 1 overload
static member Read: unit -> int
static member ReadKey: unit -> ConsoleKeyInfo + 1 overload
static member ReadLine: unit -> string
...
<summary>Represents the standard input, output, and error streams for console applications. This class cannot be inherited.</summary>
System.Console.ReadLine() : string
val f: x: int -> int list
val x: int
val g: x: int -> int option
union case Option.Some: Value: 'T -> Option<'T>
union case Option.None: Option<'T>
val resSomeminus100: int option list
val traverse: f: ('T -> 'Functor<'U>) -> t: 'Traversable<'T> -> 'Functor<'Traversable<'U>> (requires member Traverse)
<summary>
Map each element of a structure to an action, evaluate these actions from left to right, and collect the results.
</summary>
<category index="13">Traversable</category>
val resLstOfNull: int option list
val res210: int list list
val resSome210: int list option
val resEmptyList: int list list
val resEListOfElist: int list list
val resSome321: int list option
val sequence: t: 'Traversable<'Functor<'T>> -> 'Functor<'Traversable<'T>> (requires member Sequence)
<summary>
Evaluate each action in the structure from left to right, and collect the results.
</summary>
<category index="13">Traversable</category>
val resNone: int list option
val res654: int list
val resCombined: int list list
val resLstOfArr: int array list
val resArrOfLst: int list array
val get3strings: Async<string list>