-
-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[doc/ref] Document __builtins__, Str/List method design
I went for the non-polymorphic design, i.e. Str and List are different. Str.find(s) -> int # like Python Str.findLast(s) -> int Str.includes(s) -> bool List.indexOf(item) -> int # like JS List.lastIndexOf(item) -> int # like JS List.contains(item) -> bool This isn't set in stone. But here's some justification: - Avoid "accidentally quadratic" of the 'in' operator - it is defined to be O(1), not O(n) - the O(n) boolean test operations are includes() and contains() - Avoid "false polymorphism" - Str is NOT a container of substrings. There are arbitrarily many substrings! - It's also not a parameterized type Note that most languages are more polymorphic than this; - JavaScript uses indexOf/lastIndexOf/includes for both String and Array - Python is less consistent, but it has index() on both str and list - Ruby is pretty polymorphic However, they do NOT use the UTF-8 string model that we use. JavaScript strings are treated as an array of 16 bit code units, and Python strings are an array of 32 bit code units. So I think these things justify a different API. We have NEITHER the Python nor JavaScript string model.
- Loading branch information
Andy C
committed
Nov 7, 2024
1 parent
106726e
commit ffa0bf5
Showing
3 changed files
with
36 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters