Giter VIP home page Giter VIP logo

Comments (14)

igstan avatar igstan commented on August 26, 2024 1

I'm a bit late to this, but... why does Either.fold also take a 'b as argument? I think it should be just:

val fold : ('left -> 'b) * ('right -> 'b) -> ('left, 'right) either -> 'b

The "accumulator" argument makes sense for list and option, which both exhibit an empty variant, but not so much for Either.

from basislibrary.

MatthewFluet avatar MatthewFluet commented on August 26, 2024

A fold and a projection function might be useful:

val fold : ('left * 'b -> 'b) * ('right * 'b -> 'b) -> 'b -> ('left, 'right) either -> 'b
fun fold (fl, fr) init sum = (case sum
       of INL x => fl (x, init)
        | INR x => fr (x, init)
      (* end case *))

val proj : ('a, 'a) either -> 'a
fun proj sum = fold (fn (x,_) => x, fn (x,_) => x) () sum

from basislibrary.

JohnReppy avatar JohnReppy commented on August 26, 2024

The fold and proj operations seem useful, so I've added them to the proposal.

from basislibrary.

MatthewFluet avatar MatthewFluet commented on August 26, 2024

My implementation of proj doesn't type check; I guess it would need a direct implementation:

fun proj sum = case sum of INL x => x | INR x => x

from basislibrary.

JohnReppy avatar JohnReppy commented on August 26, 2024

I used the following code for the reference implementation:

fun proj (INL x) = x
  | proj (INR x) = x

from basislibrary.

rossberg-old avatar rossberg-old commented on August 26, 2024

In Alice ML we had this under the name of ('a, 'b) alt. Besides fold, a set of additional functions that turned out useful was:

val appLeft :  ('a -> unit) -> ('a,'b) either -> unit
val appRight : ('b -> unit) -> ('a,'b) either -> unit
val mapLeft :  ('a -> 'c) -> ('a,'b) either -> ('c,'b) either
val mapRight : ('b -> 'c) -> ('a,'b) either -> ('a,'c) either

from basislibrary.

MatthewFluet avatar MatthewFluet commented on August 26, 2024

+1 for appLeft and friends; I was going to make the same suggestion.

from basislibrary.

JohnReppy avatar JohnReppy commented on August 26, 2024

I've updated the proposal with these additional functions

from basislibrary.

igstan avatar igstan commented on August 26, 2024

Additionally, another useful function might be:

val swap : ('a, 'b) either -> ('b, 'a) either

Though probably less so if both mapLeft and mapRight are present.

from basislibrary.

JohnReppy avatar JohnReppy commented on August 26, 2024

The type that you propose for fold1 looks more like a map to me. I think that there is some advantage in keeping the "accumulator" argument to fold, since it might be used in a fold over a sequence, but perhaps the type should be

val fold : ('left * 'b -> 'b) * ('right * 'b -> 'b) -> ('left, 'right) either * 'b -> 'b

to make such usage easier (although that breaks the common shape of fold functions in the Basis Library). The swap function seems like a useful addition.

from basislibrary.

igstan avatar igstan commented on August 26, 2024

I have gotten used to see fold as the catamorphism (or Church encoding, maybe) of a data structure, where each of the function arguments mimics one of the data structure's constructors — an alternative to pattern matching. This is the only reason it seemed weird at first sight.

Regardless of the big words, how would someone use fold as currently implemented when they don't have that "accumulator" available?

from basislibrary.

igstan avatar igstan commented on August 26, 2024

Actually, disregard my last question. If they don't have it, they will not use it, but this will probably limit fold's applicability.

from basislibrary.

tjark avatar tjark commented on August 26, 2024

The type that you propose for fold1 looks more like a map to me.

Well, that's just because either isn't a recursive data type.

from basislibrary.

pclayton avatar pclayton commented on August 26, 2024

The type that you propose for fold1 looks more like a map to me.

Well, that's just because either isn't a recursive data type.

The proposed type for fold1 suggests the definition
fun fold1 f = proj o map f
so it's not surprising it looks like a map.

from basislibrary.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.