Skip to content

Commit

Permalink
Rework Sequence.locate and Sequence.rlocate
Browse files Browse the repository at this point in the history
Nothing actually used these before, primarily since their fuctionalty was not only unnecessary, but rather complicated.

As such, remove those functions and replace them with new ones that (can) do pretty much the same, but much more than that, hopefully making them actually useful. (for reference, `Sequence.locate` now does what the javascript `Array.find` function does, taking a predicate and returning th first matching element, or `none`)

Migration (in case someone actually used the old ones):
```
// Before
local z = x.locate(y);

// After
local z = x.locate(item -> item == y);
if (z is none)
    throw ValueError();
```
  • Loading branch information
GrieferAtWork committed Dec 15, 2024
1 parent 80225c7 commit cb8aa8e
Show file tree
Hide file tree
Showing 29 changed files with 295 additions and 1,384 deletions.
1 change: 1 addition & 0 deletions .vs/deemon-v141.sln
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "include", "include", "{2360
..\lib\include\function = ..\lib\include\function
..\lib\include\gc = ..\lib\include\gc
..\lib\include\intrin = ..\lib\include\intrin
..\lib\include\m_builtins.dee = ..\lib\include\m_builtins.dee
..\lib\include\m_fs.dee = ..\lib\include\m_fs.dee
..\lib\include\m_fs_path.dee = ..\lib\include\m_fs_path.dee
..\lib\include\m_fs_posix.dee = ..\lib\include\m_fs_posix.dee
Expand Down
2 changes: 0 additions & 2 deletions .vs/deemon-v141.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@
<ClInclude Include="..\src\deemon\objects\seq\each.h" />
<ClInclude Include="..\src\deemon\objects\seq\filter.h" />
<ClInclude Include="..\src\deemon\objects\seq\hashfilter.h" />
<ClInclude Include="..\src\deemon\objects\seq\locateall.h" />
<ClInclude Include="..\src\deemon\objects\seq\mapped.h" />
<ClInclude Include="..\src\deemon\objects\seq\range.h" />
<ClInclude Include="..\src\deemon\objects\seq\repeat.h" />
Expand Down Expand Up @@ -459,7 +458,6 @@
<ClCompile Include="..\src\deemon\objects\seq\fastseq.c" />
<ClCompile Include="..\src\deemon\objects\seq\filter.c" />
<ClCompile Include="..\src\deemon\objects\seq\hashfilter.c" />
<ClCompile Include="..\src\deemon\objects\seq\locateall.c" />
<ClCompile Include="..\src\deemon\objects\seq\mapped.c" />
<ClCompile Include="..\src\deemon\objects\seq\range.c" />
<ClCompile Include="..\src\deemon\objects\seq\repeat.c" />
Expand Down
6 changes: 0 additions & 6 deletions .vs/deemon-v141.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -780,9 +780,6 @@
<ClInclude Include="..\src\deemon\objects\seq\hashfilter.h">
<Filter>src\objects\seq</Filter>
</ClInclude>
<ClInclude Include="..\src\deemon\objects\seq\locateall.h">
<Filter>src\objects\seq</Filter>
</ClInclude>
<ClInclude Include="..\src\deemon\objects\seq\mapped.h">
<Filter>src\objects\seq</Filter>
</ClInclude>
Expand Down Expand Up @@ -1346,9 +1343,6 @@
<ClCompile Include="..\src\deemon\objects\seq\hashfilter.c">
<Filter>src\objects\seq</Filter>
</ClCompile>
<ClCompile Include="..\src\deemon\objects\seq\locateall.c">
<Filter>src\objects\seq</Filter>
</ClCompile>
<ClCompile Include="..\src\deemon\objects\seq\mapped.c">
<Filter>src\objects\seq</Filter>
</ClCompile>
Expand Down
1 change: 1 addition & 0 deletions .vs/deemon-v142.sln
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "include", "include", "{2360
..\lib\include\function = ..\lib\include\function
..\lib\include\gc = ..\lib\include\gc
..\lib\include\intrin = ..\lib\include\intrin
..\lib\include\m_builtins.dee = ..\lib\include\m_builtins.dee
..\lib\include\m_fs.dee = ..\lib\include\m_fs.dee
..\lib\include\m_fs_path.dee = ..\lib\include\m_fs_path.dee
..\lib\include\m_fs_posix.dee = ..\lib\include\m_fs_posix.dee
Expand Down
2 changes: 0 additions & 2 deletions .vs/deemon-v142.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@
<ClInclude Include="..\src\deemon\objects\seq\each.h" />
<ClInclude Include="..\src\deemon\objects\seq\filter.h" />
<ClInclude Include="..\src\deemon\objects\seq\hashfilter.h" />
<ClInclude Include="..\src\deemon\objects\seq\locateall.h" />
<ClInclude Include="..\src\deemon\objects\seq\mapped.h" />
<ClInclude Include="..\src\deemon\objects\seq\range.h" />
<ClInclude Include="..\src\deemon\objects\seq\repeat.h" />
Expand Down Expand Up @@ -459,7 +458,6 @@
<ClCompile Include="..\src\deemon\objects\seq\fastseq.c" />
<ClCompile Include="..\src\deemon\objects\seq\filter.c" />
<ClCompile Include="..\src\deemon\objects\seq\hashfilter.c" />
<ClCompile Include="..\src\deemon\objects\seq\locateall.c" />
<ClCompile Include="..\src\deemon\objects\seq\mapped.c" />
<ClCompile Include="..\src\deemon\objects\seq\range.c" />
<ClCompile Include="..\src\deemon\objects\seq\repeat.c" />
Expand Down
6 changes: 0 additions & 6 deletions .vs/deemon-v142.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -780,9 +780,6 @@
<ClInclude Include="..\src\deemon\objects\seq\hashfilter.h">
<Filter>src\objects\seq</Filter>
</ClInclude>
<ClInclude Include="..\src\deemon\objects\seq\locateall.h">
<Filter>src\objects\seq</Filter>
</ClInclude>
<ClInclude Include="..\src\deemon\objects\seq\mapped.h">
<Filter>src\objects\seq</Filter>
</ClInclude>
Expand Down Expand Up @@ -1346,9 +1343,6 @@
<ClCompile Include="..\src\deemon\objects\seq\hashfilter.c">
<Filter>src\objects\seq</Filter>
</ClCompile>
<ClCompile Include="..\src\deemon\objects\seq\locateall.c">
<Filter>src\objects\seq</Filter>
</ClCompile>
<ClCompile Include="..\src\deemon\objects\seq\mapped.c">
<Filter>src\objects\seq</Filter>
</ClCompile>
Expand Down
3 changes: 3 additions & 0 deletions .vs/dex/rt-v141.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
<ClCompile Include="..\..\src\dex\rt\librt.c" />
<ClCompile Include="..\..\src\dex\rt\slab.c" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\src\dex\rt\constants.def" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{85DE4E2D-552A-48E3-A320-0000000003E4}</ProjectGuid>
<RootNamespace>rt</RootNamespace>
Expand Down
3 changes: 3 additions & 0 deletions .vs/dex/rt-v142.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
<ClCompile Include="..\..\src\dex\rt\librt.c" />
<ClCompile Include="..\..\src\dex\rt\slab.c" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\src\dex\rt\constants.def" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{85DE4E2D-552A-48E3-A320-0000000003E4}</ProjectGuid>
<RootNamespace>rt</RootNamespace>
Expand Down
24 changes: 9 additions & 15 deletions include/deemon/method-hints.def
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,15 @@ Dee_DEFINE_TYPE_METHOD_HINT_FUNC(WUNUSED_T NONNULL_T((1, 2)), int, DCALL, seq_co
Dee_DEFINE_TYPE_METHOD_HINT_FUNC(WUNUSED_T NONNULL_T((1, 2, 5)), int, DCALL, seq_contains_with_range_and_key, (DeeObject *self, DeeObject *item, size_t start, size_t end, DeeObject *key))
Dee_DEFINE_TYPE_METHOD_HINT_KWMETHOD(seq_contains, "contains", "(item,start=!0,end:?Dint=!A!Dint!PSIZE_MAX,key:?DCallable=!N)->?Dbool")

Dee_DEFINE_TYPE_METHOD_HINT_FUNC(WUNUSED_T NONNULL_T((1, 2)), DREF DeeObject *, DCALL, seq_locate, (DeeObject *self, DeeObject *item))
Dee_DEFINE_TYPE_METHOD_HINT_FUNC(WUNUSED_T NONNULL_T((1, 2, 3)), DREF DeeObject *, DCALL, seq_locate_with_key, (DeeObject *self, DeeObject *item, DeeObject *key))
Dee_DEFINE_TYPE_METHOD_HINT_FUNC(WUNUSED_T NONNULL_T((1, 2)), DREF DeeObject *, DCALL, seq_locate_with_range, (DeeObject *self, DeeObject *item, size_t start, size_t end))
Dee_DEFINE_TYPE_METHOD_HINT_FUNC(WUNUSED_T NONNULL_T((1, 2, 5)), DREF DeeObject *, DCALL, seq_locate_with_range_and_key, (DeeObject *self, DeeObject *item, size_t start, size_t end, DeeObject *key))
Dee_DEFINE_TYPE_METHOD_HINT_KWMETHOD(seq_locate, "locate", "(item,start=!0,end:?Dint=!A!Dint!PSIZE_MAX,key:?DCallable=!N)->")

/*Dee_DEFINE_TYPE_METHOD_HINT_FUNC(WUNUSED_T NONNULL_T((1, 2)), DREF DeeObject *, DCALL, seq_rlocate, (DeeObject *self, DeeObject *item))*/ /* Wouldn't make sense: for reverse, you need indices */
/*Dee_DEFINE_TYPE_METHOD_HINT_FUNC(WUNUSED_T NONNULL_T((1, 2, 3)), DREF DeeObject *, DCALL, seq_rlocate_with_key, (DeeObject *self, DeeObject *item, DeeObject *key))*/ /* Wouldn't make sense: for reverse, you need indices */
Dee_DEFINE_TYPE_METHOD_HINT_FUNC(WUNUSED_T NONNULL_T((1, 2)), DREF DeeObject *, DCALL, seq_rlocate_with_range, (DeeObject *self, DeeObject *item, size_t start, size_t end))
Dee_DEFINE_TYPE_METHOD_HINT_FUNC(WUNUSED_T NONNULL_T((1, 2, 5)), DREF DeeObject *, DCALL, seq_rlocate_with_range_and_key, (DeeObject *self, DeeObject *item, size_t start, size_t end, DeeObject *key))
Dee_DEFINE_TYPE_METHOD_HINT_KWMETHOD(seq_rlocate, "rlocate", "(item,start=!0,end:?Dint=!A!Dint!PSIZE_MAX,key:?DCallable=!N)->")
/* Returns the first element (within the given range) where `match(elem)' is true. */
Dee_DEFINE_TYPE_METHOD_HINT_FUNC(WUNUSED_T NONNULL_T((1, 2, 3)), DREF DeeObject *, DCALL, seq_locate, (DeeObject *self, DeeObject *match, DeeObject *def))
Dee_DEFINE_TYPE_METHOD_HINT_FUNC(WUNUSED_T NONNULL_T((1, 2, 5)), DREF DeeObject *, DCALL, seq_locate_with_range, (DeeObject *self, DeeObject *match, size_t start, size_t end, DeeObject *def))
Dee_DEFINE_TYPE_METHOD_HINT_KWMETHOD(seq_locate, "locate", "(match,start=!0,end:?Dint=!A!Dint!PSIZE_MAX,def=!N)->?X2?O?Q!Adef]")

/* Returns the last element (within the given range) where `match(elem)' is true. */
/*Dee_DEFINE_TYPE_METHOD_HINT_FUNC(WUNUSED_T NONNULL_T((1, 2, 3)), DREF DeeObject *, DCALL, seq_rlocate, (DeeObject *self, DeeObject *match, DeeObject *def))*/ /* Wouldn't make sense: for reverse, you need indices */
Dee_DEFINE_TYPE_METHOD_HINT_FUNC(WUNUSED_T NONNULL_T((1, 2, 5)), DREF DeeObject *, DCALL, seq_rlocate_with_range, (DeeObject *self, DeeObject *match, size_t start, size_t end, DeeObject *def))
Dee_DEFINE_TYPE_METHOD_HINT_KWMETHOD(seq_rlocate, "rlocate", "(match,start=!0,end:?Dint=!A!Dint!PSIZE_MAX,def=!N)->?X2?O?Q!Adef]")

/* @return: 0 : Does not start with
* @return: 1 : Does start with
Expand Down Expand Up @@ -281,10 +279,6 @@ Dee_DEFINE_TYPE_METHOD_HINT_FUNC(WUNUSED_T NONNULL_T((1, 2, 5)), int, DCALL, seq
Dee_DEFINE_TYPE_METHOD_HINT_FUNC(WUNUSED_T NONNULL_T((1, 2, 5, 6)), int, DCALL, seq_brange_with_key, (DeeObject *self, DeeObject *item, size_t start, size_t end, DeeObject *key, size_t result_range[2]))
Dee_DEFINE_TYPE_METHOD_HINT_KWMETHOD(seq_brange, "brange", "(item,start=!0,end:?Dint=!A!Dint!PSIZE_MAX,key:?DCallable=!N)->?X2?Dint?Dint")

Dee_DEFINE_TYPE_METHOD_HINT_FUNC(WUNUSED_T NONNULL_T((1, 2)), DREF DeeObject *, DCALL, seq_blocate, (DeeObject *self, DeeObject *item, size_t start, size_t end))
Dee_DEFINE_TYPE_METHOD_HINT_FUNC(WUNUSED_T NONNULL_T((1, 2, 5)), DREF DeeObject *, DCALL, seq_blocate_with_key, (DeeObject *self, DeeObject *item, size_t start, size_t end, DeeObject *key))
Dee_DEFINE_TYPE_METHOD_HINT_KWMETHOD(seq_blocate, "blocate", "(item,start=!0,end:?Dint=!A!Dint!PSIZE_MAX,key:?DCallable=!N)->")



/************************************************************************/
Expand Down
10 changes: 0 additions & 10 deletions include/deemon/method-hints.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,8 @@ INTDEF ATTR_PURE ATTR_RETNONNULL WUNUSED NONNULL((1)) Dee_mh_seq_contains_with_k
INTDEF ATTR_PURE ATTR_RETNONNULL WUNUSED NONNULL((1)) Dee_mh_seq_contains_with_range_t DCALL DeeType_RequireSeqContainsWithRange(DeeTypeObject *__restrict self);
INTDEF ATTR_PURE ATTR_RETNONNULL WUNUSED NONNULL((1)) Dee_mh_seq_contains_with_range_and_key_t DCALL DeeType_RequireSeqContainsWithRangeAndKey(DeeTypeObject *__restrict self);
INTDEF ATTR_PURE ATTR_RETNONNULL WUNUSED NONNULL((1)) Dee_mh_seq_locate_t DCALL DeeType_RequireSeqLocate(DeeTypeObject *__restrict self);
INTDEF ATTR_PURE ATTR_RETNONNULL WUNUSED NONNULL((1)) Dee_mh_seq_locate_with_key_t DCALL DeeType_RequireSeqLocateWithKey(DeeTypeObject *__restrict self);
INTDEF ATTR_PURE ATTR_RETNONNULL WUNUSED NONNULL((1)) Dee_mh_seq_locate_with_range_t DCALL DeeType_RequireSeqLocateWithRange(DeeTypeObject *__restrict self);
INTDEF ATTR_PURE ATTR_RETNONNULL WUNUSED NONNULL((1)) Dee_mh_seq_locate_with_range_and_key_t DCALL DeeType_RequireSeqLocateWithRangeAndKey(DeeTypeObject *__restrict self);
INTDEF ATTR_PURE ATTR_RETNONNULL WUNUSED NONNULL((1)) Dee_mh_seq_rlocate_with_range_t DCALL DeeType_RequireSeqRLocateWithRange(DeeTypeObject *__restrict self);
INTDEF ATTR_PURE ATTR_RETNONNULL WUNUSED NONNULL((1)) Dee_mh_seq_rlocate_with_range_and_key_t DCALL DeeType_RequireSeqRLocateWithRangeAndKey(DeeTypeObject *__restrict self);
INTDEF ATTR_PURE ATTR_RETNONNULL WUNUSED NONNULL((1)) Dee_mh_seq_startswith_t DCALL DeeType_RequireSeqStartsWith(DeeTypeObject *__restrict self);
INTDEF ATTR_PURE ATTR_RETNONNULL WUNUSED NONNULL((1)) Dee_mh_seq_startswith_with_key_t DCALL DeeType_RequireSeqStartsWithWithKey(DeeTypeObject *__restrict self);
INTDEF ATTR_PURE ATTR_RETNONNULL WUNUSED NONNULL((1)) Dee_mh_seq_startswith_with_range_t DCALL DeeType_RequireSeqStartsWithWithRange(DeeTypeObject *__restrict self);
Expand Down Expand Up @@ -391,8 +388,6 @@ INTDEF ATTR_PURE ATTR_RETNONNULL WUNUSED NONNULL((1)) Dee_mh_seq_bposition_t DCA
INTDEF ATTR_PURE ATTR_RETNONNULL WUNUSED NONNULL((1)) Dee_mh_seq_bposition_with_key_t DCALL DeeType_RequireSeqBPositionWithKey(DeeTypeObject *__restrict self);
INTDEF ATTR_PURE ATTR_RETNONNULL WUNUSED NONNULL((1)) Dee_mh_seq_brange_t DCALL DeeType_RequireSeqBRange(DeeTypeObject *__restrict self);
INTDEF ATTR_PURE ATTR_RETNONNULL WUNUSED NONNULL((1)) Dee_mh_seq_brange_with_key_t DCALL DeeType_RequireSeqBRangeWithKey(DeeTypeObject *__restrict self);
INTDEF ATTR_PURE ATTR_RETNONNULL WUNUSED NONNULL((1)) Dee_mh_seq_blocate_t DCALL DeeType_RequireSeqBLocate(DeeTypeObject *__restrict self);
INTDEF ATTR_PURE ATTR_RETNONNULL WUNUSED NONNULL((1)) Dee_mh_seq_blocate_with_key_t DCALL DeeType_RequireSeqBLocateWithKey(DeeTypeObject *__restrict self);

/* Set functions */
INTDEF ATTR_PURE ATTR_RETNONNULL WUNUSED NONNULL((1)) Dee_mh_set_insert_t DCALL DeeType_RequireSetInsert(DeeTypeObject *__restrict self);
Expand Down Expand Up @@ -568,11 +563,8 @@ INTDEF ATTR_PURE ATTR_RETNONNULL WUNUSED NONNULL((1)) Dee_mh_map_popitem_t DCALL
#define DeeType_RequireSeqContainsWithRange(self) ((Dee_mh_seq_contains_with_range_t)DeeType_GetMethodHint(self, Dee_TMH_seq_contains_with_range))
#define DeeType_RequireSeqContainsWithRangeAndKey(self) ((Dee_mh_seq_contains_with_range_and_key_t)DeeType_GetMethodHint(self, Dee_TMH_seq_contains_with_range_and_key))
#define DeeType_RequireSeqLocate(self) ((Dee_mh_seq_locate_t)DeeType_GetMethodHint(self, Dee_TMH_seq_locate))
#define DeeType_RequireSeqLocateWithKey(self) ((Dee_mh_seq_locate_with_key_t)DeeType_GetMethodHint(self, Dee_TMH_seq_locate_with_key))
#define DeeType_RequireSeqLocateWithRange(self) ((Dee_mh_seq_locate_with_range_t)DeeType_GetMethodHint(self, Dee_TMH_seq_locate_with_range))
#define DeeType_RequireSeqLocateWithRangeAndKey(self) ((Dee_mh_seq_locate_with_range_and_key_t)DeeType_GetMethodHint(self, Dee_TMH_seq_locate_with_range_and_key))
#define DeeType_RequireSeqRLocateWithRange(self) ((Dee_mh_seq_rlocate_with_range_t)DeeType_GetMethodHint(self, Dee_TMH_seq_rlocate_with_range))
#define DeeType_RequireSeqRLocateWithRangeAndKey(self) ((Dee_mh_seq_rlocate_with_range_and_key_t)DeeType_GetMethodHint(self, Dee_TMH_seq_rlocate_with_range_and_key))
#define DeeType_RequireSeqStartsWith(self) ((Dee_mh_seq_startswith_t)DeeType_GetMethodHint(self, Dee_TMH_seq_startswith))
#define DeeType_RequireSeqStartsWithWithKey(self) ((Dee_mh_seq_startswith_with_key_t)DeeType_GetMethodHint(self, Dee_TMH_seq_startswith_with_key))
#define DeeType_RequireSeqStartsWithWithRange(self) ((Dee_mh_seq_startswith_with_range_t)DeeType_GetMethodHint(self, Dee_TMH_seq_startswith_with_range))
Expand Down Expand Up @@ -615,8 +607,6 @@ INTDEF ATTR_PURE ATTR_RETNONNULL WUNUSED NONNULL((1)) Dee_mh_map_popitem_t DCALL
#define DeeType_RequireSeqBPositionWithKey(self) ((Dee_mh_seq_bposition_with_key_t)DeeType_GetMethodHint(self, Dee_TMH_seq_bposition_with_key))
#define DeeType_RequireSeqBRange(self) ((Dee_mh_seq_brange_t)DeeType_GetMethodHint(self, Dee_TMH_seq_brange))
#define DeeType_RequireSeqBRangeWithKey(self) ((Dee_mh_seq_brange_with_key_t)DeeType_GetMethodHint(self, Dee_TMH_seq_brange_with_key))
#define DeeType_RequireSeqBLocate(self) ((Dee_mh_seq_blocate_t)DeeType_GetMethodHint(self, Dee_TMH_seq_blocate))
#define DeeType_RequireSeqBLocateWithKey(self) ((Dee_mh_seq_blocate_with_key_t)DeeType_GetMethodHint(self, Dee_TMH_seq_blocate_with_key))
#define DeeType_RequireSetInsert(self) ((Dee_mh_set_insert_t)DeeType_GetMethodHint(self, Dee_TMH_set_insert))
#define DeeType_RequireSetRemove(self) ((Dee_mh_set_remove_t)DeeType_GetMethodHint(self, Dee_TMH_set_remove))
#define DeeType_RequireSetUnify(self) ((Dee_mh_set_unify_t)DeeType_GetMethodHint(self, Dee_TMH_set_unify))
Expand Down
10 changes: 6 additions & 4 deletions lib/include/__builtin_util.dee
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ final local __builtin_seq_index = (Sequence from deemon).operator . ("index");
final local __builtin_seq_rindex = (Sequence from deemon).operator . ("rindex");
final local __builtin_seq_contains = (Sequence from deemon).operator . ("contains");
final local __builtin_seq_count = (Sequence from deemon).operator . ("count");
final local __builtin_seq_locate = (Sequence from deemon).operator . ("locate");
final local __builtin_seq_rlocate = (Sequence from deemon).operator . ("rlocate");
final local __builtin_seq_locate_all = (Sequence from deemon).operator . ("locateall");
from ".m_builtins" import
__builtin_seq_locate = "seq_locate",
__builtin_seq_rlocate = "seq_rlocate",
__builtin_seq_locate_all = "seq_locateall"
;
final local __builtin_seq_transform = (Sequence from deemon).operator . ("transform");
final local __builtin_seq_empty = (Sequence from deemon).operator . ("isempty");
final local __builtin_seq_non_empty = (Sequence from deemon).operator . ("isnonempty");
Expand All @@ -63,5 +65,5 @@ final local __builtin_seq_at = (Sequence from deemon).operator . ("at");
final local __builtin_seq_reversed = (Sequence from deemon).operator . ("reversed");
final local __builtin_seq_sorted = (Sequence from deemon).operator . ("sorted");
final local __builtin_seq_segments = (Sequence from deemon).operator . ("segments");
#pragma pop_macro("from", "util", "import", "Sequence", "deemon", "operator", "final", "local")
#pragma pop_macro("from", "import", "Sequence", "deemon", "operator", "final", "local")

57 changes: 57 additions & 0 deletions lib/include/m_builtins.dee
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* Copyright (c) 2018-2024 Griefer@Work *
* *
* This software is provided 'as-is', without any express or implied *
* warranty. In no event will the authors be held liable for any damages *
* arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute it *
* freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must not *
* claim that you wrote the original software. If you use this software *
* in a product, an acknowledgement (see the following) in the product *
* documentation is required: *
* Portions Copyright (c) 2018-2024 Griefer@Work *
* 2. Altered source versions must be plainly marked as such, and must not be *
* misrepresented as being the original software. *
* 3. This notice may not be removed or altered from any source distribution. *
*/
import * from deemon;
import * from errors;
import identity from functools;

function seq_locate(seq, item, key = none): Object {
if (key is none)
key = identity;
item = key(item);
for (local elem: seq as Sequence) {
if (equals(item, key(elem)))
return elem;
}
throw ValueError(f"Could not locate item `{item}' in sequence `{seq}'");
}

function seq_rlocate(seq, item, key = none): Object {
if (key is none)
key = identity;
item = key(item);
local result;
for (local elem: seq as Sequence) {
if (equals(item, key(elem)))
result = elem;
}
if (result is bound)
return result;
throw ValueError(f"Could not locate item `{item}' in sequence `{seq}'");
}

function seq_locateall(seq, item, key = none): {Object...} {
if (key is none)
key = identity;
item = key(item);
for (local elem: seq as Sequence) {
if (equals(item, key(elem)))
yield elem;
}
}
7 changes: 4 additions & 3 deletions lib/rt/builtins.dee
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import compat_util = ..include.m_util;
import compat_util_attr = ..include.m_util_attr;
import compat_math = ..include.m_math;
import compat_gcc = ..include.__builtin_gcc;
import compat_builtins = ..include.m_builtins;
import compat_sys = ..include.m_sys;
import compat_fs = ..include.m_fs;
final local opt_win32 = try import("win32") catch (...) none;
Expand Down Expand Up @@ -498,9 +499,9 @@ local __objects = deemon.List {
/* 0x01b4 */ deemon.Sequence.index,
/* 0x01b5 */ deemon.Sequence.rindex,
/* 0x01b6 */ deemon.Sequence.count,
/* 0x01b7 */ deemon.Sequence.locate,
/* 0x01b8 */ deemon.Sequence.rlocate,
/* 0x01b9 */ deemon.Sequence.locateall,
/* 0x01b7 */ compat_builtins.seq_locate,
/* 0x01b8 */ compat_builtins.seq_rlocate,
/* 0x01b9 */ compat_builtins.seq_locateall,
/* 0x01ba */ deemon.Sequence.transform,
/* 0x01bb */ deemon.Sequence.isempty,
/* 0x01bc */ deemon.Sequence.isnonempty,
Expand Down
Loading

0 comments on commit cb8aa8e

Please sign in to comment.