Monday, 6 June 2016

Haskell: Map employee ids to names

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")]


Previous                                                 Next                                                 Home

No comments:

Post a Comment