Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add in, contains, and MembershipTestable #615

Merged
merged 2 commits into from
Jan 20, 2025
Merged

add in, contains, and MembershipTestable #615

merged 2 commits into from
Jan 20, 2025

Conversation

mflatt
Copy link
Member

@mflatt mflatt commented Jan 18, 2025

Add an in operator that tests membership as in many other languages (e.g., Python, Kotlin, Rust). The operator combination !in works because ! now has an infix mode as long as its followed by in, is_now or is_a.

A new class can support in by implementing the MembershipTestable interface, which has a single contains method. The method is required to return a Boolean.

Rename List.has_element and Range.has_element to List.contains and Range.contains. Add Array.contains, Map.contains (an alias for Map.has_key), and Set.contains.

Change sets to be non-indexable. The former indexing operation on a set is better expressed using in. Mutable sets no longer have an operator form for assignment; MutableSet.add or MutableSet.remove must be used, instead.

While we're at it, add Set.add, rename MutableMap.delete to MutableMap.remove, and rename MutableSet.delete to MutableSet.remove.

demo.rhm Outdated Show resolved Hide resolved
rhombus-lib/rhombus/private/amalgam.rkt Outdated Show resolved Hide resolved
@usaoc
Copy link
Collaborator

usaoc commented Jan 19, 2025

Bikeshedding: in in Rust is a keyword that is only used in for, as in for elem in iter { .... }, similar to Python and Kotlin. I think for (elem in seq): .... would’ve been more expected than the current for (elem: seq): .... if we end up adding in.

Add an `in` operator that tests membership as in many other languages
(e.g., Python, Kotlin, Rust). The operator combination `!in` works
because `!` now has an infix mode as long as its followed by `in`,
`is_now` or `is_a`.

A new class can support `in` by implementing the `MembershipTestable`
interface, which has a single `contains` method. The method is
required to return a `Boolean`.

Rename `List.has_element` and `Range.has_element` to `List.contains`
and `Range.contains`. Add `Array.contains`, `Map.contains` (an alias
for `Map.has_key`), and `Set.contains`.

Change sets to be non-indexable. The former indexing operation on a
set is better expressed using `in`. Mutable sets no longer have an
operator form for assignment; `MutableSet.add` or `MutableSet.remove`
must be used, instead.

While we're at it, add `Set.add`, rename `MutableMap.delete` to
`MutableMap.remove`, and rename `MutableSet.delete` to
`MutableSet.remove`.
Similar to the way `def` allows `=` in place of `:`, allow `in` in
place of `:` for `each` bindings:

```
  for (i in 0..10):
    println(i)

  for:
    each i in 0..10
    println(i)
```

Change tests and docs to use `in` as the preferred form for `each`.
@mflatt
Copy link
Member Author

mflatt commented Jan 19, 2025

I agree that in should be used in for for each bindings and the shorthand, so I added a second commit to enable for (elem in seq): .....

Conversion to use in as the preferred form was only partly automated, so likely I missed some, but hopefully I found most places.

@mflatt mflatt merged commit 2f8598b into racket:master Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants