parameter (option string); storage (pair (pair nat nat) (map nat string)); return (option string); code { DUP; CAR; # Retrieving an element IF_NONE { CDR; DUP; CAR; DIP{CDR; DUP}; DUP; CAR; SWAP; DIP{GET}; # Check if an element is available SWAP; # Put NONE on stack and finish IF_NONE { NONE string; DIP{PAIR}; PAIR} # Reoption the element and remove the entry from the map { SOME; DIP{ DUP; DIP{ CAR; DIP{ NONE string }; UPDATE }; # Increment the counter and cleanup DUP; CAR; PUSH nat 1; ADD; DIP{ CDR }; PAIR; PAIR}; PAIR }} # Arrange the stack { DIP{DUP; CDAR; DIP{CDDR}; DUP}; SWAP; CAR; # Add the element to the map DIP{ SOME; SWAP; CDR; DUP; DIP{UPDATE}; # Increment the second number PUSH nat 1; ADD}; # Cleanup and finish PAIR; PAIR; NONE string; PAIR }}