[Closed] mini-challenge #5
here is a little problem … how can you make an unique and sorted array from an array that contains any possible iteratable number(!) values…
here is a sample:
[left]
#(7, point3 7 1.6 12, red, x_axis, #{87..100}, [1,17], #(#(#(1, "Hi!"))))
[/left]
You’ll have to define some logic, won’t you? Is 7 bigger than red? What is your intended output?
Agreed, what’s the sample output? Should the original values be kept or is the goal a sorted array of the extracted numbers, types unchanged (int, float etc.)? In that case, here’s the obvious solution (I guess the hint is they are all iterables but I can’t think of any universal approach):
mapped fn flatten arr buffer = case of
(
(isKindOf arr Number) : append buffer arr
(isKindOf arr BitArray) : join buffer arr
(isKindOf arr Array) : flatten arr buffer
(isKindOf arr Color) : join buffer #(arr.r, arr.g, arr.b, arr.a)
(isProperty arr #x) :
(
join buffer #(arr.x, arr.y)
if isProperty arr #z do append buffer arr.z
if isProperty arr #w do append buffer arr.w
)
)
fn sortNumbers arr buffer:#() =
(
flatten arr buffer
makeUniqueArray (sort buffer)
)
sortNumbers #(7, point3 7 1.6 12, red, x_axis, #{87..100}, [1,17], #(#(#(1, "Hi!"))))
color has to be excluded from the list… it does’t have get function, which means the color[i] doesn’t work…
but matrix3 works for addition.
Wouldn’t it be useful if you would put the expected output result for the given example?
#(7, point3 7 1.6 12, red, x_axis, #{87..100}, [1,17], #(#(#(1, "Hi!"))))
I honestly have no idea what you’re looking to achieve. You’ll need to post sample output and explain it.
the final output for the sample is
#(0.0, 1.0, 1.6, 7, 12.0, 17.0, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100)
red, and “Hi!” were wiped because one doesn’t have get method, and another returns not number
red[1] -- not works
"Hi!"[1] == "H" -- not number
for example (matrix3 1) is good:
(matrix3 1)[1] == [1,0,0] -- OK
([1,0,0])[1] == 1.0 -- OK
--so
(matrix3 1) => 1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0 => #(0.0,1.0)
where has this task came from …
there are several methods that returns vertices as Point3, or bitarray, or array, or single index… you can handle every case individually but… maybe there is a universal solution
I’m still confused as to why you would need to sort this type of array in the first place?
the sorting is not really important and it’s easy to do. but it makes sense if you are talking about indexes for example… to make the final array unique is absolutely necessary.