Skip to content

Collections again #185

@apblack

Description

@apblack

I just closed #104 because it was old, and the particular issues discussed there have been resolved. I've recently run up against some new ones.

Recently, I was wanting to extract an arbitrary element from a set, and could not see how. So I added anyone to sets. Then, later, I found this specification in type Collection⟦T⟧

first -> T
    // The first element of self; raises BoundsError if there is none.  
    // If self is unordered, then first returns an arbitrary element. 

I had forgotten about this. This question I want to raise is: which is preferable?

  1. Having first on unordered collections return an arbitrary element, and on collections with numeric keys be equivalent to at 1
  2. Having anyone work on any collection, and return an arbitrary element, and have first work only on collections with numeric keys, where is is always equivalent to at 1

I also noticed that find(predicate) ... and includes(predicate), which look for an element that satisfies predicate and either return it (find) or tell us whether or not the search was successful (includes), existed on sets but not other collections. This seems wrong; there is no reason not to implement them on all collections, and indeed the same code would work for all. But I have trouble remembering the distinction between contains and includes (perhaps because Smalltalk uses includes for what Grace calls contains). So I'm suggesting

anySatisfies(predicate: Function1⟦T,Boolean⟧) -> Boolean
    // true if predicate holds for any of the elements of self

allSatisfy(predicate: Function1⟦T,Boolean⟧) -> Boolean
    // true if predicate holds for all of the elements of self

where anySatisfies is the new name for includes, and allSatisfy is a new method.

The tension here is between putting more stuff into collections, and thus making the interface better for sophisticated programmers, but harder for novices to wrap their minds around, or leaving stuff out, and thus providing opportunities for student assignments that implement methods like anySatisfies and includes.

We can have the best of both worlds, by creating a beginning dialect that exposes simpleCollections, for example. But then someone has to maintain them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions