# Any package or provision to suggest alternative/fast code

**URL:** https://discourse.julialang.org/t/any-package-or-provision-to-suggest-alternative-fast-code/105610
**Category:** Performance
**Tags:** question, performance
**Created:** [October 31, 2023, 8:53am UTC](https://discourse.julialang.org/t/any-package-or-provision-to-suggest-alternative-fast-code/105610 "2023-10-31T08:53:28Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![VinodV](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinodv/32/32733_2.png) [@VinodV](https://discourse.julialang.org/u/VinodV)
#### Post date: [October 31, 2023, 8:53am UTC](https://discourse.julialang.org/t/any-package-or-provision-to-suggest-alternative-fast-code/105610/1 "2023-10-31T08:53:29Z")

</div>

Suppose I have written a piece of code to check whether a string say “malayalam” is a palindrome.

```julia
s = "malayalam"
s == reverse(a)

```

.  
An alternative is `s == s[end:-1:1]`.

Is there a package or provision to suggest alternates, efficient code in Julia.  
My question is in general sense for any piece of code.

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [October 31, 2023, 12:16pm UTC](https://discourse.julialang.org/t/any-package-or-provision-to-suggest-alternative-fast-code/105610/2 "2023-10-31T12:16:01Z")

</div>

I don’t know if this is possible in general. In a general case, it’s unclear if `s[end:-1:1]` and `reverse(s)` are equivalent, let alone to judge which one is more efficient. There are certain patterns that the VS Code linter suggests.

---

<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: [October 31, 2023, 1:59pm UTC](https://discourse.julialang.org/t/any-package-or-provision-to-suggest-alternative-fast-code/105610/3 "2023-10-31T13:59:42Z")

</div>

> [@jishnub](#):
>
> In a general case, it’s unclear if `s[end:-1:1]` and `reverse(s)` are equivalent

`s[end:-1:1]` is _not_ functionally equivalent to `reverse(s)` in general, because `s[end:-1:1]` is **incorrect** for non-ASCII strings:

```julia
julia> s = "αβγ"
"αβγ"

julia> reverse(s)
"γβα"

julia> s[end:-1:1]
ERROR: StringIndexError: invalid index [4], valid nearby indices [3]=>'β', [5]=>'γ'

```

The problem is that for non-ASCII strings, the [string indices are not consecutive integers](https://docs.julialang.org/en/v1/manual/strings/#Unicode-and-UTF-8):

```julia
julia> collect(eachindex(s))
3-element Vector{Int64}:
 1
 3
 5

```

More generally, if you are checking for palindromes that might include non-ASCII strings, even `reverse(s)` might not do quite what you want. The `reverse(s)` function reverses the codepoints of a string — mainly useful for reverse-order string processing such as regex searches (see the discussion in [julia#6165](https://github.com/JuliaLang/julia/issues/6165)) — but as [explained in the manual](https://docs.julialang.org/en/v1/base/strings/#Base.reverse-Tuple%7BUnion%7BSubString%7BString%7D,%20String%7D%7D) this may not correspond to your intuitive notion of string reversal for strings containing combining characters. In that case, you might want to reverse graphemes instead:

```julia
julia> using Unicode

julia> ispalindrome(s::AbstractString) = s == join(reverse(collect(graphemes(s))))

julia> ispalindrome("ax̂a")
true

julia> reverse("ax̂a") == "ax̂a"
false

julia> reverse("ax̂a")
"âxa"

```

Unicode makes string processing more complicated than most people realize.

---

<div class="post-metadata">

### Author: ![VinodV](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinodv/32/32733_2.png) [@VinodV](https://discourse.julialang.org/u/VinodV)
#### Post date: [October 31, 2023, 3:36pm UTC](https://discourse.julialang.org/t/any-package-or-provision-to-suggest-alternative-fast-code/105610/4 "2023-10-31T15:36:12Z")

</div>

In general, is there a package or tool which suggests alternate for any piece of code.

---

<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: [October 31, 2023, 3:56pm UTC](https://discourse.julialang.org/t/any-package-or-provision-to-suggest-alternative-fast-code/105610/5 "2023-10-31T15:56:38Z")

</div>

> [@VinodV](#):
>
> In general, is there a package or tool which suggests alternate for any piece of code.

Well, people use LLM machine-learning tools for this sort of thing sometimes, ala ChatGPT. [_Caveat emptor_](https://en.wikipedia.org/wiki/Caveat_emptor), though — those tools work best when you ask about coding problems for which there are zillions of similar examples online that it can interpolate from, but they will also cheerfully and confidently give you wrong answers.

For example, “write julia code to check if a string is a palindrome” in ChatGPT gives:

```julia
function is_palindrome(str)
    # Remove spaces and convert the string to lowercase for a case-insensitive check
    str = replace(lowercase(str), r"\s+" => "")
    
    # Compare the original string with its reverse
    return str == reverse(str)
end

```

which is pretty reasonable, but it doesn’t handle multi-character graphemes properly. If you tell it “fix it to reverse graphemes”, it changes the last line to `str == reverse(graphemes(str))`, which is incorrect code because `graphemes` returns an iterator that is not reversible unless you `collect` it first.
