Questions tagged [bifunctor]

A bifunctor is a type constructor with two covariant type parameters that can be "mapped" over. Examples include tuples and Either (a.k.a. Result, disjunction) types.

In , the canonical bifunctors implementation is the bifunctors package by Edward Kmett. It provides the Bifunctor class with several common instances, and a number of other useful modules. The basic functions for working with Bifunctor are first, second and bimap, as seen in the following ghci transcript:

> import Data.Bifunctor
> let pair = (5, "hi")
> first (*6) pair
(30,"hi")
> second (fmap toUpper) pair
(5,"HI")
> bimap (*6) (fmap toUpper) pair
(30,"HI")
24 questions
3
votes
0 answers

Modern haskell implementation of generically derived bifunctors

I'm looking for a way to derive fmapFirst and fmapSecond for bifunctors automatically. I would prefer a way to do it using the new Generic type class or using Data.Data, and without Template Haskell. (Note that I already know that…
tohava
  • 4,982
  • 1
  • 16
  • 44
2
votes
1 answer

What is ZIO error channel and how to get a feeling about what to put in it?

ZIO (https://zio.dev/) is a scala framework which has at its core the ZIO[R, E, A] datastructure and its site gives the following information for the three parameters: ZIO The ZIO[R, E, A] data type has three type parameters: R - Environment Type.…
fanf42
  • 1,638
  • 13
  • 19
2
votes
1 answer

Why doesn't `first` from Data.Bifunctor transform this value

In the following: import Data.Bifunctor import qualified Data.ByteString.Lazy.UTF8 as BLU safeReadFile :: FilePath -> ExceptT Text IO Text safeReadFile p = (lift $ doesFileExist p) >>= bool (throwError "File does not exist") (lift $ pack <$>…
Cameron Ball
  • 3,960
  • 5
  • 22
  • 30
1
vote
1 answer

Why is Data.Map.Map not a Bifunctor?

When storing data in a Map, I was recently looking for a Bitraversable instance, since I wanted to traverse over both keys and values. To my surprise, I found out that Map does not even have a Bifunctor instance, even though implementations for both…
Prophet
  • 97
  • 5
1
vote
1 answer

Merge function in ConcurrentHashMap

Got a question about the merge function with ConcurrentHashMaps. New to functional programming so not sure if I'm utilizing it…
SS'
  • 669
  • 1
  • 5
  • 14
1
vote
1 answer

Instance declaration with type variables

Writing something like this works fine: data Either a b = Left a | Right b instance Functor (Either a) where fmap _ (Left x) = Left x fmap f (Right x) = Right (f x) Now lets say I want to invert this, Left applies f to value: instance…
Pireax
  • 103
  • 2
  • 7
1
vote
1 answer

On inferring fmap for ADTs

Suppose that two new types are defined like this type MyProductType a = (FType1 a, FType2 a) type MyCoproductType a = Either (FType1 a) (FType2 a) ...and that FType1 and Ftype2 are both instances of Functor. If one now were to declare…
kjo
  • 27,601
  • 42
  • 124
  • 225
0
votes
0 answers

Implicit parameter for BiFunctor

I'm planning to use Bifunctor IO with a channel for errors. So I tried to provide an implicit instance for MonadError but it fails to compile. import cats.MonadError sealed trait AppErrors //... object App{ def runApp[F[_, _]:…
St.Antario
  • 23,179
  • 26
  • 96
  • 243
0
votes
1 answer

Parametrized type alias for Bifunctors

I have a Seq[R] and I wanna split this into a Tuple2[Seq[E], Seq[S]], while I was coding this I thought about the fact that I could use a custom Bifunctor for a tuple of seqs and as exercise tried to code this: import scalaz.Bifunctor type…
Ende Neu
  • 14,921
  • 5
  • 50
  • 67
1
2