_swap([], current) do current end defp _swap([a], current) do current ++ [a] end defp _swap([a, b | tail], current) do _swap(tail, current ++ [b, a]) end end
do msg = "Please agree to continue." render(conn, "new.html", alert: msg) end def register(conn, params) do create_user_in_db(params) redirect_to_profile(conn) end end Logic branch Logic trunk
salary end def change_salary_by(amt) @salary = @salary + amt end def description "#{@name} makes #{@salary}" end end bob = Employee.new("Bob", 10_000) bob.change_salary(1000) puts bob.description # "Bob makes 11000" def change_salary_by(amt) # make function pure by returning a new object self.class.new(@name, @salary + amt) end bob = Employee.new("Bob", 10_000) bob = bob.change_salary_by(1000) FP way
salary: 0 def change_salary_by(person, amt) do Map.update(person, :salary, amt, &(&1 + amt)) end def description(person) do "#{person.name} makes #{person.salary}" end end %Employee{name: "Bob", salary: 10_000} |> Employee.description() |> Employee.change_salary_by(1000) |> IO.puts # "Bob makes 11000"