Comments (14)
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.
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.
The fold and proj operations seem useful, so I've added them to the proposal.
from basislibrary.
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.
I used the following code for the reference implementation:
fun proj (INL x) = x
| proj (INR x) = x
from basislibrary.
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.
+1 for appLeft
and friends; I was going to make the same suggestion.
from basislibrary.
I've updated the proposal with these additional functions
from basislibrary.
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.
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.
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.
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.
The type that you propose for
fold1
looks more like amap
to me.
Well, that's just because either
isn't a recursive data type.
from basislibrary.
The type that you propose for
fold1
looks more like amap
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)
- Regular expressions HOT 5
- Expose ML implementation name in the REPL HOT 1
- Ephemeral resource management HOT 1
- Unsigned integer types HOT 2
- Discussion for proposal 2016-2 HOT 8
- Make `Option` and `List` structures more consistent with each other HOT 9
- Discussion for proposal 2016-003 (Conformance levels) HOT 3
- Add a type t to many signatures HOT 8
- Discussion for proposal 2017-001 (Millisecond sleep)
- Subscript exceptionless signatures HOT 1
- Discussion for proposal 2018-001 (Addition of monomorphic buffers) HOT 6
- Discussion for proposal 2018-002 (Additional slice operations) HOT 1
- Discussion for proposal 2019-001 (Correction to the PRIM_IO signature) HOT 1
- Discussion for proposal 2020-001 (Addition of the Universal module) HOT 9
- ambiguity of Date.date offset larger than 24hours
- ambiguity of offset precision of Date.date
- Discussion of 2021-001 (Add getWindowSz function to Posix.TTY structure)
- Addition of a Void module HOT 4
- Clarify how string conversion functions in signature REAL treat the sign of a NaN value
- Discussion for proposal 2022-001 (Add value tau to MATH signature)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from basislibrary.