[elm-discuss] Re: Random.list

2016-11-26 Thread Max Goldstein
Oh, and I'm obliged to say: this function will never terminate if you pass 
it the wrong predicate, so be careful about that!

"Wrong" might mean unsatisfiable (or always satisfied, depending on how you 
want it to work), or not satisfied by any element generated, for example, 
(\n -> n > 100) for (Random.int 0 100).

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] Re: Random.list

2016-11-26 Thread Max Goldstein
You can do this, but you have to define something that *should* be defined 
for you (and is by random-extra and elm-random-pcg): Random.constant.

import Random

constant x = Random.map (always x) Random.bool

listUntil : Random.Generator a -> (a -> Bool) -> Random.Generator (List a)
listUntil element predicate =
  element |> Random.andThen (\elem ->
if predicate elem then
  constant [elem] -- or []
else
  Random.map (\tail -> elem :: tail) (listUntil element predicate)
  
  )

You can play with the implementation depending on whether or not you want 
the last element and whether the predicate should return True or False.

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.