Please look at the following piece of code:
draw :: (Ord n, Show n) => StagedListVM n -> Widget n
draw state =
vLimit
(StagedListVM.rowsAmount state)
( renderList
( \_isSelected pairs ->
hBox $ map (someFunction _isSelected) pairs
)
(StagedListVM.isFocused state)
(StagedListVM.bricksList state)
)
where
someFunction _isSelected pair = StagedListVM.something
I have 2 more similar pieces of code:
draw :: (Ord n, Show n) => UnstagedListVM n -> Widget n
draw state =
vLimit
(UnstagedListVM.rowsAmount state)
( renderList
( \_isSelected pairs ->
hBox $ map (someFunction _isSelected) pairs
)
(UnstagedListVM.isFocused state)
(UnstagedListVM.bricksList state)
)
where
someFunction _isSelected pair = UnstagedListVM.something
And the last one:
draw :: (Ord n, Show n) => TreeVM n -> Widget n
draw state =
vLimit
(TreeVM.rowsAmount state)
( renderList
( \_isSelected pairs ->
hBox $ map (someFunction _isSelected) pairs
)
(TreeVM.isFocused state)
(TreeVM.bricksList state)
)
where
someFunction _isSelected pair = TreeVM.something
As you can see, they are the same, except the type of the first parameter to the draw
function. I am somewhat new to Haskell. Now, if it was some imperative OOP language, I would write one function instead of 3, let's call it superDraw
and pass it some abstract interface as a parameter.
How can I wrap these 3 functions in one function in Haskell?
Thanks.
rowsAmount
,isFocused
, etc, with each ofStagedListVM
et al. having instances of that type class. Thendraw :: (Ord n, Show n, YourTypeClass t) => t n -> Widget n
.max :: Ord a => a -> a -> a
is silently passed a comparison function as an additional argument. A callmax "a" "bc"
implicitly passes the string comparison operator.length :: [a] -> Int
implements parametric polymorphism: the definition does not depend on the specific typea
, only on the fact that it's a list.(+) :: Num a => a -> a -> a
, on the other hand, provides ad hoc polymorphism: every class that has aNum
instance defines(+)
in ana
-specific way.