mpneuried
10/4/2016 - 6:51 PM

Small example how to use with

Small example how to use with

defmodule WithTest do
	def run do
		with{:ok, a} <- fun("A"),
			{:ok, b} <- fun("B"),
			{:ok, c} <- fun("C")
		do
			{:ok, [a,b,c] }
		else
			{:error, err} ->
				{:error, err}
		end
	end
	
	def runError do
		with{:ok, a} <- fun("A"),
			{:ok, b} <- fun("B"),
			{:ok, _x} <- fun(nil),
			{:ok, c} <- fun("C")
		do
			{:ok, [a,b,c] }
		else
			{:error, err} ->
				{:error, err}
		end
	end

	defp fun( nil ) do
		{:error, "NILL" }
	end

	defp fun( chr ) do
		IO.puts( chr )
		{:ok, chr}
	end

end

IO.inspect WithTest.run()
IO.inspect WithTest.runError()