-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add SWITCH #16
Add SWITCH #16
Conversation
class Switch < ::XlsFunction::Evaluators::FunctionEvaluator | ||
function_as :switch | ||
|
||
MAX_ARGUMENTS_COUNT = 254 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MAX_ARGUMENTS_COUNT
is total value below.
- Value to switch (1)
- What value do you want to match (126 * 2 = 252)
- Default value to return if there's no match found (1)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MAX_ARGUMENTS_COUNT.times do |n| | ||
text_name = :"condition#{n + 1}" | ||
define_arg text_name | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should take efficiency into account because this function has variable arguments.
Could you try to re-consider implementing as short-circuit ? Please refer to the ifs
function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fixed it using ifs
as a reference.
9a6db25
半分くらい理解できて半分くらいわからないので明日聞く😇 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1点だけコメントしました🙏
|
||
def not_match(args) | ||
args.delete_at(1) | ||
args.length.odd? ? args.last : nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[質問]
こちらargs.delete_at(1)する理由とかありますかね?
delete_atしなくて
args.length.even? ? args.last : nil
↑でいい気がしまして、、
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fixed it using ifs
as a reference.
9a6db25
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's becoming more easier to understand!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return value&.evaluate(context) if expr&.evaluate(context) == condition | ||
|
||
return expr&.evaluate(context) if value&.evaluate(context).nil? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expr&.evaluate(context)
runs twice. Plz fix it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fixed in the commit below.
b066e5d
|
||
arg_list[1..-1].each_slice(2) do |expr, value| | ||
count += 1 | ||
break if count > 126 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[imo] Original one is restricted to 126 as you implemented. But I think our function does not need to be restricted as well. I don't restrict similar functions such as ifs
and let
. How do you think ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First of all, I tried to reproduce it faithfully, but I also think that restrictions are unnecessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I deleted it in the commit below.
4a009cd
return expr&.evaluate(context) if value&.evaluate(context).nil? | ||
end | ||
|
||
XlsFunction::ErrorValue.na |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to add some messages with class_info. See also other ErrorValue usage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a message in the commit below.
a230f0f
end | ||
|
||
XlsFunction::ErrorValue.na | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗒
- 基本的に関数追加する場合は、 eval メソッドの中に関数処理を実装すれば良い
- arg_list で入力した式をいい感じに取得できる
xls_function/lib/xls_function/evaluators/function_evaluator.rb
Lines 16 to 18 in 3b304f6
def arg_list @arg_list ||= Array(context[:arglist]) end - expr: 一致する値、 value: 一致した時に出力したい値、が入る想定
- value&.evaluate(context).nil? であれば、最後の一致する値がない場合に返す値とみなしている
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Background
It is difficult to produce the required output with the current function.
This can be solved by using
SWITCH
function.Changes
If there are more than 126 arguments and there is no matching value up to the 126th argument, returns
"#N/A"
.