Headline

Reusable abstractions for accessing company data

Characteristics

Feature:Total is an example of an operation on company salaries. Other options on company salaries are conceivable, too; see, for example, Feature:Median. Feature:Cut is an example of an operation for transforming companies in the salary position. Other options on company salaries are conceivable, too; see, for example, Feature:Cut. It is quite common to set up reusable abstractions (potentially higher-order functions) to process heterogeneous data structures in all kinds of ways. That is, we generalize over cutting salaries by setting up a transformation function which is parameterized in the function to be applied in salary positions and we generalizing over totaliong salaries by setting up a function to extract all salaries as a list.

Illustration

We total salaries by leveraging the extraction of salaries from companies:

total :: Company -> Float
total = sum . getSalariesFromCompany

Computing the median salary can rely on the same function for salary extraction:

median :: Company -> Float
median c = sort ss!!(length ss `div` 2)
  where
    ss = getSalariesFromCompany c

We cut salaries by leveraging the function for transforming salaries in companies:

cut :: Company -> Company
cut = transformSalariesInCompany (/2) 

Raising salaries can rely on the same function for transforming salaries in companies:

raise :: Company -> Company
raise = transformSalariesInCompany (*1.01) 

Architecture

Salary extraction

getSalariesFromCompany :: Company -> [Salary]
getSalariesFromCompany (Company n ds) = concat ds'
  where
    ds' = map fromD ds
    fromD (Department n m ds es) = m' : concat ds' ++ es'
      where
        m' = fromE m
        ds' = map fromD ds
        es' = map fromE es
        fromE (Employee _ _ s) = s

Salary transformation

transformSalariesInCompany :: (Salary -> Salary) -> Company -> Company
transformSalariesInCompany f (Company n ds) = Company n ds'
  where
    ds' = map inD ds
    inD (Department n m ds es) = Department n m' ds' es'
      where
        m' = inE m
        ds' = map inD ds
        es' = map inE es
        inE (Employee n a s) = Employee n a (f s)

Discussion

Heterogeneous data structures such as companies breaking down into departments, employees, names, addresses, and salaries. We could set up transoformation and extraction helpers for other ingredients of compoanies. See Contribution:haskellFunctorial for an alternative approach of organizing access to positions of a certain type. Ultimately, we could leverage Theme:Haskell genericity to perform traversals on heterogeneous data structures; see, for example, Contribution:haskellSyb.


Ralf Lämmel edited this article at Sun, 09 Jul 2023 17:53:21 +0200
Compare revisions Compare revisions

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.