Search code examples
rascal

Convert list[value] to map[value,int countOccurences]


Is there an elegant way to count duplicates and write the result to a map.

I know how to do this with traditional loops etc, but I am simply very curious: is there a nice way (eg oneliner) to do this using e.g. comprehensions, reducers or a more traditional Rascal API.

So e.g. convert:

list[loc] 

To

map[loc location,int nrOfOccurrencesInListOfThisLocationKey]

Tx,

Jos


Solution

  • You are looking for the distribution function

    map[&T element, int occurs] distribution(list[&T] lst)
    

    in the List library