Skip to content

Commit

Permalink
fix lazy eval of ors
Browse files Browse the repository at this point in the history
  • Loading branch information
smn committed May 8, 2024
1 parent d64f570 commit 88a017f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
18 changes: 16 additions & 2 deletions lib/expression/callbacks/standard.ex
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,23 @@ defmodule Expression.Callbacks.Standard do
@expression_doc expression: "or(false, false)",
code_expression: "false or false",
result: false
@expression_doc expression: "or(a, b)",
context: %{"a" => false, "b" => "bee"},
code_expression: "a or b",
result: "bee"
@expression_doc expression: "or(a, b)",
context: %{"a" => "a", "b" => false},
code_expression: "a or b",
result: "a"
@expression_doc expression: "or(b, b)",
context: %{},
code_expression: "b or b",
result: false
def or_vargs(ctx, arguments) do
arguments = eval_args!(arguments, ctx)
Enum.reduce(arguments, fn a, b -> a || b end)
Enum.reduce_while(arguments, false, fn arg, acc ->
arg = eval!(arg, ctx)
if(arg, do: {:halt, arg}, else: {:cont, acc})
end)
end

@doc """
Expand Down
16 changes: 15 additions & 1 deletion lib/expression/v2/callbacks/standard.ex
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,22 @@ defmodule Expression.V2.Callbacks.Standard do
@expression_doc expression: "or(false, false)",
code_expression: "false or false",
result: false
@expression_doc expression: "or(a, b)",
context: %{"a" => false, "b" => "bee"},
code_expression: "a or b",
result: "bee"
@expression_doc expression: "or(a, b)",
context: %{"a" => "a", "b" => false},
code_expression: "a or b",
result: "a"
@expression_doc expression: "or(b, b)",
context: %{},
code_expression: "b or b",
result: false
def or_vargs(_ctx, arguments) do
Enum.reduce(arguments, fn a, b -> a || b end)
Enum.reduce_while(arguments, false, fn arg, acc ->
if(arg, do: {:halt, arg}, else: {:cont, acc})
end)
end

@doc """
Expand Down

0 comments on commit 88a017f

Please sign in to comment.