Last active
June 26, 2021 22:00
-
-
Save kipcole9/9f9fc4f3f7db3bbc5c5c045d4de3d1e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Extract do | |
@moduledoc """ | |
Split into words only when the words have a "%" as a prefix | |
or a suffix | |
""" | |
# Empty string returns empty list | |
def words("") do | |
[] | |
end | |
# Skip over spaces | |
def words(<<" ", rest::binary>>) do | |
words(rest) | |
end | |
# A word that starts with a "%" prefix | |
def words(<<"%", string::binary>>) do | |
{word, rest} = extract_word(string) | |
[word | words(rest)] | |
end | |
# Start of word, but no "%" prefix | |
def words(<<string::binary>>) do | |
{word, rest} = extract_word(string) | |
case rest do | |
# The word ends in "%" so keep it | |
<<"%", rest::binary>> -> | |
[word | words(rest)] | |
# Doesn't end in "%" so discard it | |
_other -> | |
words(rest) | |
end | |
end | |
# Extract a word from a string | |
def extract_word(rest, acc \\ "") | |
# There's no string left so return | |
# whatever we accumulated | |
def extract_word("" = rest, acc) do | |
{acc, rest} | |
end | |
def extract_word(<<char::utf8, rest::binary>>, acc) when char not in [?\s, ?%] do | |
extract_word(rest, acc <> <<char::utf8>>) | |
end | |
# End of word, return the word and the | |
# rest of the string | |
def extract_word(rest, acc) do | |
{acc, rest} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some examples: