renderList2 : List Int -> Int -> Html msg
renderList2 lst limit =
let
attrBool x = if x > limit then True else False
in
lst
|> List.map (\i -> (String.fromInt i ++ ", "))
|> List.map (span [classList [("numberRed", True)] ] << List.singleton << text)
|> ul []
How do I pass the value of i to attrBool x so that the attrBool is True only when i > limit?
In this case, you can do a single List.map for the transformation to a string and the transformation to a span. If you do these two operations in the same function, then you keep access to i.
Here is what I mean in code:
renderList2 : List Int -> Int -> Html msg
renderList2 lst limit =
let
attrBool x = if x > limit then True else False
in
lst
|> List.map (\i -> span [classList [("numberRed", attrBool i)] ] [ text (String.fromInt i ++ ", ")])
|> ul []
You may know, but a tip if that is not the case… the if statement can also be done inline:
renderList2 : List Int -> Int -> Html msg
renderList2 lst limit =
lst
|> List.map (\i -> span [classList [("numberRed", i > limit )] ] [ text (String.fromInt i ++ ", ")])
|> ul []
It is possible, but you would have to be explicit about it. Basically, at every step of the pipeline, you’d return something (like a tuple), that contains i and the value you actually want to work on. Here is an example using a tuple:
renderList2 : List Int -> Int -> Html msg
renderList2 lst limit =
let
attrBool x = if x > limit then True else False
in
lst
|> List.map (\i -> (i, String.fromInt i ++ ", ")) -- Return a tuple
|> List.map (\(i, intAsText) -> -- Deconstructing the tuple
span [classList [("numberRed", attrBool i)] ] [text intAsText]
)
|> ul []