functions.xml 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <codewalk title="First-Class Functions in Go">
  2. <step title="Introduction" src="doc/codewalk/pig.go">
  3. Go supports first class functions, higher-order functions, user-defined
  4. function types, function literals, closures, and multiple return values.
  5. <br/><br/>
  6. This rich feature set supports a functional programming style in a strongly
  7. typed language.
  8. <br/><br/>
  9. In this codewalk we will look at a simple program that simulates a dice game
  10. called <a href="http://en.wikipedia.org/wiki/Pig_(dice)">Pig</a> and evaluates
  11. basic strategies.
  12. </step>
  13. <step title="Game overview" src="doc/codewalk/pig.go:/\/\/ A score/,/thisTurn int\n}/">
  14. Pig is a two-player game played with a 6-sided die. Each turn, you may roll or stay.
  15. <ul>
  16. <li> If you roll a 1, you lose all points for your turn and play passes to
  17. your opponent. Any other roll adds its value to your turn score. </li>
  18. <li> If you stay, your turn score is added to your total score, and play passes
  19. to your opponent. </li>
  20. </ul>
  21. The first person to reach 100 total points wins.
  22. <br/><br/>
  23. The <code>score</code> type stores the scores of the current and opposing
  24. players, in addition to the points accumulated during the current turn.
  25. </step>
  26. <step title="User-defined function types" src="doc/codewalk/pig.go:/\/\/ An action/,/bool\)/">
  27. In Go, functions can be passed around just like any other value. A function's
  28. type signature describes the types of its arguments and return values.
  29. <br/><br/>
  30. The <code>action</code> type is a function that takes a <code>score</code>
  31. and returns the resulting <code>score</code> and whether the current turn is
  32. over.
  33. <br/><br/>
  34. If the turn is over, the <code>player</code> and <code>opponent</code> fields
  35. in the resulting <code>score</code> should be swapped, as it is now the other player's
  36. turn.
  37. </step>
  38. <step title="Multiple return values" src="doc/codewalk/pig.go:/\/\/ roll returns/,/true\n}/">
  39. Go functions can return multiple values.
  40. <br/><br/>
  41. The functions <code>roll</code> and <code>stay</code> each return a pair of
  42. values. They also match the <code>action</code> type signature. These
  43. <code>action</code> functions define the rules of Pig.
  44. </step>
  45. <step title="Higher-order functions" src="doc/codewalk/pig.go:/\/\/ A strategy/,/action\n/">
  46. A function can use other functions as arguments and return values.
  47. <br/><br/>
  48. A <code>strategy</code> is a function that takes a <code>score</code> as input
  49. and returns an <code>action</code> to perform. <br/>
  50. (Remember, an <code>action</code> is itself a function.)
  51. </step>
  52. <step title="Function literals and closures" src="doc/codewalk/pig.go:/return func/,/return roll\n\t}/">
  53. Anonymous functions can be declared in Go, as in this example. Function
  54. literals are closures: they inherit the scope of the function in which they
  55. are declared.
  56. <br/><br/>
  57. One basic strategy in Pig is to continue rolling until you have accumulated at
  58. least k points in a turn, and then stay. The argument <code>k</code> is
  59. enclosed by this function literal, which matches the <code>strategy</code> type
  60. signature.
  61. </step>
  62. <step title="Simulating games" src="doc/codewalk/pig.go:/\/\/ play/,/currentPlayer\n}/">
  63. We simulate a game of Pig by calling an <code>action</code> to update the
  64. <code>score</code> until one player reaches 100 points. Each
  65. <code>action</code> is selected by calling the <code>strategy</code> function
  66. associated with the current player.
  67. </step>
  68. <step title="Simulating a tournament" src="doc/codewalk/pig.go:/\/\/ roundRobin/,/gamesPerStrategy\n}/">
  69. The <code>roundRobin</code> function simulates a tournament and tallies wins.
  70. Each strategy plays each other strategy <code>gamesPerSeries</code> times.
  71. </step>
  72. <step title="Variadic function declarations" src="doc/codewalk/pig.go:/\/\/ ratioS/,/string {/">
  73. Variadic functions like <code>ratioString</code> take a variable number of
  74. arguments. These arguments are available as a slice inside the function.
  75. </step>
  76. <step title="Simulation results" src="doc/codewalk/pig.go:/func main/,/\n}/">
  77. The <code>main</code> function defines 100 basic strategies, simulates a round
  78. robin tournament, and then prints the win/loss record of each strategy.
  79. <br/><br/>
  80. Among these strategies, staying at 25 is best, but the <a
  81. href="http://www.google.com/search?q=optimal+play+pig">optimal strategy for
  82. Pig</a> is much more complex.
  83. </step>
  84. </codewalk>