![]() Or simply use pattern matching: iex> take_prefix = fn full, prefix ->. > binary_part ( full, base, byte_size ( full ) - base ). The middle of a code point made of more than one byte: iex> take_prefix = fn full, prefix ->. In this case, since we want toĮxtract a substring from a string, we can use te_size/1Īnd Kernel.binary_part/3 as there is no chance we will slice in While this is much better (we don't traverse full twice), ToĬalculate the length of the string, we need to traverse itįully, so we traverse both prefix and full strings, thenĪ first attempt at improving it could be with ranges: iex> take_prefix = fn full, prefix ->. " ) "John"Īlthough the function above works, it performs poorly. ![]() One may be tempted to write: iex> take_prefix = fn full, prefix ->. Remove this prefix from another string named full. There are many situations where using the String module canīe avoided in favor of binary functions or pattern matching.įor example, imagine you have a string prefix and you want to
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |