Skip to content
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

Implement some methods on Literal::Array #143

Merged
merged 11 commits into from
Nov 14, 2024

Conversation

christopher-b
Copy link
Contributor

@christopher-b christopher-b commented Nov 11, 2024

Implementation and tests for:

  • Literal::Array#*
  • Literal::Array#+
  • Literal::Array#-

Closes #152
Closes #151
Closes #153
Closes #156
Closes #160
Closes #162
Closes #184

lib/literal/array.rb Outdated Show resolved Hide resolved
Comment on lines 80 to 81
when Literal::Array
# Do nothing
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move line 86 in here?

Also, I think above this, we can handle the case that we’re adding an another Literal::Array of the same type. Should be able to do when @__generic__ (see above).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joeldrapper Yes, it ends up looking either like:

def +(other)
  case other
  when ::Array
    other_literal = @__generic__.new(*other)
    Literal::Array.new(@__value__ + other_literal.__value__, type: @__type__)
  when Literal::Array
    Literal::Array.new(@__value__ + other.__value__, type: @__type__)
  else
    raise ArgumentError.new("Cannot perform + with #{other.class.name}.")
  end
end

or

def +(other)
  case other
  when ::Array
    values = @__value__ + @__generic__.new(*other).__value__
  when Literal::Array
    values = @__value__ + other.__value__
  else
    raise ArgumentError, "Cannot perform `+` with #{other.class.name}."
  end

  Literal::Array.new(values, type: @__type__)
end

I'm partial to the second, but let me know which style you prefer.

@christopher-b christopher-b marked this pull request as ready for review November 13, 2024 16:01
def +(other)
case other
when ::Array
values = @__value__ + @__generic__.new(*other).__value__
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably check other against the collection type and then use __with__ to return a Literal::Array. I believe this will return an Array at the moment.

Copy link
Contributor Author

@christopher-b christopher-b Nov 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This creates a temporary variable values (perhaps poorly named, too close to __values__) that is used to initialize a new Literal::Array on line 101. So the type checking is deferred to the instantiation of the new Literal::Array at the end of the method.

I think my error here is that I'm creating a new Literal::Array twice. This should work as just:

values = @__value__ + other

And the return value could actually be:

@__generic__.new(*values)

So the whole method would be

def +(other)
	case other
	when ::Array
		values = @__value__ + other
	when Literal::Array
		values = @__value__ + other.__value__
	else
		raise ArgumentError.new("Cannot perform `+` with #{other.class.name}.")
	end

	@__generic__.new(*values)
end

Does this approach make sense; should we be checking types inside the case...when?

@joeldrapper
Copy link
Owner

I’m going to merge this and then take a look at +. I want to remove @__generic__. After benchmarking, it looks like it's not worth storing that object.

@joeldrapper joeldrapper merged commit 78253ef into joeldrapper:main Nov 14, 2024
1 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants