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_rec__(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. def __unfoldl_string_while__(b, f):
  17. s = ""
  18. pair = f(b)
  19.  
  20. while not(pair is None):
  21. a, b1 = pair
  22.  
  23. s += a
  24. pair = f(b1)
  25.  
  26. return s
  27.  
  28.  
  29. # unfoldl_string = __unfoldl_string_rec__
  30. unfoldl_string = __unfoldl_string_while__
  31.  
  32.  
  33. digitNames = {
  34. 0: "Zero", 1: "One", 2: "Two", 3: "Three", 4: "Four",
  35. 5: "Five", 6: "Six", 7: "Seven", 8: "Eight", 9: "Nine"
  36. }
  37. numbers = [16, 58, 510]
  38.  
  39.  
  40. def number_to_string(number):
  41. return unfoldl_string(
  42. number,
  43. lambda num:
  44. None
  45. if num <= 0 else
  46. (digitNames[num % 10], num / 10)
  47. )
  48. strings = [number_to_string(number) for number in numbers]
  49.  
  50.  
  51. for s in strings:
  52. print(s)
Success #stdin #stdout 0.01s 7852KB
stdin
Standard input is empty
stdout
SixOne
EightFive
ZeroOneFive