Closed schanzer closed 2 years ago
Minimal program that causes the error:
builtins.raw-array-sort-nums([raw-array: "a", "b"], false)
Modes currently only works on numeric columns, as evidenced by that call to raw-array-sort-nums
at https://github.com/brownplt/pyret-lang/blob/horizon/src/arr/trove/statistics.arr#L56. Making that polymorphic over sortable values should be doable, I think, but it will be slower...
I don't need it to work on other types - throwing an error would be fine!
I'm not sure how to best give an error here; the ergonomics all are lousy.
You could fix it yourself, in the starter file ;-) You could mimic the raise
on line 198, and complain to the student that the column doesn't contain quantitative data. You could also try a refinement annotation approach, via
is-all-numbers = _.all(is-number)
fun ensure-numbers(l :: List<Number>%(is-all-numbers)): l end
# modes :: (t :: Table, col :: String) -> List<Number>
fun my-modes( t, col): S.modes( ensure-numbers(t.column(col))) end
produces
It's not perfect, since it reveals List<Number>%(is-all-numbers)
in the trace (which is a stupid-looking annotation -- isn't it redundant? -- which is why the plot library uses a hack here...), but it avoids your internal error.
I think the last option (either the raise
or refinement version) is the fastest fix for you (and you should fix it for everything in that background code that uses t.column
, so mean, median, modes, stdev, r-value
etc.).
@blerner thanks - it's really cool to see how refinements work!
Given that Pyret doesn't allow mixed-type columns, couldn't I just check the first value in the column to determine the type of the rest of the values?
I think that's likely risky: iirc, Pyret guesses what each column ought to contain, but I don't recall the heuristics it uses to do so. Checking all the values in a column is still pretty cheap, at the size of tables you're dealing with, so I'd probably just check them all.
@blerner that's helpful, as I don't have a sense for what's going to hold up CPO. Done - thank you!
Open https://code.pyret.org/editor#share=1CUza8tsxDscC1YUgp3FPM-52nAkLzaPy&v=5f98106 Evaluate
modes(movies-table, "studio")