Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support list components #2

Closed
paf31 opened this issue Nov 21, 2015 · 6 comments
Closed

Support list components #2

paf31 opened this issue Nov 21, 2015 · 6 comments

Comments

@paf31
Copy link

paf31 commented Nov 21, 2015

I don't know if this is possible but something like

many :: forall a. a -> UI a -> UI (List a)

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.

@sharkdp
Copy link
Owner

sharkdp commented Nov 22, 2015

That's a great idea. Definitely a bit more complex than the current components, but it should be possible.

@Gabriella439
Copy link

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.

@sharkdp
Copy link
Owner

sharkdp commented Jan 3, 2016

@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 [+] buttons

    For 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..

  • Writing a 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?).

  • PureScript provides 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:

  • All [+]/[-] buttons are shown at the top.
  • Rather obviously, state is not maintained (in all cases). If I enter a number in the list with two elements and switch to the three-element input, everything gets reset.

@Gabriella439
Copy link

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.

@sammthomson
Copy link
Contributor

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...

@sharkdp
Copy link
Owner

sharkdp commented Jul 30, 2017

Closed via #26

@sharkdp sharkdp closed this as completed Jul 30, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants