What do you call an iterator that returns the current, previous and next siblings of each node of a list? [closed]

What do you call an iterator that given a list [a, b, c], returns an object of the form { prev, curr, next } for each iteration?

e.g, (? === undefined)

{ prev: ?, curr: a, next: b }
{ prev: a, curr: b, next: c }
{ prev: b, curr: c, next: ? }

I thought tuples, but I am used to think that a tuple is usually a 2-item list so I am undecided.


EDIT: Triples could work, but I was also wondering if there is any mention of this type of structures in the literature. I am familiar with linked lists, but this is different.

EDIT 2: Here is the generator implementation in JavaScript.

  /**
    @syntax
    Parsec.prototype.tuples([a, b, c])
      { prev: ?, curr: a, next: b }
      { prev: a, curr: b, next: c }
      { prev: b, curr: c, next: ? }
    @param {Array|String} list List to iterate.
    @param {Object} [last] Define to use as the last value.
    @param {Object} [prev] Define to use as the first prev value.
    @param {Object} [curr] Define to use as the first curr value.
  */
  function* tuples(list, last, prev, curr) {
    for (let next of list) {
      if (curr !== undefined) yield { prev, curr, next /* add last here? */}
      prev = curr
      curr = next
    }
    yield { prev, curr, last }
  }

4

Apart from triple (which only means “we have three values”) and node (which has been suggested by manlio), I can suggest the following alternatives:

Sliding window

It looks like you always have a window of three consecutives elements in the list. This is the most appropriate name IMHO according to your situation.

That structure does not allow to easily navigate the list in terms of prev, next pointers, however (maybe it is intentional).
If you want to navigate the data-structure, you might want to call it a node (like in doubly-linked lists, as manlio said) or a cursor, which is a slightly better name that conveys the iterative capabilities of the object.

Cursor

Iterators in Ada are built upon a so-called Cursor type, which encapsulates the position of an iterator inside a structure, along with Next and Previous operations.

See for example Gem #127 or the Rationale for Ada 2005.

Zipper

You question originally reminded me of Zippers which are construct usually found with purely functional data-structures. They allow to iterate over a structure while keeping enough context to navigate it.

For example, a list zipper would contain the reversed list of previous elements (Prefix), Current element and the usual Tail list:

(Prefix,Current,Tail)

It is easy to build a zipper for the next element in constant time:

(cons(Current,Prefix),head(Tail),tail(Tail))

Or, a zipper for the previous element in constant time:

(tail(Prefix),head(Prefix),cons(Current,Tail))

Of course, the cost of this approach is that you are manipulating two lists instead of one (during the lifetime of the iterator/zipper). Contrary to doubly-linked lists, you don’t have to mutate your data-structures.

Your structure is clearly not a zipper, but it shared enough similarities with them that I thought it would be interesting to write about them.

1

In Scala, this is called a sliding window:

Seq(1, 2, 3, 4, 5).sliding(2).foreach(println)
// List(1, 2)
// List(2, 3)
// List(3, 4)
// List(4, 5)

As opposed to grouped when the elements don’t overlap:

Seq(1, 2, 3, 4, 5).grouped(2).foreach(println)
// List(1, 2)
// List(3, 4)
// List(5)

In Ruby, it is called cons:

[1, 2, 3, 4, 5].each_cons(2).to_a
# => [[1, 2], [2, 3], [3, 4], [4, 5]]

and the other one is called slice:

[1, 2, 3, 4, 5].each_slice(2).to_a
# => [[1, 2], [3, 4], [5]]

Note: in your case, the window starts before the first element and ends after the last, whereas in Ruby and Scala it starts at the first element and ends at the last, but that’s just an arbitrary API design choice, and probably shouldn’t affect the name.

I think “cons” (for “consecutive”) is an unfortunate choice of name, because it clashes with the well-established term “cons” for “construct”, as in Lisp and other functional languages. I like “sliding window” or “sliding view” much better.

A tuple is a finite sequence (ordered list) of elements. It’s true in mathematics, relational databases and many languages (e.g. C++, Prolog, Python…).

So tuple could be a good name.

node is an alternative since an object of the form {prev, curr, next} remembers of a node of a doubly linked list.

1

Well this seems like a theory question on algorithms and datastructures.
As for the datastructure, if you want to return the three results at once, of course you need to put them into one datastructure, and of course that would make it a triple.
Without a given syntax it’s hard to give an answer and the question seems not properly stated, I feel the question is also how to calculate previous and next, and for this I would say something like:

curry = this;
prev = this;
next = this;
prev —-;
next++;

if(pre <0) prev = undefined;
if (next >= length) next = undefined;

As for the name of this kind of itertor, well as you already did I would call it triple or triplet, as it needs to store 3 values.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật