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

[Feature Request] [stdlib] [proposal] Migrate/wrap String.join() methods into StringLiteral and add temp dict and list iter args #2767

Closed
1 task done
martinvuyk opened this issue May 21, 2024 · 0 comments
Labels
enhancement New feature or request mojo-repo Tag all issues with this label

Comments

@martinvuyk
Copy link
Contributor

Review Mojo's priorities

What is your request?

I would like to take this temporary implementation into StringLiteral (currently only the fn join[*Types: Stringable](self, *elems: *Types) -> String implementation is in String)

trait _StringableCollectableKey(StringableCollectionElement, KeyElement):
    ...


struct StringLiteral:
    ...
    fn join[*Types: Stringable](self, *elems: *Types) -> String:
        """Joins string elements using the current string as a delimiter.

        Parameters:
            Types: The types of the elements.

        Args:
            elems: The input values.

        Returns:
            The joined string.
        """
        var result = String("")

        @parameter
        fn add_elt[T: Stringable](a: T):
            result += self + str(a)

        elems.each[add_elt]()
        return result[len(self) :]

    fn join[T: StringableCollectionElement](self, elems: List[T]) -> String:
        """Joins string elements using the current string as a delimiter.

        Parameters:
            T: The Stringable type of the List.

        Args:
            elems: The input values.

        Returns:
            The joined string.
        """
        var result = String("")
        for elem in elems:
            result += self + elem[]
        return result[len(self) :] if len(result) > 0 else ""

    fn join[
        T: _StringableCollectableKey, A: CollectionElement, keys: Bool
    ](self, elems: Dict[T, A]) -> String:
        """Joins string elements using the current string as a delimiter.

        Parameters:
            T: The Stringable type of the Dict Keys.
            A: The type of the Values.
            keys: A temporary placeholder to differentiate between key
                and value iter.

        Args:
            elems: The input values.

        Returns:
            The joined string.
        """
        var result = String("")
        for elem in elems.keys():
            result += self + elem[]
        return result[len(self) :] if len(result) > 0 else ""

    fn join[
        T: KeyElement, A: StringableCollectionElement
    ](self, elems: Dict[T, A]) -> String:
        """Joins string elements using the current string as a delimiter.

        Parameters:
            T: The Stringable type of the Dict Keys.
            A: The type of the Values.

        Args:
            elems: The input values.

        Returns:
            The joined string.
        """
        var result = String("")
        for elem in elems.values():
            result += self + elem[]
        return result[len(self) :] if len(result) > 0 else ""

And later on when we have a generic Iterable trait as per isse #2629

trait _IterStringable:
    fn __iter__[T: StringableCollectionElement](self) -> Iterator[T]:
        ...

struct StringLiteral:
    ...

    fn join[T: _IterStringable](self, elems: T) -> String:
        """Joins string elements using the current string as a delimiter.

        Parameters:
            T: The Stringable type of the Iterator.

        Args:
            elems: The input values.

        Returns:
            The joined string.
        """
        var result = String("")
        for elem in elems:
            if not elem:
                break
            result += self + elem.value()
        return result[len(self) :] if len(result) > 0 else ""

What is your motivation for this change?

Being able to do ", ".join(List("hello", "world")) dynamically

Any other details?

No response

@martinvuyk martinvuyk added enhancement New feature or request mojo-repo Tag all issues with this label labels May 21, 2024
@martinvuyk martinvuyk closed this as not planned Won't fix, can't repro, duplicate, stale May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request mojo-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

1 participant