Closed paf31 closed 7 years ago
That's a great idea. Definitely a bit more complex than the current components, but it should be possible.
Probably the best way to solve this would be to figure out how to implement Alternative
for UI
and then many
would fall naturally out of that.
Here's a simple sketch of what the user interface might look like that would be compatible with what @paf31 wants: if you have two controls, x
and y
then x <|> y
is a panel that begins with x
and has something like a [+]
button in the top-left corner. If you click that button then the panel changes to y
and the [+]
button changes to [-]
. If you click the [-]
button then the panel switches back to x
.
So if you combine that with Haskell's definition for many
it should do exactly what @paf31 wants. I'm not sure if this would work with Purescript since Haskell's many
depends crucially on laziness, though.
@Gabriel439 Thank you! That sounds like a really interesting approach.
Here is what I found out so far:
I managed to write an Alt
instance for UI
(which provides <|>
) in the way that you proposed. However, the instance does not fulfill the associativity law:
x <|> (y <|> z)
is a UI with a single [+]
button (initially)(x <|> y) <|> z
is a UI with two [+]
buttonsFor the List
UI, we want the right-associative (first) option. <|>
is implemented as left-associative in PureScript. Not really a big problem, but a little unfortunate..
Plus
instance, which would need to provide empty :: forall a. UI a
is not possible, I think (Plus
is needed for Alternative
). On the other hand, it seems that empty
is not needed to implement some
and many
(maybe the constraints could actually be loosened in PureScript?).many
with a Lazy
constraint, but I have no idea how a Lazy
instance for UI
would look like.If I understand you (and the implementation of some
and many
) correctly, many (int_ 0) :: UI (Array Int)
should expand to something like this (cut-off after three iterations):
pure []
<|> sequence [int_ 0]
<|> sequence [int_ 0, int_ 0]
<|> sequence [int_ 0, int_ 0, int_ 0]
Running this UI actually works (if I add some parentheses to make <|>
right-associative), but it suffers from two problems:
[+]/[-]
buttons are shown at the top.Hmmm. You're right. I didn't think that through.
It seems like if there were some sort of [+]
-button functionality it would have to be provided by some separate combinator and not implicitly by the Alternative
class operations.
I'll have to think about this some more and experiment with this in typed-spreadsheet
first.
This was something I wanted also, so I took a stab at it. I didn't use Alternative or anything, just recursively nested UIs that match the List structure. It works nicely, but the code might could use a review. PR incoming...
Closed via #26
I don't know if this is possible but something like
might be nice. It could be rendered as a list of items, each with its own Remove button, and an Add button at the top.