def sequence_to_autocomplete(sequence, *, fuzzy=False):
    """
    Converts a sequence into an autocomplete function.

    By default, matching is at the front only, i.e. if I type 'Af' it will suggest
    'Afghanistan' but not 'Central African Republic'. To match anywhere in the input,
    set the `fuzzy` parameter to True.

    Matching is case-insensitive.
    """
    sequence = [x.lower() for x in sequence]

    def autocomplete(chars):
        chars = chars.lower()
        prefix_matches = []
        substring_matches = []
        for x in sequence:
            if x.startswith(chars):
                prefix_matches.append(x)
            elif fuzzy and chars in x:
                substring_matches.append(x)

        return prefix_matches + substring_matches

    return autocomplete