Elixir Strings
I was reading through the Elixir docs and found some interesting code snippets in the `String` section. The example was how we could implement a function that returns the ending of a string.
So in other words, we want a function like this:
take_prefix.("Mr. John", "Mr. ")
# returns 'John'
The docs actually show a few solutions, gradually iterating on each, improving it slowly. I really liked how they did it, but thought that each solution could use a bit more detail.
This post will take the first naive solution and talk about it. This solution is probably the most intuitive, so is a nice way to ease into Elixir strings.
Solution
take_prefix = fn full, prefix ->
base = String.length(prefix)
String.slice(full, base, String.length(full) - base)
end
IO.puts take_prefix.("Mr. John", "Mr. ")
Let's jump right in.
First line
take_prefix = fn full, prefix ->
The first line declares the function. We want a function that takes in two parameters, the full/entire string, and the prefix that we want chopped off.
Second line
base = String.length(prefix)
This line figures out how long the prefix string is. There is something to note here, and that this function `String.length` needs to traverse the entire string in order to figure out it's length. The reason for this is that some letters are made up of two characters, but are perceived by humans as one.
One example is this letter:
# é
iex> String.codepoints("é")
["e", "́"]
Two characters used to represent one. So the `String.length` function has no choice but to traverse the entire string to check for weird conditions like this. As a result, as the string gets longer, this function call takes longer to complete as it has more characters to check.
Third line
String.slice(full, base, String.length(full) - base)
The next line is a bit compact, but let's dig in and see if we can't unpack it.
Let's start inside and go out.
String.length(full) - base
We saw the first part before. But now we are taking the length of the full/entire string. And from that we are subtracting out the length of the prefix.
So from our example, the length of our full string is ("Mr. John") is 8, and the length of the prefix ("Mr. ") is 4. Simple subtraction (8-4) and we have 4.
Our call really then is:
String.slice(full, 4, 4)
Looking up the docs for that:
slice(string, start, len)
Returns a substring starting at the offset start, and of length len
In plain English, this method says, "take the string Mr. John and starting at the 4th element (0 based), give me back a string 4 characters long".
Fourth line
IO.puts take_prefix.("Mr. John", "Mr. ")
And the last line shows the invocation of the method and the write to the console.
Tada. But as you may have noticed, this method is expensive, mainly due to the two length calls we have. And then we have an additional slice call that has to yet again traverse the string in order to give us the substring. For short strings, this solution is no problem. But when you get longer strings, this method will not be so great. There are ways we can improve on this, which we will take a look at next time.
No comments:
Post a Comment