-
-
Notifications
You must be signed in to change notification settings - Fork 746
Introduce fold to algorithm #1955
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Two relevant bug reports about fold over reduce are: Issue 10670 - std.algorithm.reduce: no-seed initialization wrong design |
std/algorithm.d
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"of of"
What is the purpose of EDIT: to elaborate, I would expect this: [1, 2, 3].fold!min() == 1; // no default seed used
[1].fold!min() == 1; // same as above, no default seed used
[].fold!min(); // runtime assert
[].fold!min(1) == 1; // use provided seed
[1, 2, 3].fold!min(4) == 4; // use provided seed |
std/algorithm.d
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
respectively (no double L).
Is |
std/algorithm.d
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this fail if I try to call foldl1
or foldr1
on an empty range?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant to place an assert here, but yes, it will fail. That's the point (and what reduce
currently does).
On the naming side of things, I think |
I thought of that, but (IMO) that reads awfully complicated. I think it's just as easy to have a named function that does this. It helps keep the interface complete, and is common with haskel's, so consistent cross-language. |
Well... arguably |
The problem isn't empty range, but of knowing whether or the first element should itself be passed through the pred, or used as the first seed. Both scenarios are valid. auto sumSquares = [2, 3, 4].fold"a + b^^2"(); // 0 + 4 + 9 + 16
//NOT 2 + 9 + 16 auto concat = ["hello", "there", "world"].fold1!"a ~ b"(); // "hello there world"
//NOT " hello there world" |
Haskell uses EDIT: I don't like |
Not sure I follow the argument for the |
Maybe you are right. For what it's worth, I converting the One thing I did notice that I like about the way I wrote the code is having a vararg arguments, as opposed to a tuple arg, eg: r.reduce!("a + b", "a * b")(tuple(0.0, 1.0)); //Assuming UFCS
vs
r.fold!("a + b", "a * b")(0.0, 1.0); //Neater, IMO |
TLDR : @Poita I think I agree that simply defaulting to "seed is front when no seed is passed, and dropping fold1" is better. Especially given the existing behavior of To be honest, I haven't found any real usecase for So I think I'll drop them and just leave it at I'll leave this open for a day or two, should anybody else have any suggestions, and then I'll close it to pursue work on inplementation. |
I suppose the elephant in the room is whether or not @andralex is going to be happy with having both |
The issue of reduce and UFCS has been a recurring "problem" that can only be solved with a new named function. It's been a long standing thorn. Also, the idea is that fold is a drop-in replacement for reduce: they aren't meant to co-exist. That, and I think the interface is nicer, which gives the new fold a nice facelift compared to reduce. @andralex : If you have time to read this, your input would be appreciated. |
For now I suggest to add just fold, that acts like reduce, with swapped arguments. More folds can be added later if needed. |
yes, that's the idea. I have the rough code ready, it needs some finishing touches. |
ping |
I've been busy, but my re-implementation of reduce should mean it becomes easier to add fold (as I imagine it) to phobos now. I'll try to get on it. |
Sorry, the current status is that I will not be implementing this, as I am way too busy. That said, the code for reduce should be ready for use with fold directly, and should be relatively easy to do from a code perspective. Passing the review might be harder, are there were (I remember) points of contention. |
I know that pings are annoying, but I ran into this issue on my first day of using D. If that's it, I would be happy to help out and fix the missing bits ;-) |
This is a preliminary rough sketch to add a new "fold" to algorithm. It is designed as a replacement to reduce:
This is a proof of concept submission. I'd be most interested in reviewing the API/design first. If/when we decide the API is good, I'd be more than happy to nitpick away at the code.
Pinging @bearophile : I'm not sure if this is exactly what haskell does, but I think this design makes sense for D? but can you give me your opinion?