summaryrefslogtreecommitdiff
path: root/letter_table.kpl
blob: 29856b085f4df66cd21ee1993d03ec11c8bd403d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

// Read a utf8 string and count all the occurrences of each letter found

`export letter_table : ([string]
    table : Map[Char; U64] $ ()
    @ string {[letter]
        ? table `has letter {
            ^ table `get letter {[count] count +: 1 }
        } {
            table `set (letter; 1)
        }
    }
    table
)

`is_main ([]
    `use "sys" [args]
    ? 3 != `length args {
        `return `error String $ (
            "#BOLD#RED#Got: %#\n" `format " " `join args
            "#BOLD#WHITE#Usage: % % <file>#\n" `format (args `get 0; args `get 1)
         )
    }
    `use "file" [open; read]
    `log letter_table `sync String $ read `sync open `sync args `get -1
)