FSharpPlus


Contravariant

A Contravariant Functor can be mapped over the input.


One can think of a Functor as containing or producing values, a contravariant functor is a functor that can be thought of as consuming values.

Minimal complete definition

static member Contramap (x:'Contravariant<'T>, f:'U->'T) :'Contravariant<'U>

Rules

contramap id = id
contramap f << contramap g = contramap (g << f)

Related Abstractions

Concrete implementations

From .Net/F#

From F#+

Examples

#r @"nuget: FSharpPlus"
open System
open FSharpPlus


module Predicate = let run (p: Predicate<_>) x = p.Invoke (x)

let intToString (x:int) = string x
let resStr54 = contramap (fun (x:float) -> int x) intToString <| 54.
let isEven      = Predicate (fun x -> x % 2 = 0)
let fstIsEven   = contramap List.head isEven
let resBoolTrue = Predicate.run fstIsEven [0..10]

type Person = Person of string
let personEqComp = HashIdentity.Structural<Person>
let personList = [1, Person "me"; 2, Person "you"; 3, Person "you"]
let cnt3 = Seq.length <| Linq.Enumerable.Distinct (personList)
let cnt2 = Seq.length <| Linq.Enumerable.Distinct (personList, contramap snd personEqComp)

For instance a predicate function from a type to bool. An example of such a function is the predicate that classifies integers as negative:

let negative = Predicate( fun integer -> integer < 0 )

However, given this predicate, we can re-use it in other situations, providing we have a way to map values to integers. For instance, we can use the negative predicate on a person's bank balance to work out if they are currently overdrawn.

let personBankBalance (person:Person) : int = failwith "query persons bank account" 
let overdrawn = contramap personBankBalance negative
val id: x: 'T -> 'T
namespace System
namespace FSharpPlus
type Predicate<'T> = new: object: obj * method: nativeint -> unit member BeginInvoke: obj: 'T * callback: AsyncCallback * object: obj -> IAsyncResult member EndInvoke: result: IAsyncResult -> bool member Invoke: obj: 'T -> bool
<summary>Represents the method that defines a set of criteria and determines whether the specified object meets those criteria.</summary>
<param name="obj">The object to compare against the criteria defined within the method represented by this delegate.</param>
<typeparam name="T">The type of the object to compare.</typeparam>
<returns><see langword="true" /> if <paramref name="obj" /> meets the criteria defined within the method represented by this delegate; otherwise, <see langword="false" />.</returns>
val run: p: Predicate<'a> -> x: 'a -> bool
val p: Predicate<'a>
val x: 'a
Predicate.Invoke(obj: 'a) : bool
val intToString: x: int -> string
val x: int
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 = String
val resStr54: string
val contramap: f: ('U -> 'T) -> x: 'Contravariant<'T> -> 'Contravariant<'U> (requires member Contramap)
<summary> Maps over the input. </summary>
<category index="6">Contravariant/Bifunctor/Profunctor/Invariant</category>
val x: float
Multiple items
val float: value: 'T -> float (requires member op_Explicit)

--------------------
type float = Double

--------------------
type float<'Measure> = float
val isEven: Predicate<int>
val fstIsEven: Predicate<int list>
Multiple items
module List from FSharpPlus
<summary> Additional operations on List </summary>

--------------------
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 ...
val head: list: 'T list -> 'T
val resBoolTrue: bool
Multiple items
union case Person.Person: string -> Person

--------------------
type Person = | Person of string
val personEqComp: Collections.Generic.IEqualityComparer<Person>
module HashIdentity from Microsoft.FSharp.Collections
val Structural<'T (requires equality)> : Collections.Generic.IEqualityComparer<'T> (requires equality)
val personList: (int * Person) list
val cnt3: int
Multiple items
module Seq from FSharpPlus.Operators

--------------------
module Seq from FSharpPlus
<summary> Additional operations on Seq </summary>

--------------------
module Seq from Microsoft.FSharp.Collections
val length: source: 'T seq -> int
Multiple items
namespace System.Linq

--------------------
namespace Microsoft.FSharp.Linq
type Enumerable = static member Aggregate<'TSource> : source: IEnumerable<'TSource> * func: Func<'TSource,'TSource,'TSource> -> 'TSource + 2 overloads static member All<'TSource> : source: IEnumerable<'TSource> * predicate: Func<'TSource,bool> -> bool static member Any<'TSource> : source: IEnumerable<'TSource> -> bool + 1 overload static member Append<'TSource> : source: IEnumerable<'TSource> * element: 'TSource -> IEnumerable<'TSource> static member AsEnumerable<'TSource> : source: IEnumerable<'TSource> -> IEnumerable<'TSource> static member Average: source: IEnumerable<decimal> -> decimal + 19 overloads static member Cast<'TResult> : source: IEnumerable -> IEnumerable<'TResult> static member Chunk<'TSource> : source: IEnumerable<'TSource> * size: int -> IEnumerable<'TSource array> static member Concat<'TSource> : first: IEnumerable<'TSource> * second: IEnumerable<'TSource> -> IEnumerable<'TSource> static member Contains<'TSource> : source: IEnumerable<'TSource> * value: 'TSource -> bool + 1 overload ...
<summary>Provides a set of <see langword="static" /> (<see langword="Shared" /> in Visual Basic) methods for querying objects that implement <see cref="T:System.Collections.Generic.IEnumerable`1" />.</summary>
Linq.Enumerable.Distinct<'TSource>(source: Collections.Generic.IEnumerable<'TSource>) : Collections.Generic.IEnumerable<'TSource>
Linq.Enumerable.Distinct<'TSource>(source: Collections.Generic.IEnumerable<'TSource>, comparer: Collections.Generic.IEqualityComparer<'TSource>) : Collections.Generic.IEnumerable<'TSource>
val cnt2: int
val snd: tuple: ('T1 * 'T2) -> 'T2
val negative: Predicate<int>
val integer: int
val personBankBalance: person: Person -> int
val person: Person
val failwith: message: string -> 'T
val overdrawn: Predicate<Person>