Hi Marco,
Most of Spire's type classes can't correctly implement map. Let's look
at a simple example, Monoid[A], and see if we can do it.
trait Monoid[A] { self =>
def id: A // abstract
def op(x: A, y: A): A // abstract
def map[B](f: A => B): Monoid[B] =
new Monoid[B] {
def id: B = f(
self.id)
def op(x: B, y: B): B =
f(self.op(x??, x??))
}
}
This would work except for the x?? and y?? parts. In order to use the
underlying Monoid to implement combine we also need a way to go from
(B => A), so we can convert the mapped combine's arguments into A
values so we can use them with the original Monoid.
We *could* give these types a method like imap:
trait Monoid[A] { self =>
def id: A // abstract
def op(x: A, y: A): A // abstract
def imap[B](f: A => B)(g: B => A): Monoid[B] =
new Monoid[B] {
def id: B = f(
self.id)
def op(x: B, y: B): B =
f(self.op(g(x), g(y)))
}
}
Does this make sense?
To generalize this a bit (and move from Spire to Cats), we can
characterize any type that supports a map method as having a
cats.Functor (specificallly, a covariant functor). Types like
spire.algebra.Monoid do not have covariant functors, but they do have
a cats.functor.Invariant (an invariant functor) which has the more
restrictive imap method.
Spire itself does not currently put an imap method on these type
classes, but I would be open to adding it. Also, if you are using
Cats, you could define an instance of Invariant[Field] (for example)
which would give you access to .imap via enrichment.
-- Erik
> --
> You received this message because you are subscribed to the Google Groups "Typelevel Users & Development List" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
typelevel+...@googlegroups.com.
> To post to this group, send email to
type...@googlegroups.com.
> To view this discussion on the web visit
https://groups.google.com/d/msgid/typelevel/f56d753d-230d-4e7d-b6df-e240f05dff4d%40googlegroups.com.
> For more options, visit
https://groups.google.com/d/optout.