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. class String
  6. def self.unfoldl(b, &block)
  7. unless (pair = yield b).nil?
  8. a, b1 = pair
  9.  
  10. self.unfoldl(b1, &block) + a
  11. else
  12. ''
  13. end
  14. end
  15. end
  16.  
  17.  
  18. DigitNames = {
  19. 0 => "Zero", 1 => "One", 2 => "Two", 3 => "Three", 4 => "Four",
  20. 5 => "Five", 6 => "Six", 7 => "Seven", 8 => "Eight", 9 => "Nine"
  21. }
  22. Numbers = [16, 58, 510]
  23.  
  24.  
  25. strings = Numbers.map { |number|
  26. String.unfoldl(number) { |num|
  27. if num <= 0
  28. nil
  29. else
  30. [DigitNames[num % 10], num / 10]
  31. end
  32. }
  33. }
  34.  
  35.  
  36. for s in strings
  37. puts s
  38. end
Success #stdin #stdout 0.01s 7412KB
stdin
Standard input is empty
stdout
OneSix
FiveEight
FiveOneZero