FSharpPlus


Bifunctor

Intuitively a bifunctor is a functor with 2 arguments which are covariant.


A bifunctor (short for binary functor) or functor of two variables is simply a functor whose domain is the product of two types.

Minimal complete definition

static member Bimap (x:'Bifunctor<'T,'V>, f:'T->'U, g:'V->'W) :'Bifunctor<'U,'W>

Other operations

static member First (x:Bifunctor<'T,'V>, f:'T->'U) :'Bifunctor<'U,'V>
static member Map (x:Bifunctor<'T,'V>, f:'V->'W) :'Bifunctor<'T,'W>

Rules

bimap f g = first f << second g

Related Abstractions

Concrete implementations of Bifunctor<'T1,'T2>

From .Net/F#

From F#+

Examples

#r @"nuget: FSharpPlus"
open FSharpPlus

// convert (map) first element to an int and the second to a string
let rInt10Str10 = bimap  int string (10.0, 10)


let resOk11  = bimap  ((+) 1) string (Ok 10)
let rStrTrue = first  string (true, 10)
let rStr10   = second string (true, 10)
Multiple items
module Map from Microsoft.FSharp.Collections

--------------------
type Map<'Key,'Value (requires comparison)> = interface IReadOnlyDictionary<'Key,'Value> interface IReadOnlyCollection<KeyValuePair<'Key,'Value>> interface IEnumerable interface IStructuralEquatable interface IComparable interface IEnumerable<KeyValuePair<'Key,'Value>> interface ICollection<KeyValuePair<'Key,'Value>> interface IDictionary<'Key,'Value> new: elements: ('Key * 'Value) seq -> Map<'Key,'Value> member Add: key: 'Key * value: 'Value -> Map<'Key,'Value> ...

--------------------
new: elements: ('Key * 'Value) seq -> Map<'Key,'Value>
namespace FSharpPlus
val rInt10Str10: int * string
val bimap: f: ('T -> 'U) -> g: ('V -> 'W) -> source: 'Bifunctor<'T,'V> -> 'Bifunctor<'U,'W> (requires member Bimap)
<summary> Maps over both arguments of the Bifunctor at the same time. </summary>
<category index="6">Contravariant/Bifunctor/Profunctor/Invariant</category>
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
Multiple items
val string: value: 'T -> string

--------------------
type string = System.String
val resOk11: Result<string,int>
union case Result.Ok: ResultValue: 'T -> Result<'T,'TError>
val rStrTrue: string * int
val first: f: ('T -> 'V) -> source: 'Bifunctor<'T,'V> -> 'Bifunctor<'U,'V> (requires member First)
<summary> Maps covariantly over the first argument of the Bifunctor. </summary>
<category index="6">Contravariant/Bifunctor/Profunctor/Invariant</category>
val rStr10: bool * string
val second: f: ('V -> 'W) -> source: 'Bifunctor<'T,'V> -> 'Bifunctor<'T,'W> (requires member Map)
<summary> Maps covariantly over the second argument of the Bifunctor. </summary>
<category index="6">Contravariant/Bifunctor/Profunctor/Invariant</category>