12

XY problem

How do I convert an array to a list in PureScript?

arrayToList :: forall a. Array a -> List a
arrayToList = ???

Actual problem

Must I necessarily write this function?

Neither purescript-arrays nor purescript-lists define such a function, which leads me wonder if there is an idiomatic way to deal with arrays in the contexts of functions taking a list.

For example Matrix.getRow returns an array which needs to be transformed into a list of Pux Html elements (in the process of rendering the matrix as HTML). What is the best way to do this?

Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187

1 Answers1

18

With compiler version 0.10.2, you can simply write

arrayToList :: forall a. Array a -> List a
arrayToList = ?whatGoesHere

and the compiler will give you a list of things to fill in, based on the type information. ?whatGoesHere is called a typed hole.

In this case, you probably want Data.Array.toUnfoldable or Data.List.fromFoldable.

Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
Phil Freeman
  • 4,199
  • 1
  • 20
  • 15
  • 1
    Just needed a function `Data.List.Lazy.List a -> Data.Array a`, and `toUnfoldable` worked as well. – stholzm Nov 10 '16 at 08:16