Skip to content

Validation as a ZipFunctor? #280

Closed
Closed

Description

@NickDarvey

I'm not even sure if I'm phrasing this right, but, is Validation a ZipFunctor? And would this be a valid implementation of it?

module Validation =
  let zip = liftA2 tuple2

I can open a PR if it actually is a zippable functor.

Activity

  1. gusty commented on Feb 25, 2020

    @gusty
    Member

    That's a very interesting question.

    Unfortunately I think the answer is No and I'll tell you why:

    Let's suppose Validation is a ZipFunctor which means that it has a zip operation which is the inverse of Functor's unzip (which is defined as unzip s = map fst s, map snd s).

    Then we can do:

    > zip (Validation<string,int>.Success 42) (Validation<string,float>.Failure "Error") ;;
    val it : Validation<string,(int * float)> = Failure "Error"
    

    and now

    > unzip it ;;
    val it : Validation<string,int> * Validation<string,float> =
      (Failure "Error", Failure "Error")
    

    but now

    > it ||> zip;;
    val it : Validation<string,(int * float)> = Failure "ErrorError"
    

    so we came back to a different value !

    Note however that Result seems to be well behaved in this regard:

    > zip (Result<int,string>.Ok 42) (Result<float,string>.Error "No") ;;
    [<Struct>]
    val it : Result<(int * float),string> = Error "No"
    
    > unzip it ;;
    val it : Result<int,string> * Result<float,string> = (Error "No", Error "No")
    
    > it ||> zip;;
    [<Struct>]
    val it : Result<(int * float),string> = Error "No"
    

    Does it makes sense?

  2. NickDarvey commented on Feb 25, 2020

    @NickDarvey
    ContributorAuthor

    That does make sense. Thank you for the accessible explanation @gusty 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions