Receive array of either Int or Double in Scala function

Receive array of either Int or Double in Scala function



I have the following normalization function:


...
private def NormalizeValues(dataValues: Array [Double]): Array[Double] =
val min = dataValues.min
val max = dataValues.max
dataValues.map(v => (v - min) / (max - min))

...



And I want it to work whether it receives an Array of Double or an Array of Int. Better than just turning the Array of Int to Array of Double, I guess there must be a better way to do this maybe using a more generic type or specifying that Array[Doubles] and Array[Int] can be received indistinctly, in the function definition.


Array[Doubles]


Array[Int]



Do you have any idea?





Do you want to use Int's / for that case? I suspect not...
– Alexey Romanov
Sep 2 at 16:23


Int


/





What's supposed to happen with [0, 1, 3]? min = 0, max = 3, (1 - 0) / (3 - 0) = 1/3. It's not an integer. Do you want to round it, or floor it, or ceil it, or do sth. else? Integers are not a field, and therefore there is no good superclass / typeclass / interface shared by Doubles and Ints.
– Andrey Tyukin
Sep 2 at 16:24



[0, 1, 3]


min = 0


max = 3


(1 - 0) / (3 - 0) = 1/3


floor


ceil


Double


Int





That is just a function part of a class part of an preprocessing algorithm. Don't you worry about the indeterminations case, NaN values are treated inside the algorithm :)
– Cristina HG
Sep 2 at 16:29





There are two slightly different questions in your title and in the text. In the title, you ask about an "array of either Int or Double" (i.e. something like Array[Int | Double]) whereas in the text you ask about "an Array of Double or an Array of Int" (i.e. something like Array[Int] | Array[Double]). [Note that those are actually legal types in Dotty and thus likely also in Scala 3.]
– Jörg W Mittag
Sep 2 at 17:40


Array[Int | Double]


Array[Int] | Array[Double]





For implementing this idea in Scala, see the awesome answers to this scala question on Stack Overflow: stackoverflow.com/q/3508077/2988
– Jörg W Mittag
Sep 2 at 17:47




1 Answer
1



Here is a version that converts all inputs to Double, and then proceeds exactly as your original code:


Double


def normalize[N: math.Numeric](data: Array[N]): Array[Double] =
val num = implicitly[Numeric[N]]
val dataValues = data.map(num.toDouble)
val min = dataValues.min
val max = dataValues.max
dataValues.map(v => (v - min) / (max - min))



The return type is always Array[Double], because it does not seem to make any sense to return Array[N] for Ints, Longs, and all other integer types.


Array[Double]


Array[N]


Int


Long



An example:


normalize(Array[Int](0, 1, 3))
// returns: Array[Double]
// Array(0.0, 0.3333333333333333, 1.0)



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

How do I collapse sections of code in Visual Studio Code for Windows?