-
Notifications
You must be signed in to change notification settings - Fork 112
network: Documented, simplified and cleaner eachBin function in pot.go #1783
network: Documented, simplified and cleaner eachBin function in pot.go #1783
Conversation
…o. Crated several types to express the use of several complicated functions
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.
very nice refactor.
NOTE that pot will be rewritten very soon. Dont spend too much time deciphering this
pot/pot.go
Outdated
type ValConsumer func(Val) bool | ||
|
||
// ValIterator is a function that iterates values and executes for each of them a supplied ValConsumer. | ||
// it returns the result of the last ValConsumer executed. Usually is the pin of a pot but if could be the last element |
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.
if -> it
pot/pot.go
Outdated
type ValConsumer func(Val) bool | ||
|
||
// ValIterator is a function that iterates values and executes for each of them a supplied ValConsumer. | ||
// it returns the result of the last ValConsumer executed. Usually is the pin of a pot but if could be the last element |
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.
Usually is the pin of a pot
I am not sure I understand what you mean here.
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.
Almost all uses of EachBin will use k.base as pivotVal, so it will be the node address and the "pin" of the pot.
In that situation eachBin is just traversing the bins from less po
to more po
and finally consuming the pin of the pot.
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.
Ok. But I am unsure about the wording here. "Usually the value passed to the ValConsumer is the pin"
If we want a tutorial-ish overview of the use cases in the comment, maybe they should be in bullet points and give a bit more context. What do you think?
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.
In fact I think I'm going to change it. The last val consumed will be always the pivot value.
pot/pot.go
Outdated
n = t.bins[i] | ||
size += n.size | ||
if n.po < po { | ||
var pot *Pot |
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.
Please consider using a different variable name than the package name
Ok, let the rewrite of pot have a better guideline for rewrite EachBin function |
My bet is it won't be THAT soon ;) |
I've tried to make EachBin function more clear to developers. It used to have this signature:
...and almost no documentation. It was difficult to understand what was the purpose of each of the three annonymous function parameters of
EachBin
.I've created several abstractions to try to express better the behaviour of these iterators.
There is a new type
ValConsumer
with signature:type ValConsumer func(Val) bool
that consumes a valueVal
and returns whether the caller wants to consume more values.This consumer can be generalized so I created also
BinConsumer
with similar signature:type BinConsumer func(bin *Bin) bool
. Unsurprisingly, this function consumes a Bin and returns whether to continue consuming Bins or stop.There is also a
ValIterator
function type:type ValIterator func(ValConsumer) bool
that completes the abstraction of iterators. An iterator of type
T
takes a consumer of typeT
(ValConsumer
in this case) and starts iteratingT
elements. The iterator returns a boolean signaling the last value returned by the last consume function. TheValIterator
in our case is responsible of iteratingVal
elements inside aBin
.Finally,
Bin
is just a struct with the information provided to aBinConsumer
, itsProximityOrder
, itsSize
and theValIterator
to traverse all itsVal
elements:With this new types, the signature of
EachBin
now is:I also renamed the parameters to express their use.