<commit_before>! Copyright (c) 2012 Anonymous
! See http://factorcode.org/license.txt for BSD license.
USING: accessors assocs fry io kernel math.parser sequences
sorting ;
IN: rosetta-code.top-rank

! http://rosettacode.org/wiki/Top_rank_per_group

! Find the top N salaries in each department, where N is
! provided as a parameter.

! Use this data as a formatted internal data structure (adapt it
! to your language-native idioms, rather than parse at runtime),
! or identify your external data source:

! Employee Name,Employee ID,Salary,Department
! <NAME>,E10297,32000,D101
! <NAME>,E21437,47000,D050
! <NAME>,E00127,53500,D101
! <NAME>,E63535,18000,D202
! <NAME>,E39876,27800,D202
! <NAME>,E04242,41500,D101
! <NAME>,E01234,49500,D202
! <NAME>,E41298,21900,D050
! <NAME>,E43128,15900,D101
! <NAME>,E27002,19250,D202
! <NAME>,E03033,27000,D101
! <NAME>,E10001,57000,D190
! <NAME>,E16398,29900,D190

TUPLE: employee name id salary department ;
 
CONSTANT: employees {
        T{ employee f "<NAME>" "E10297" 32000 "D101" }
        T{ employee f "<NAME>" "E21437" 47000 "D050" }
        T{ employee f "<NAME>" "E00127" 53500 "D101" }
        T{ employee f "<NAME>" "E63535" 18000 "D202" }
        T{ employee f "<NAME>" "E39876" 27800 "D202" }
        T{ employee f "<NAME>" "E04242" 41500 "D101" }
        T{ employee f "<NAME>" "E01234" 49500 "D202" }
        T{ employee f "<NAME>" "E41298" 21900 "D050" }
        T{ employee f "<NAME>" "E43128" 15900 "D101" }
        T{ employee f "<NAME>" "E27002" 19250 "D202" }
        T{ employee f "<NAME>" "E03033" 27000 "D101" }
        T{ employee f "<NAME>" "E10001" 57000 "D190" }
        T{ employee f "<NAME>" "E16398" 29900 "D190" }
    }

: group-by ( seq quot -- hash )
    H{ } clone [ '[ dup @ _ push-at ] each ] keep ; inline

: prepare-departments ( seq -- departments )
    [ department>> ] group-by
    [ [ salary>> ] inv-sort-with ] assoc-map ;

: first-n-each ( seq n quot -- )
    [ short head-slice ] dip each ; inline

: top-rank-main ( -- )
    employees prepare-departments [
        [ "Department " write write ":" print ] dip<commit_msg>rosetta-code.top-rank: Use collect-by instead of defining it as group-by.<commit_after>! Copyright (c) 2012 Anonymous
! See http://factorcode.org/license.txt for BSD license.
USING: accessors assocs fry io kernel math.parser sequences
sorting math.statistics ;
IN: rosetta-code.top-rank

! http://rosettacode.org/wiki/Top_rank_per_group

! Find the top N salaries in each department, where N is
! provided as a parameter.

! Use this data as a formatted internal data structure (adapt it
! to your language-native idioms, rather than parse at runtime),
! or identify your external data source:

! Employee Name,Employee ID,Salary,Department
! <NAME>,E10297,32000,D101
! <NAME>,E21437,47000,D050
! <NAME>,E00127,53500,D101
! <NAME>,E63535,18000,D202
! <NAME>,E39876,27800,D202
! <NAME>,E04242,41500,D101
! <NAME>,E01234,49500,D202
! <NAME>,E41298,21900,D050
! <NAME>,E43128,15900,D101
! <NAME>,E27002,19250,D202
! <NAME>,E03033,27000,D101
! <NAME>,E10001,57000,D190
! <NAME>,E16398,29900,D190

TUPLE: employee name id salary department ;
 
CONSTANT: employees {
        T{ employee f "<NAME>" "E10297" 32000 "D101" }
        T{ employee f "<NAME>" "E21437" 47000 "D050" }
        T{ employee f "<NAME>" "E00127" 53500 "D101" }
        T{ employee f "<NAME>" "E63535" 18000 "D202" }
        T{ employee f "<NAME>" "E39876" 27800 "D202" }
        T{ employee f "<NAME>" "E04242" 41500 "D101" }
        T{ employee f "<NAME>" "E01234" 49500 "D202" }
        T{ employee f "<NAME>" "E41298" 21900 "D050" }
        T{ employee f "<NAME>" "E43128" 15900 "D101" }
        T{ employee f "<NAME>" "E27002" 19250 "D202" }
        T{ employee f "<NAME>" "E03033" 27000 "D101" }
        T{ employee f "<NAME>" "E10001" 57000 "D190" }
        T{ employee f "<NAME>" "E16398" 29900 "D190" }
    }

: prepare-departments ( seq -- departments )
    [ department>> ] collect-by
    [ [ salary>> ] inv-sort-with ] assoc-map ;

: first-n-each ( seq n quot -- )
    [ short head-slice ] dip each ; inline

: top-rank-main ( -- )
    employees prepare-departments [
        [ "Department " write write ":" print ] dip
{'fraction_non_alphanumeric': 0.15128449096098953, 'fraction_numerical': 0.16270218839200762, 'mean_word_length': 3.4973262032085564, 'pattern_counts': {'":': 2, '<': 55, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '122455', 'n_tokens_mistral': 2002, 'n_tokens_neox': 1525, 'n_words': 425}