cactoos icon indicating copy to clipboard operation
cactoos copied to clipboard

Scalar/Iterable conversion

Open andreoss opened this issue 5 years ago • 6 comments

It seems that a higher rate of reuse is possible between Scalar and Iterable decorators if there's a clear way to convert one to another.

Proposal:

Let's introduce

  • IterableOfScalar(Scalar<T>)
  • ScalarOfIterable(Iterable<T>)

A more radical change would be to extend Scalar<T> with Iterable<T>, which would make iterable.* decorators such as Mapped applicable to any Scalar. I.e new ScalarOfIterable<>(new Mapped<>(..., new ScalarOf<>(...))) // no need for scalar.Mapped

andreoss avatar Nov 11 '20 09:11 andreoss

@andreoss IMHO extend Scalar<T> with Iterable<T> is no way but I'd like to ask you:

  1. Can we add a new ctor in IterableOf to put a Scalar<T> instead of create a new class?
  2. Can we add a new ctor in ScalarOf to put a Iterable<T> instead of create a new class?

fabriciofx avatar Nov 12 '20 11:11 fabriciofx

@fabriciofx Overloaded ctors would be ambiguous

  1. IterableOf<>(Scalar<T>) could be as well Iterable<Scalar<T>>
  2. ScalarOf<>(Iterbale<T>) could be Scalar<Iterable<T>>

andreoss avatar Nov 12 '20 11:11 andreoss

@andreoss how would ScalarOfIterable behave for a scalar with multiple values?

I like the idea anyway, it makes sense!

Concerning constructors vs classes, it's always been a problem to decide between the two in many cases, we will see if it is possible to do it with constructor but as you said there is the ambiguity problem, so introducing classes should be acceptable :)

victornoel avatar Nov 14 '20 16:11 victornoel

@victornoel I believe ScalarOfIterable should work only on single-element Iterable, thowring NoSuchElement and IllegalArgumentExeception otherwise. Scalar which iterates over Iterable doesn't make much sense

andreoss avatar Nov 14 '20 21:11 andreoss

@andreoss

Scalar which iterates over Iterable doesn't make much sense

Yes, that was my main problem with it.

I believe ScalarOfIterable should work only on single-element Iterable, thowring NoSuchElement and IllegalArgumentExeception otherwise.

So if the Iterable is empty, NoSuchElement or IllegalArgumentException is good, but if there is more than one element, I don't think it is really needed to test for it. And so in that case, there is already FirstOf (we should just add a constructor taking only an Iterable).

victornoel avatar Nov 15 '20 14:11 victornoel

@andreoss let's do the following, what do you think:

  • using FirstOf taking a single Iterable to get the functionality of your proposed ScalarOfIterable
  • continue using an Iterator to have something to iterate over an Iterable
  • introducing IterableOfScalar taking a Scalar as your propose
    • Too bad IterableOf already takes a Scalar of Iterator, Java and its type erasure really sucks sometimes

As for having Scalar extend Iterable, this is not such a bad idea actually, a Scalar is an Iterable of exactly one element. But I'm always worried about introducing more methods to a type hierarchy, even if they are just interfaces. So let's go with the delegation instead of the inheritance :)

victornoel avatar Dec 28 '20 14:12 victornoel