Advertisement
Garey

Untitled

Apr 14th, 2022
2,955
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 2.51 KB | None | 0 0
  1. defmodule Calculator do
  2.   def add(left, right), do: left + right
  3.   def sub(left, right), do: left - right
  4.   def div(left, right), do: left / right
  5.   def mul(left, right), do: left * right
  6. end
  7.  
  8. defmodule Zad2 do
  9.   def isEven(number), do: rem(number, 2) == 0
  10. end
  11.  
  12. defmodule Figures do
  13.   def squareArea(squareSide), do: squareSide ** 2
  14.   def rectangleArea(width, height), do: width * height
  15.   def checkTriangle(x, y, z) do
  16.     cond do
  17.       (x === y and y === z) -> "Equilateral"
  18.       (x === y or y === z or x === z) -> "Isosceles"
  19.       true -> "Scalene"
  20.     end
  21.   end
  22. end
  23.  
  24. defmodule Zad3 do
  25.   def mapExercise do
  26.     sample = %{:first => 1, 2 => 2, 3 => 3, 4 => 4, :last => 5}
  27.     IO.inspect(sample)
  28.  
  29.     sample = %{sample | :last => 10, :first => 50}
  30.     IO.inspect(sample)
  31.  
  32.     sample = Map.drop(sample, [:first, :last])
  33.     IO.inspect(sample)
  34.   end
  35.  
  36.   def stringInterpolation(left, right), do: left <> right
  37.  
  38.   def fileOperations do
  39.     {:ok, file} = File.open('test.txt', [:write, :read])
  40.     IO.write(file, 'sample')
  41.     File.close(file)
  42.  
  43.     {:ok, content} = File.read('test.txt')
  44.     IO.puts content
  45.   end
  46. end
  47.  
  48. defmodule Book do
  49.   defstruct isbn: '', author: '', title: '', description: '', price: 0.0, genre: ''
  50.  
  51.   defimpl String.Chars, for: Book do
  52.     def to_string(book) do
  53.       """
  54.      ISBN: #{book.isbn}
  55.      Author: #{book.author}
  56.      Title: #{book.title}
  57.      Description: #{book.description}
  58.      Price: #{book.price}
  59.      Genre: #{book.genre}
  60.      """
  61.     end
  62.   end
  63. end
  64.  
  65.  
  66.  
  67. defmodule Main do
  68.   def main do
  69.     IO.puts Calculator.add(4, 3)
  70.     IO.puts Calculator.sub(4, 3)
  71.     IO.puts Calculator.mul(4, 3)
  72.     IO.puts Calculator.div(4, 3)
  73.  
  74.     IO.puts Zad2.isEven(4)
  75.  
  76.     IO.puts Figures.squareArea(4)
  77.     IO.puts Figures.rectangleArea(4, 5)
  78.     IO.puts Figures.checkTriangle(8, 7, 5)
  79.  
  80.     #
  81.     # Zad 3
  82.     #
  83.     Zad3.mapExercise()
  84.     IO.puts Zad3.stringInterpolation("Dajba", "mamkamu")
  85.     Zad3.fileOperations()
  86.  
  87.  
  88.     # Zad 4
  89.  
  90.     book = %Book{}
  91.     IO.puts book
  92.  
  93.     book = %Book{book | author: 'Astrit Lintgren', isbn: '01111', title: 'Pipi Linstocking', description:  'It\'s about a girl names Pipi', price: 10.00, genre: 'novel'}
  94.     IO.puts book
  95.     IO.puts "Author: #{book.author}"
  96.  
  97.     stream = -5..5
  98.     newStream = Enum.map(stream, &(&1 ** 3))
  99.  
  100.     IO.inspect Enum.to_list(stream)
  101.     IO.inspect newStream
  102.  
  103.     for number <- stream,  do: if rem(number, 5) === 0 or rem(number, 2) === 0, do: IO.puts number
  104.   end
  105. end
  106.  
  107. Main.main()
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement