# Left and right division for strings

**URL:** <https://discourse.julialang.org/t/left-and-right-division-for-strings/109839>\
**Category:** Internals & Design\
**Tags:** strings, arithmetic\
**Created:** [February 6, 2024, 9:31pm UTC](https://discourse.julialang.org/t/left-and-right-division-for-strings/109839 "2024-02-06T21:31:04Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 7, 2024, 1:24pm UTC](https://discourse.julialang.org/t/left-and-right-division-for-strings/109839/21 "2024-02-07T13:24:52Z")

</div>

Split this topic on February 7, 2024, 1:24pm UTC

6 posts were split to a new topic: [Case-insensitive prefix/suffix stripping](https://discourse.julialang.org/t/case-insensitive-prefix-suffix-stripping/109871)

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [February 7, 2024, 2:35pm UTC](https://discourse.julialang.org/t/left-and-right-division-for-strings/109839/22 "2024-02-07T14:35:52Z")

</div>

Please don’t do this. Strings aren’t numbers, and it’s completely senseless to apply mathematical rules for something that just isn’t math. It’s already confusing enough that we have `*` for concatenation instead of the more obvious `+`, justified by the mathematical concept of commutativity.

There’s an argument about semantics, but honestly, it’d just be weird and confusing to see people do arithmetic on strings. In practice, people won’t be awed that we managed to impose an algebraic structure over strings, they’d just be annoyed that Julia appears to behave in a weakly typed manner.

Else, I’m going to have to retaliate and implement String-specific concepts for numbers: 😈 Surely we should have:

```julia
julia> reverse(123)
321

julia> replace(123, 2=>3)
133

```

And of course we should have `uppercase(x₂) === x2`, which by commutativity should be `2x`. We could implement this with metaprogramming!

Edit: Whoops this was not meant as a reply to you, Valentin, but just as a post in this topic!

---

<div class="post-metadata">

**Author:** ![mnemnion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mnemnion/32/206596_2.png) [@mnemnion](https://discourse.julialang.org/u/mnemnion)\
**Post date:** [February 7, 2024, 2:46pm UTC](https://discourse.julialang.org/t/left-and-right-division-for-strings/109839/23 "2024-02-07T14:46:09Z")

</div>

> [@jakobnissen](#):
>
> Please don’t do this. Strings aren’t numbers, and it’s completely senseless to apply mathematical rules for something that just isn’t math. It’s already confusing enough that we have `*` for concatenation instead of the more obvious `+`, justified by the mathematical concept of commutativity.

Strings were a concept in mathematics before they were a type in computer programs. Take a look at [Kleene Algebras](https://en.wikipedia.org/wiki/Kleene_algebra) for more of the theory, which is applicable in all sorts of ways to the task of computer programming with strings.

Mathematics is not defined in terms of numbers. That’s arithmetic.

---

<div class="post-metadata">

**Author:** ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)\
**Post date:** [February 7, 2024, 3:09pm UTC](https://discourse.julialang.org/t/left-and-right-division-for-strings/109839/24 "2024-02-07T15:09:27Z")

</div>

```julia-repl
julia> occursin(5, 123456)
true

julia> split(123.456, ".")
2-element Vector{Int64}:
 123
 456

julia> startswith(pi, 3)
true

julia> contains(pi, strip(pi, "."))
LoadError: OutOfMemoryError()
in expression starting at REPL

```

Edit: could be a fun package for inclusion in [JuliaWTF · GitHub](https://github.com/JuliaWTF)

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [February 7, 2024, 3:20pm UTC](https://discourse.julialang.org/t/left-and-right-division-for-strings/109839/25 "2024-02-07T15:20:30Z")

</div>

> [@jakobnissen](#):
>
> Edit: Whoops this was not meant as a reply to you, Valentin, but just as a post in this topic!

All good, I agree with you wholeheartedly 🙂

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [February 7, 2024, 6:50pm UTC](https://discourse.julialang.org/t/left-and-right-division-for-strings/109839/26 "2024-02-07T18:50:14Z")

</div>

> [@jakobnissen](#):
>
> It’s already confusing enough that we have `*` for concatenation instead of the more obvious `+`, justified by the mathematical concept of commutativity.

Sorry, maybe I’m missing something, but why is `+` more obvious here?

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [February 7, 2024, 8:32pm UTC](https://discourse.julialang.org/t/left-and-right-division-for-strings/109839/27 "2024-02-07T20:32:03Z")

</div>

> [@jakobnissen](#):
>
> Else, I’m going to have to retaliate and implement String-specific concepts for numbers: 😈 Surely we should have:
> 
> ```julia
> julia> reverse(123)
> 321
> 
> ```

I know you’re joking but I think that example is a useful hook for discussing the bigger idea. Regarding `reverse(::Number)`, there is no sense in which a number can be reversed by reversing the order of its digits, since (among other reasons) the value of a digit-reversed number depends on the base of its representation. For example `reverse_digits(0b10) == 0b01 == 1` whereas `reverse_digits(2) == 2`.

Strings really are mathematical objects, analyzed extensively in the study of [formal languages and their operations](https://en.wikipedia.org/wiki/Formal_language#Operations_on_languages). If we define `*` as the binary operation on a monoid – notably a much more precise and well defined concept than `Base.Number` – it makes total sense that `*` would be provided for the `(String, append)` monoid instance.

Imho the better way to oppose this proposal is to take the mathematics more seriously, not less. Namely, this proposal does not include a good response to this problem:

> [@bertschi](#):
>
> Well, it’s more like integer division though, i.e., you won’t have
> 
> ```julia
> ("abc" * "de") / "cde" == "abc" * ("de" / "cde")
> 
> ```

I think it is good for Julia to lean into generic algebraic interfaces for functions, rather than coasting on general “a matrix is kinda like a number” vibes, so we can specify strict interfaces that are computer-checkable for all instances.

This policy would be both liberating, since any type matching the contract can define methods for a generic function, and restricting, since a type that only _mostly_ conforms to the contract must not define a method for such a generic function.

[Previous page](https://discourse.julialang.org/t/left-and-right-division-for-strings/109839.md?page=1)
