rsc.io/go@v0.0.0-20150416155037-e040fd465409/doc/codewalk/functions.xml (about)

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