Advertisement
Guest User

Untitled

a guest
Sep 28th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 1.81 KB | None | 0 0
  1. defmodule Editor do
  2.  
  3.     def copy("") do
  4.         {"", ""}
  5.     end
  6.    
  7.     def copy(string) do
  8.         f = String.first string
  9.         s = string_tail(string)
  10.         {f, s}
  11.     end
  12.    
  13.     defp string_tail(string) do
  14.         String.slice(string, 1..-1)
  15.     end
  16.    
  17.     def insert(string, character) do
  18.         insert(string, character, is_character(character))
  19.     end
  20.    
  21.     defp insert(string, character, true) do
  22.         string <> character
  23.     end
  24.    
  25.     defp insert(_string, _character, false) do
  26.         :error
  27.     end
  28.    
  29.     defp is_character(string) do
  30.         String.length(string) == 1
  31.     end
  32.    
  33.     def delete(string) do
  34.         string_tail(string)
  35.     end
  36.    
  37.     def edit(string, commands) do
  38.         trimmed_commands = Enum.map(commands, &String.trim/1)
  39.         edit(string, trimmed_commands, "")
  40.     end
  41.    
  42.    
  43.     defp edit(string, ["копировать"|rest], res) do
  44.         {next_string, to_res} = copy(string)
  45.         edit(to_res, rest, res <> next_string)
  46.     end
  47.    
  48.     defp edit(string, ["удалить"|rest], res) do
  49.         next_string = delete(string)
  50.         edit(next_string, rest, res)
  51.     end
  52.    
  53.     defp edit(string, [command|rest], res) do
  54.         has = String.contains?(command, "вставить")
  55.         case has do
  56.             true ->
  57.             a = String.split(command)
  58.             symbol = List.last a
  59.             next_res = insert(res, symbol)
  60.             edit(string, rest, next_res)
  61.             false -> :error
  62.         end
  63.     end
  64.    
  65.     defp edit(_string, [], res) do
  66.         res
  67.     end
  68.    
  69.     def main do
  70.         c = "копировать "
  71.         IO.puts(edit("helo wosld", [c, c, c, "вставить l  ", c, c, c, c, "вставить r", "  удалить  ", c, c]))
  72.     end
  73. end
  74.  
  75.  
  76. Editor.main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement