cat/instances/functor

Functor instances: Option, List, Reader, Writer, Const, Tuple, Triple, Pair, Either.

Types

Phantom type for Const Functor.
We bind the first parameter of Const.

pub type ConstF(c)

Phantom type for Either Functor.

pub type EitherF(a)

Phantom type for Function Functor.

pub type FunctionF(r)
pub type IdentityF

Phantom type for List Functor.

pub type ListF

Phantom type for Option Functor.

pub type OptionF

Phantom type for Pair Functor.

pub type PairF(a)

Phantom type for Reader Functor.

pub type ReaderF(r)

Phantom type for Triple Functor.

pub type TripleF(a, b)

Phantom type for Tuple Functor.

pub type TupleF(a)

Phantom type for Writer Functor.

pub type WriterF

Functions

pub fn const_functor() -> Functor(
  ConstF(a),
  b,
  c,
  Const(a, b),
  Const(a, c),
)

Const Functor Instance.

// Haskell instance
instance Functor (Const c) where
    fmap :: (a -> b) -> Const c a -> Const c b
    fmap _ (Const v) = Const v  
pub fn either_functor() -> Functor(
  EitherF(a),
  b,
  c,
  Either(a, b),
  Either(a, c),
)

Either Functor.

Examples

either_functor().fmap(bool.negate)(Left(27))
// -> Left(27)
either_functor().fmap(bool.negate)(Right(False))
// -> Right(True)
pub fn function_functor() -> Functor(
  FunctionF(a),
  b,
  c,
  fn(a) -> b,
  fn(a) -> c,
)

(->) Functor Instance.

// Haskell instance
instance Functor ((->) r) where
    fmap :: (a -> b) -> (r -> a) -> (r -> b)
    fmap f g = f ∘ g

Examples

let f = fn(x) { x % 2 == 0 }
let g = bool.to_string

function_functor().fmap(g)(f)(19)
// -> "False"
pub fn identity_functor() -> Functor(
  IdentityF,
  a,
  b,
  Identity(a),
  Identity(b),
)

Identity Functor Instance.

// Haskell implementation
instance Functor Maybe where
    fmap :: (a -> b) -> Identity a -> Identity b
    fmap f (Identity x) = Identity(f x)

Examples

let f = fn(x: Int) -> Bool { x % 2 == 0 }
identity_functor().fmap(f)(Identity(5))
// -> Identity(False)
pub fn list_functor() -> Functor(ListF, a, b, List(a), List(b))

List Functor Instance.

// Haskell instance
instance Functor [] where
    fmap :: (a -> b) -> [a] -> [b]
    fmap _ [] = []
    fmap f (x:xs) = (f x):(fmap f xs)
pub fn option_functor() -> Functor(
  OptionF,
  a,
  b,
  Option(a),
  Option(b),
)

Option Functor Instance (generic over a and b).

// Haskell instance
instance Functor Maybe where
    fmap :: (a -> b) -> Maybe a -> Maybe b
    fmap _ Nothing = Nothing
    fmap f (Just x) = Just (f x)

Examples

let double = fn(x) { x * 2 }
option_functor().fmap(double)(None)
// -> None
Some(2)
|> option_functor().fmap(double)
// -> Some(4)
pub fn pair_functor() -> Functor(
  PairF(a),
  b,
  c,
  Pair(a, b),
  Pair(a, c),
)

Pair Functor.

Examples

pair_functor().fmap(bool.negate)(Pair(9, False))
// -> Pair(9, True)
pub fn reader_functor() -> Functor(
  ReaderF(a),
  b,
  c,
  Reader(a, b),
  Reader(a, c),
)

Reader Functor Instance.

// Haskell implementation
instance Functor (Reader r) where
  fmap :: (a -> b) -> Reader r a -> Reader r b
  fmap f g = f ∘ g

Examples

let ra = Reader(fn(x) { x % 2 == 0 })
let f = bool.to_string

reader_functor().fmap(f)(ra).apply(19)
// -> "False"
pub fn triple_functor() -> Functor(
  TripleF(a, b),
  c,
  d,
  #(a, b, c),
  #(a, b, d),
)

Triple Functor.

Examples

triple_functor().fmap(bool.negate)(#("abc", 9, False))
// -> #("abc", 9, True)
pub fn tuple_functor() -> Functor(
  TupleF(a),
  b,
  c,
  #(a, b),
  #(a, c),
)

Tuple Functor.

Examples

tuple_functor().fmap(bool.negate)(#(9, False))
// -> #(9, True)
pub fn writer_functor() -> Functor(
  WriterF,
  a,
  b,
  Writer(a),
  Writer(b),
)

Writer Functor Instance.

// Haskell Instance
fmap :: (a -> b) -> Writer a -> Writer b
fmap f = id >=> (\x -> return (f x))

Examples

monad.Writer(16, "message")
|> writer_functor().fmap(fn(x) { x % 4 == 0 })
// -> monad.Writer(True, "message")
Search Document