Skip to content

Instantly share code, notes, and snippets.

@elrayle
Last active October 21, 2015 15:39
Show Gist options
  • Save elrayle/ebd0853958e09c0417be to your computer and use it in GitHub Desktop.
Save elrayle/ebd0853958e09c0417be to your computer and use it in GitHub Desktop.
PCDM API for ordering and sets

Observations:

  • What is the purpose for maintaining two lists where the only difference is whether an item can repeat?
  • It seems that the use case for order is more common than the use case for unordered sets.
  • The use case for an unordered set can be achieved using ordered lists code.
  • Why not make ordered lists the default?

Proposed API changes making everything an ordered set

Working with everything as an ordered list

# appending
parent_col.members = [gw1,col1]   # => [gw1,col1]
parent_col.members << gw2         # => [gw1,col1,gw2]
parent_col.members << col1        # => [gw1,col1,gw2,col1]

# readers
parent_col.members                # => [gw1,gw2,col1]
parent_col.collections            # => [col1]
parent_col.works                  # => [gw1,gw2]

# moving
class Array
  def move(from,to)
    insert(to, delete_at(from))
  end
end
parent_col.members = [gw1,col1,gw2,gw3,col2,col3]   # => [gw1,col1,gw2,gw3,col2,col3]
parent_col.members.move(1,3)                        # => [gw1,gw2,gw3,col1,col2,col3]
parent_col.members.move(1,-2)                       # => [gw1,gw3,col1,col2,gw2,col3]

# deleting
parent_col.members = [col1,col2,col1,col3,col1]     # => [col1,col2,col1,col3,col1]
parent_col.members.delete_at(1)                     # => [col1,col1,col3,col1]

parent_col.members = [col1,col2,col1,col3,col1]     # => [col1,col2,col1,col3,col1]
parent_col.delete_all col1                          # => [col2,col3]

parent_col.members = [col1,col2,col1,col3,col1]     # => [col1,col2,col1,col3,col1]
parent_col.delete(col1)                             # => [col2,col1,col3,col1]          # default to 1st occurrence

parent_col.members = [col1,col2,col1,col3,col1]     # => [col1,col2,col1,col3,col1]
parent_col.delete(col1,2)                           # => [col1,col2,col3,col1]          # 2nd occurrence

parent_col.members = [col1,col2,col1,col3,col1]     # => [col1,col2,col1,col3,col1]
parent_col.delete(col1,-1)                          # => [col1,col2,col1,col3]          # last occurrence

# get unique (set view of ordered list)
parent_col.members = [col1,col2,col1,col3,col1]     # => [col1,col2,col1,col3,col1]
parent_col.members.uniq                             # => [col1,col2,col3]
@grosscol
Copy link

That would simplify the API. I can't recall exactly why it wasn't all ordered. I would bet that @tpendragon would know.

@tpendragon
Copy link

The only difference is not whether an item can repeat, it's also how expensive it is to add an item and the fact that ORE should allow inherently for items to be members but not ordered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment