evnu / closed_intervals

Storing a set of closed intervals
Apache License 2.0
0 stars 1 forks source link

Function get_interval_on #10

Open turion opened 5 years ago

turion commented 5 years ago

Often one wants to have a comparison function that focuses onto a specific part of a data structure, e.g. order = fn a, b -> a.idx < b.idx end from the test.

There should be a dedicated function to this, @spec get_interval_on(t(data1), (data1 -> data2)), similar to https://hexdocs.pm/elixir/Enum.html#sort_by/3.

turion commented 5 years ago

(In Haskell, this is called a "lens", I believe)

evnu commented 5 years ago

I guess that order and eq must be able to handle data2 as well, right? If so, can this be encoded in the specs somehow?

turion commented 4 years ago

I guess that order and eq must be able to handle data2 as well, right? If so, can this be encoded in the specs somehow?

I didn't think about them being polymorphic. I thought about doing this only for a tree, and passing order and eq for data2 in.

One could in principle encode a polymorphic comparison function as @spec (polydata -> polydata -> bool) when polydata: data1 | data2, data1: var, data2: var or something similar. But we can't put that spec in the definition of ClosedInterval.t because we don't know data2 there.

One alternative would be creating a ClosedInterval from a Tree.t(data1), a function data1 -> data2, and order/eq for data2. Then define order and eq for data1 accordingly.