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

no BatList.shuffle #702

Closed
UnixJunkie opened this issue Jan 6, 2017 · 9 comments
Closed

no BatList.shuffle #702

UnixJunkie opened this issue Jan 6, 2017 · 9 comments

Comments

@UnixJunkie
Copy link
Member

no Array.shuffle too.

@UnixJunkie
Copy link
Member Author

How about this one:

let shuffle (rand_state: Random.State.t option) (l: 'a list) =
  let () = match rand_state with
    | Some s -> Random.set_state s
    | None -> Random.self_init ()
  in
  let with_rands =
    List.map (fun i ->
        let rand = Random.bits () in
        (rand, i)
      ) l
  in
  let shuffled =
    List.sort (fun (rand0, _i) (rand1, _j) ->
        compare rand0 rand1
      ) with_rands
  in
  List.map snd shuffled

@c-cube
Copy link
Member

c-cube commented Jan 9, 2017

I'd suggest to write Array.shuffle first, then use it for shuffling lists. You can take code from https://github.com/c-cube/ocaml-containers/blob/master/src/core/CCArray.ml#L271 if you want :-)

@UnixJunkie
Copy link
Member Author

I was thinking about that since List.sort dumps the list into an array if I'm right.

@gasche
Copy link
Member

gasche commented Jan 9, 2017

To be clear, we do have a shuffle implementation, it's in the BatRandom module. (It's enum -> 'a array but internally operates on arrays, as it should)

@UnixJunkie
Copy link
Member Author

proposal here: #707

@UnixJunkie
Copy link
Member Author

Any comments? @thizanne maybe?

@UnixJunkie
Copy link
Member Author

What can I do for this to go further?

@gasche
Copy link
Member

gasche commented Jan 27, 2017

I cannot find time to look at the PR before the week-end. I hope give some cycles to Batteries at the beginning of next week.

@UnixJunkie
Copy link
Member Author

corresponding pull request was merged recently

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

3 participants