By enabling XTransformListComp
extension, we can further enhance list comprehensions and perform operations by
traversing several lists at a time.
How
to enable ParallelListComp in
GHCi?
:set
-XParallelListComp
Above statement enables ParallelListComp.
How to enable ParallelListComp in Haskell
file?
By
adding following statement, you can enable ParallelListComp.
{-#
LANGUAGE ParallelListComp #-}
For
example,
you are
maintaining two lists one for employeeIds, and other for employee names, Map
employee ids to names.
ListUtil.hs
{-# LANGUAGE ParallelListComp #-} import GHC.Exts mapIdsToNames empIds empNames = [ (empId, empName) | empId <- empIds | empName <- empNames ]
*Main> :load ListUtil.hs [1 of 1] Compiling Main ( ListUtil.hs, interpreted ) Ok, modules loaded: Main. *Main> *Main> mapIdsToNames [1, 2, 3] ["Hari", "Krishna", "Silar"] [(1,"Hari"),(2,"Krishna"),(3,"Silar")]
If lists are not in same size, then exceeding elements
are ignored.
*Main> mapIdsToNames [1, 2, 3] ["Hari", "Krishna", "Silar", "Joel"] [(1,"Hari"),(2,"Krishna"),(3,"Silar")] *Main> *Main> mapIdsToNames [1, 2, 3, 4, 5] ["Hari", "Krishna", "Silar"] [(1,"Hari"),(2,"Krishna"),(3,"Silar")]
No comments:
Post a Comment