def unique_shortened_url_string(has_url, generate_url, length=5):
  """
  Get a unique shortened URL string.

  Args:
    has_url a function that takes a string and returns True if that
        string already exists (and can't be used).
    generate_url a function to generate a URL. Presumably, not referentially
        transparent.
    length the length of the string.
  """
  shortened = generate_url(length)
  while has_url(shortened):
    # Try again until we get a unique one.
    shortened = generate_url(length)
  return shortened