Author Topic: Constrained Genericity (Rosetta Code Tasks)  (Read 2107 times)

0 Members and 2 Guests are viewing this topic.

Charles Pegge

  • Guest
Constrained Genericity (Rosetta Code Tasks)
« on: June 26, 2012, 03:38:00 PM »

This is all about abstract programming, - or the joys of eating - whiver you prefer.

http://rosettacode.org/wiki/Constrained_genericity#OxygenBasic

Code: OxygenBasic
  1. macro Gluttony(vartype, capacity, foodlist)
  2. '==========================================
  3.  
  4. typedef vartype physical
  5.  
  6. enum food foodlist
  7.  
  8. type ActualFood
  9.   sys      name
  10.   physical size
  11.   physical quantity
  12. end type
  13.  
  14. Class foodbox
  15. '============
  16. has ActualFood Item[capacity]
  17. sys max
  18.  
  19. method put(sys f, physical s,q)
  20.   max++
  21.   Item[max]<=f,s,q
  22. end method
  23.  
  24. method GetNext(ActualFood *Stuff)
  25.   if max then
  26.     copy @stuff,@Item[max], sizeof Item
  27.     max--
  28.   end if
  29. end method
  30.  
  31. end class
  32.  
  33. Class Gourmand
  34. '=============
  35. physical WeightGain, SleepTime
  36.  
  37. method eat(ActualFood *stuff)
  38.   WeightGain+=stuff.size*stuff.quantity*0.75
  39.   stuff.size=0
  40.   stuff.quantity=0
  41. end method
  42.  
  43. end class
  44.  
  45. end macro
  46.  
  47.  
  48. 'IMPLEMENTATION
  49. '==============
  50.  
  51.  
  52. Gluttony (
  53. double,100,{
  54. oyster,trout,bloater,
  55. chocolate,truffles,
  56. cheesecake,cream,pudding,pie
  57. })
  58.  
  59. % small  1
  60. % medium 2
  61. % large  3
  62. % huge   7
  63.  
  64. % none    0
  65. % single  1
  66. % few     3
  67. % several 7
  68. % many    12
  69.  
  70. 'INSTANCE
  71. '========
  72.  
  73. FoodBox  Hamper
  74. Gourmand MrG
  75.  
  76. 'TEST
  77. '====
  78.  
  79. Hamper.put food.pudding,large,several
  80. Hamper.put food.pie,huge,few
  81. ActualFood Course
  82. Hamper.GetNext Course
  83. MrG.eat Course
  84.  
  85. print MrG.WeightGain
  86.  
  87.  


Charles