fork download
  1. # Swift's closure
  2. # https://d...content-available-to-author-only...e.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html
  3.  
  4.  
  5. def unfoldl_string(b, f):
  6. pair = f(b)
  7.  
  8. if pair is None:
  9. return ""
  10. else:
  11. a, b1 = pair
  12.  
  13. return unfoldl_string(b1, f) + a
  14.  
  15.  
  16. digitNames = {
  17. 0: "Zero", 1: "One", 2: "Two", 3: "Three", 4: "Four",
  18. 5: "Five", 6: "Six", 7: "Seven", 8: "Eight", 9: "Nine"
  19. }
  20. numbers = [16, 58, 510]
  21.  
  22.  
  23. def number_to_string(number):
  24. return unfoldl_string(
  25. number,
  26. lambda num:
  27. None
  28. if num <= 0 else
  29. (digitNames[num % 10], num / 10)
  30. )
  31. strings = [number_to_string(number) for number in numbers]
  32.  
  33.  
  34. for s in strings:
  35. print(s)
Success #stdin #stdout 0.01s 7896KB
stdin
Standard input is empty
stdout
OneSix
FiveEight
FiveOneZero