origamitower / folktale

[not actively maintained!] A standard library for functional programming in JavaScript
https://folktale.origamitower.com/
MIT License
2.05k stars 102 forks source link

bimap unwraps values before passing to the left or right functions #219

Closed EarthyOrange closed 5 years ago

EarthyOrange commented 5 years ago

The bimap function unwraps the value present in the Result container, before passing it to the left or right functions.

Steps to reproduce

import * as R from 'ramda';
import Result from 'folktale/result';
import bimap from 'folktale/fantasy-land/bimap';

const leftF = R.pipe(
  R.tap(console.log),
);
console.log(bimap.curried(leftF, R.identity)(Result.Error('error')));

Expected behaviour

It should pass the container, as is, to the left or right function.

Environment

robotlolita commented 5 years ago

bimap is more related to the map function (where map is a functor, bimap is a bifunctor), so the current behaviour is the expected behaviour. You can see it from the types:

map :: Functor f => (f a) . (a -> b) -> f b
bimap :: Bifunctor f => (f a b) . (a -> c, b -> d) -> f c d

So each function takes in a value from inside the functor (or bifunctor in this case), transforms it, and the operation repackages that into a new functor (or bifunctor).

What's your use case? If you need to operate at the container level, but branching depending on the state of the container, you could try using the .matchWith operation:

Result.error('error').matchWith({
  Error: (x) => /* do something with the container x */,
  Ok: (x) => /* do something with the container x */
});

Other than that, I don't think we have many operations on the container. It's not something I've missed personally, but maybe you have a different use case.