Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- defmodule Calculator do
- def add(left, right), do: left + right
- def sub(left, right), do: left - right
- def div(left, right), do: left / right
- def mul(left, right), do: left * right
- end
- defmodule Zad2 do
- def isEven(number), do: rem(number, 2) == 0
- end
- defmodule Figures do
- def squareArea(squareSide), do: squareSide ** 2
- def rectangleArea(width, height), do: width * height
- def checkTriangle(x, y, z) do
- cond do
- (x === y and y === z) -> "Equilateral"
- (x === y or y === z or x === z) -> "Isosceles"
- true -> "Scalene"
- end
- end
- end
- defmodule Zad3 do
- def mapExercise do
- sample = %{:first => 1, 2 => 2, 3 => 3, 4 => 4, :last => 5}
- IO.inspect(sample)
- sample = %{sample | :last => 10, :first => 50}
- IO.inspect(sample)
- sample = Map.drop(sample, [:first, :last])
- IO.inspect(sample)
- end
- def stringInterpolation(left, right), do: left <> right
- def fileOperations do
- {:ok, file} = File.open('test.txt', [:write, :read])
- IO.write(file, 'sample')
- File.close(file)
- {:ok, content} = File.read('test.txt')
- IO.puts content
- end
- end
- defmodule Book do
- defstruct isbn: '', author: '', title: '', description: '', price: 0.0, genre: ''
- defimpl String.Chars, for: Book do
- def to_string(book) do
- """
- ISBN: #{book.isbn}
- Author: #{book.author}
- Title: #{book.title}
- Description: #{book.description}
- Price: #{book.price}
- Genre: #{book.genre}
- """
- end
- end
- end
- defmodule Main do
- def main do
- IO.puts Calculator.add(4, 3)
- IO.puts Calculator.sub(4, 3)
- IO.puts Calculator.mul(4, 3)
- IO.puts Calculator.div(4, 3)
- IO.puts Zad2.isEven(4)
- IO.puts Figures.squareArea(4)
- IO.puts Figures.rectangleArea(4, 5)
- IO.puts Figures.checkTriangle(8, 7, 5)
- #
- # Zad 3
- #
- Zad3.mapExercise()
- IO.puts Zad3.stringInterpolation("Dajba", "mamkamu")
- Zad3.fileOperations()
- # Zad 4
- book = %Book{}
- IO.puts book
- book = %Book{book | author: 'Astrit Lintgren', isbn: '01111', title: 'Pipi Linstocking', description: 'It\'s about a girl names Pipi', price: 10.00, genre: 'novel'}
- IO.puts book
- IO.puts "Author: #{book.author}"
- stream = -5..5
- newStream = Enum.map(stream, &(&1 ** 3))
- IO.inspect Enum.to_list(stream)
- IO.inspect newStream
- for number <- stream, do: if rem(number, 5) === 0 or rem(number, 2) === 0, do: IO.puts number
- end
- end
- Main.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement