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

Array generation #16

Open
katel0k opened this issue Jan 3, 2025 · 2 comments
Open

Array generation #16

katel0k opened this issue Jan 3, 2025 · 2 comments

Comments

@katel0k
Copy link

katel0k commented Jan 3, 2025

Hello, do you know how to generate lots of similar stuff using mlx? For example, I would like to generate a list of all users. To do that, I would just map list of users into [JSX.element] and call it a day. But for recursive generation of similar elements I would need to get around the fact that <some_recursive_func /> is passed in its parent element via list. I couldn't find if I can do that in ocaml (would like a source if you know how). Generating a range and then mapping it into elements just sounds uncool:)
It would be really nice if you implemented construction <></> like in react
Thanks for help in advance, and Happy New Year!

@andreypopp
Copy link
Member

Hi,

But for recursive generation of similar elements I would need to get around the fact that <some_recursive_func /> is passed in its parent element via list.

I didn't get this sentence, can you maybe illustrate this with pseudocode?

But in general .mlx syntax desugars into plain OCaml syntax so I think any recursive scheme is possible.

Regarding the <></> fragment syntax, I was thinking of keeping .mlx as simple as possible and thus opted out of following JSX very strictly. For fragments, something like <frag>..</frag> can be used, assuming one "binds" frag symbol to a fragment constructor, e.g. React.Fragment in React.js.

@katel0k
Copy link
Author

katel0k commented Jan 4, 2025

/Thanks for the reply! For future readers: passing a mapped list into JSX.list works nicely.

I didn't get this sentence, can you maybe illustrate this with pseudocode?

I was trying to write smth like this

let rec problem ind =
  if ind < 100 then
    <span>(JSX.string @@ string_of_int ind)</span> ::
    problem (ind + 1)
  else
    []

let () =
  <div>(problem 0)</div>

This code is supposed to generate a div with 100 spans inside. Instead, it just gives an error:

let () = ((div () ~children:[problem 0])[@JSX ])
                                  ^^^^^^^^^
Error: This expression has type JSX.element list
       but an expression was expected of type JSX.element

It would have been nice if I was able to write something like this out of the box

let rec problem ind =
  if ind < 100 then
    <frag>
      <span>(JSX.string @@ string_of_int ind)</span>
      (problem (ind + 1))
    </frag>
  else
    <frag />

It is a minor gripe though, and yesterday at 1 AM I just couldn't find a way to do it even with JSX.list, so thanks a lot for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants