feat: allow making a weighted container ordered#320
Draft
LordMidas wants to merge 2 commits intodevelopmentfrom
Draft
feat: allow making a weighted container ordered#320LordMidas wants to merge 2 commits intodevelopmentfrom
LordMidas wants to merge 2 commits intodevelopmentfrom
Conversation
The primary purpose is to allow a `foreach (item in container)` type iteration instead of the standard `foreach (item, weight in container)`. This helps the container to be used interchangeably with arrays without having to do a typeof check and duplicate foreach loops separately for arrays and weighted containers.
Member
|
We discussed and this is cool but as you said it should be a sibling of WeightedContainer and seems like it requires still quite a bit of work |
Contributor
|
If you don't really care about order but just want to iterate over class WeightedContainer {
...
function items() {
foreach (k, _ in this.Table) yield k;
}
}
// Use
local items = type cont == "array" ? cont : cont.items();
foreach (item in items) {
// ... do stuff ...
}Unfortunately typeof/items cannot be removed completely because |
Contributor
|
Another approach is to provide external to // Util.iter() supposed to know what "items" mean for different types of containers,
// i.e. typeof and iterator will be hidden there.
foreach (item in Util.iter(cont)) {
// ... do stuff ...
} |
6677ac8 to
ae825b7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The primary purpose is to allow a
foreach (item in container)type iteration andforloop iteration with indices instead of the currentforeach (item, weight in container). This helps the container to be used interchangeably with arrays without having to do atypeofcheck and duplicateforeachloops separately for arrays and weighted containers.I ran into this issue while coding the Dynamic Spawns Framework whereby the same member variable in a class could be an array or a weighted container. In many functions I want to iterate over that variable in
foreachloops and sometimes inforloops with usingias index while being agnostic to its type. Having the possibility of an ordered weighted container allows me to set it as ordered during creation, and then the class can do its thing while not worrying about it.While, in theory, we could implement a child class of WeightedContainer that is ordered (I did that in Dynamic Spawns and called it WeightedArray) - but that violates the Liskov Substitution Principle, which is why I'd like to have a basic implementation of this in the base class.
Alternatively, we implement a base abstract class and then inherit WeightedContainer from it and WeightedArray from it separately. Because ideally I'd like the WeightedArray to support all kinds of array functions e.g.
pop(),push()etc.