I am new to functional programming and I decided to create an application in Purescript. I hit my first hurdle, and I'm not sure how to think about it conceptually.
I am not looking for code as a way to think functionally about this problem.
I have a list of data. In particular, something like
[ {a :: String, b :: String, c :: String} ]
I would like to create an Html list (which is a purescript-halogen type) using the provided entry (with a list of the above types).
So I would have a function
buildElements :: forall p i. MyRecordObject -> Array (HTML pi)
Now I think that I will need to provide this result of the result of the function in the computational context of Monad (purescript Eff is similar to Haskell IO )
So something like:
buildElements :: forall p i. MyRecordObject -> Eff (Array (HTML pi))
My first idea was vaguely around creating a list with something like
take $ length xs $ repeat ARecordObject
and then map the entry above this list, but I was not sure how to translate this into code. In any case, this was wrong, since my plan included a change in the state of ARecordObject , which does not matter.
So, I found this function:
forEach :: forall e a. Array a -> (a -> Eff e Unit) -> Eff Unit
which looks almost perfect! I get an array, I provide it with a function that somehow assigns the properties in the record to this new array ... but no, wait ... I think not functional again.
I really lost a little here. Basically, I want to create something like a list of <li></li> elements, where I assign properties to each element.
eg
I have a record:
[ { id: "id1", name: "name1", class: "class1", content: "content1" } , { id: "id2", name: "name2", class: "class2", content: "content2" } ]
And I need a foo function that returns an array:
[ li [ id_ rec.id, name_ rec.name, class_ rec.class ] [ text rec.content ] , li [ id_ rec.id, name_ rec.name, class_ rec.lass ] [ text rec.content ] ]
where rec is the name of the recordObject (and, obviously, the two arrays are not identical, but are actually mapped to the initial record).
(the dotted syntax is a syntax notation for a pyrex record similar to the standard getter / setter notation)