Skip to content

Commit

Permalink
chore: improvements to unready_blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
EclesioMeloJunior committed Nov 11, 2024
1 parent a0f1493 commit 1d9f261
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions dot/sync/unready_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func NewFragment(chain []*types.BlockData) *Fragment {
return &Fragment{chain}
}

// Filter returns a new fragments with blocks that satisfies the predicate p
func (f *Fragment) Filter(p func(*types.BlockData) bool) *Fragment {
filtered := make([]*types.BlockData, 0, len(f.chain))
for _, bd := range f.chain {
Expand All @@ -31,7 +32,7 @@ func (f *Fragment) Filter(p func(*types.BlockData) bool) *Fragment {
return NewFragment(filtered)
}

// Find return the first occurrence of a types.BlockData that
// Find returns the first occurrence of a types.BlockData that
// satisfies the predicate p
func (f *Fragment) Find(p func(*types.BlockData) bool) *types.BlockData {
for _, bd := range f.chain {
Expand All @@ -43,6 +44,16 @@ func (f *Fragment) Find(p func(*types.BlockData) bool) *types.BlockData {
return nil
}

// Last returns the first block in the fragment or nil otherwise
func (f *Fragment) First() *types.BlockData {
if len(f.chain) > 0 {
return f.chain[0]
}

return nil
}

// Last returns the last block in the fragment or nil otherwise
func (f *Fragment) Last() *types.BlockData {
if len(f.chain) > 0 {
return f.chain[len(f.chain)-1]
Expand All @@ -51,10 +62,13 @@ func (f *Fragment) Last() *types.BlockData {
return nil
}

// Len returns the amount of blocks in the fragment
func (f *Fragment) Len() int {
return len(f.chain)
}

// Iter returns an iterator of the blocks in the fragment
// it enables the caller to use range keyword in the Fragment instance
func (f *Fragment) Iter() iter.Seq[*types.BlockData] {
return func(yield func(*types.BlockData) bool) {
for _, bd := range f.chain {
Expand All @@ -63,14 +77,8 @@ func (f *Fragment) Iter() iter.Seq[*types.BlockData] {
}
}

func (f *Fragment) First() *types.BlockData {
if len(f.chain) > 0 {
return f.chain[0]
}

return nil
}

// Concat returns a new fragment containing the concatenation
// between this fragment and the given as argument fragment
func (f *Fragment) Concat(snd *Fragment) *Fragment {
return &Fragment{
chain: slices.Concat(f.chain, snd.chain),
Expand Down

0 comments on commit 1d9f261

Please sign in to comment.