Last active
August 30, 2022 06:21
-
-
Save dogweather/2069230dd8755f6fa7f6e89c0ab2c1cc to your computer and use it in GitHub Desktop.
Elixir implementation of five string operations
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
import String | |
defmodule StringOps do | |
def clean_up(title) do | |
title | |
|> fixWhitespace | |
|> fixHyphenation | |
|> splitIntoSentences | |
|> List.first | |
end | |
def fixWhitespace(s), do: replace(s, "\n", " ") | |
def fixHyphenation(s), do: replace(s, "- ", "") | |
def splitIntoSentences(a_string) do | |
a_string | |
|> split(". ") | |
|> Enum.map(&ensure_ends_with_period/1) | |
end | |
def ensure_ends_with_period(sentence) do | |
sentence <> if ends_with?(sentence, "."), do: "", else: "." | |
end | |
end |
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
import StringOps | |
defmodule StringOpsTest do | |
use ExUnit.Case | |
test "fixHyphenation rejoins a word" do | |
assert fixHyphenation("auto- mobile") == "automobile" | |
end | |
test "fixWhitespace changes a newline to a space" do | |
assert fixWhitespace("and\nthe story") == "and the story" | |
end | |
test "clean_up handles extra text" do | |
input = "Relating to the state transient lodging tax; creating\nnew provisions; amending ORS 284.131 and\n320.305; prescribing an effective date; and pro-\nviding for revenue raising that requires approval\nby a three-fifths majority.\nWhereas Enrolled House Bill 2267 (chapter 818," | |
assert clean_up(input) == "Relating to the state transient lodging tax; creating new provisions; amending ORS 284.131 and 320.305; prescribing an effective date; and providing for revenue raising that requires approval by a three-fifths majority." | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment