rescript-core icon indicating copy to clipboard operation
rescript-core copied to clipboard

Array.fromFormula with docs and tests

Open jmagaram opened this issue 1 year ago • 10 comments

This is helpful for initializing an array when you don't know exactly how many items are going to be in it. Also can be used for int/float ranges like Array.fromFormula(10, i => i <= 200 ? Some(i, i + 10) : None). F# exposes this as Array.unfold and from Googling this seems to be a frequently used term. Another possible name is fromSeed. It's like a generator but don't think we should use that name.

This is meant to round out the array creation functions. I know fromInitializer was added recently to fill this gap and I'm trying to make this more complete and useful. I would rename make and fromInitializer so we'd end up with this list below.

  let fromRepeat: (~length: int, 'a) => array<'a>
  let fromIndexMap: (~length: int, int => 'a) => array<'a>
  let fromOneItem: 'a => array<'a>
  let fromOption: option<'a> => array<'a>
  let fromFormula: ('state => option<('a, 'state)>, 'state) => array<'a>
  let fromArrayLike: Js.Array2.array_like<'a> => array<'a>
  let fromArrayLikeWithMap: (Js.Array2.array_like<'a>, 'a => 'b) => array<'b>

jmagaram avatar Mar 17 '23 23:03 jmagaram