Skip to content

Instantly share code, notes, and snippets.

@dogweather
Last active Aug 30, 2022
Embed
What would you like to do?
Elixir implementation of five string operations
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
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