Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- defmodule Editor do
- def copy("") do
- {"", ""}
- end
- def copy(string) do
- f = String.first string
- s = string_tail(string)
- {f, s}
- end
- defp string_tail(string) do
- String.slice(string, 1..-1)
- end
- def insert(string, character) do
- insert(string, character, is_character(character))
- end
- defp insert(string, character, true) do
- string <> character
- end
- defp insert(_string, _character, false) do
- :error
- end
- defp is_character(string) do
- String.length(string) == 1
- end
- def delete(string) do
- string_tail(string)
- end
- def edit(string, commands) do
- trimmed_commands = Enum.map(commands, &String.trim/1)
- edit(string, trimmed_commands, "")
- end
- defp edit(string, ["копировать"|rest], res) do
- {next_string, to_res} = copy(string)
- edit(to_res, rest, res <> next_string)
- end
- defp edit(string, ["удалить"|rest], res) do
- next_string = delete(string)
- edit(next_string, rest, res)
- end
- defp edit(string, [command|rest], res) do
- has = String.contains?(command, "вставить")
- case has do
- true ->
- a = String.split(command)
- symbol = List.last a
- next_res = insert(res, symbol)
- edit(string, rest, next_res)
- false -> :error
- end
- end
- defp edit(_string, [], res) do
- res
- end
- def main do
- c = "копировать "
- IO.puts(edit("helo wosld", [c, c, c, "вставить l ", c, c, c, c, "вставить r", " удалить ", c, c]))
- end
- end
- Editor.main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement