Skip to content

Commit d0eb9ac

Browse files
committed
fixing some of the go tour examples in a sagews
1 parent de79de0 commit d0eb9ac

File tree

1 file changed

+46
-13
lines changed

1 file changed

+46
-13
lines changed

go/go.sagews

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ Inside a function, the := short assignment statement can be used in place of a v
265265

266266
Outside a function, every construct begins with a keyword (var, func, and so on) and the := construct is not available.
267267
10985086-f7ce-4dcb-a449-f30f7896001a︡{"html":"<h1>Short variable declarations</h1>\n\n<p>Inside a function, the := short assignment statement can be used in place of a var declaration with implicit type.</p>\n\n<p>Outside a function, every construct begins with a keyword (var, func, and so on) and the := construct is not available.</p>\n"}︡
268-
4aca37c2-5b00-4be8-aff3-255bb7747a7f︠
268+
4aca37c2-5b00-4be8-aff3-255bb7747a7fs︠
269269
%go
270270
func main() {
271271
var i, j int = 1, 2
@@ -274,7 +274,7 @@ func main() {
274274

275275
fmt.Println(i, j, k, c, python, java)
276276
}
277-
f1a6048a-5929-4653-95cc-9e49d4115fb8︡{"stdout":"1 2 3 true false no!\n"}︡
277+
ededb9dd-177c-498f-809e-bb1582b19f46︡︡{"stdout":"1 2 3 true false no!\n","done":false}︡{"done":true}
278278
5b887c9c-2ca9-49f4-9ca1-2242e203c29bi︠
279279
%md
280280
# Basic types
@@ -298,7 +298,7 @@ Go's basic types are
298298
complex64 complex128
299299

300300
39cebb0b-38c0-49f0-ae9e-73b1eb9522d4︡{"html":"<h1>Basic types</h1>\n\n<p>Go&#8217;s basic types are</p>\n\n<pre><code>bool\n\nstring\n\nint int8 int16 int32 int64\nuint uint8 uint16 uint32 uint64 uintptr\n\nbyte // alias for uint8\n\nrune // alias for int32\n // represents a Unicode code point\n\nfloat32 float64\n\ncomplex64 complex128\n</code></pre>\n"}︡
301-
360bb0b3-30c4-4271-91a7-96a87431e5c3︠
301+
360bb0b3-30c4-4271-91a7-96a87431e5c3s︠
302302
%go
303303

304304
import "math/cmplx"
@@ -315,7 +315,7 @@ func main() {
315315
fmt.Printf(f, MaxInt, MaxInt)
316316
fmt.Printf(f, z, z)
317317
}
318-
d0b7913c-cec2-4094-927d-20a689c236fe︡{"stdout":"bool(false)\nuint64(18446744073709551615)\ncomplex128((2+3i))\n"}︡
318+
78d13852-4e8a-4584-aba5-670f8724d0be︡︡{"stdout":"bool(false)\nuint64(18446744073709551615)\ncomplex128((2+3i))\n","done":false}︡{"done":true}
319319
c2fbf1a4-d61e-40cb-bd43-ca154ba3b06ei︠
320320
%md
321321
# Type conversions
@@ -336,17 +336,17 @@ Or, put more simply:
336336

337337
Unlike in C, in Go assignment between items of different type requires an explicit conversion. Try removing the float64 or int conversions in the example and see what happens.
338338
73a905f2-8216-4378-8ba4-4265cc074c19︡{"html":"<h1>Type conversions</h1>\n\n<p>The expression T(v) converts the value v to the type T.</p>\n\n<p>Some numeric conversions:</p>\n\n<pre><code>var i int = 42\nvar f float64 = float64(i)\nvar u uint = uint(f)\n</code></pre>\n\n<p>Or, put more simply:</p>\n\n<pre><code>i := 42\nf := float64(i)\nu := uint(f)\n</code></pre>\n\n<p>Unlike in C, in Go assignment between items of different type requires an explicit conversion. Try removing the float64 or int conversions in the example and see what happens.</p>\n"}︡
339-
09b03af4-8108-4b74-9a6b-13650c7f41b9︠
339+
09b03af4-8108-4b74-9a6b-13650c7f41b9s︠
340340
%go
341341
import "math"
342342

343343
func main() {
344344
var x, y int = 3, 4
345-
var f float64 = math.Sqrt(float64(3*3 + 4*4))
345+
var f float64 = math.Sqrt(float64(3*3 + 4*4 + 5*5))
346346
var z int = int(f)
347347
fmt.Println(x, y, z)
348348
}
349-
cc8e9330-15c0-44c5-bb80-4fdb8b6a13bc︡{"stdout":"3 4 5\n"}︡
349+
e6789c1f-e730-4168-b96c-c7ac4135d736︡︡{"stdout":"3 4 7\n","done":false}︡{"done":true}
350350
c4fc8c59-1872-4a60-a5c3-986d86e8f1b4i︠
351351
%md
352352
# Constants
@@ -1073,12 +1073,18 @@ Let's have some fun with functions.
10731073

10741074
Implement a fibonacci function that returns a function (a closure) that returns successive fibonacci numbers.
10751075
5785dbe7-55e6-4a53-a87e-c8bcbb26c9c8︡{"html":"<h1>Exercise: Fibonacci closure</h1>\n\n<p>Let&#8217;s have some fun with functions.</p>\n\n<p>Implement a fibonacci function that returns a function (a closure) that returns successive fibonacci numbers.</p>\n"}︡
1076-
93dcf2ca-075f-4a91-b180-8bcf1579518b︠
1076+
93dcf2ca-075f-4a91-b180-8bcf1579518bs︠
10771077
%go
10781078

10791079
// fibonacci is a function that returns
10801080
// a function that returns an int.
10811081
func fibonacci() func() int {
1082+
x := 0
1083+
y := 1
1084+
return func() int {
1085+
x,y = y,x+y
1086+
return x
1087+
}
10821088
}
10831089

10841090
func main() {
@@ -1087,7 +1093,7 @@ func main() {
10871093
fmt.Println(f())
10881094
}
10891095
}
1090-
f4cae6d7-0264-455f-8cd7-cd13cf6bb229︡{"stderr":"# command-line-arguments\n./ca433c87-2dc0-4f0b-8cfa-994ab0b18de6.go:7: missing return at end of function\n"}︡
1096+
65bc8657-2ce5-41d7-b48f-f0fcdbbc712c︡︡{"stdout":"1\n1\n2\n3\n5\n8\n13\n21\n34\n55\n","done":false}︡{"done":true}
10911097
9640ed4b-54d4-419b-8872-fe3290b2ea0di︠
10921098

10931099
%md
@@ -1201,16 +1207,31 @@ $$
12011207

12021208
Find the cube root of 2, just to make sure the algorithm works. There is a [Pow](http://golang.org/pkg/math/cmplx/#Pow) function in the math/cmplx package.
12031209
4ce5402c-fc34-472a-aebd-ca6198f3ca1f︡{"html":"<h1>Advanced Exercise: Complex cube roots</h1>\n\n<p>Let&#8217;s explore Go&#8217;s built-in support for complex numbers via the <code>complex64</code> and <code>complex128</code> types. For cube roots, Newton&#8217;s method amounts to repeating:\n$$\nz = z- \\frac{z^3-x}{3z^2}\n$$</p>\n\n<p>Find the cube root of 2, just to make sure the algorithm works. There is a <a href=\"http://golang.org/pkg/math/cmplx/#Pow\">Pow</a> function in the math/cmplx package.</p>\n"}︡
1204-
18e1aef0-48bf-4db4-b9c4-ffdc68204bf9︠
1210+
18e1aef0-48bf-4db4-b9c4-ffdc68204bf9s︠
12051211
%go
12061212

1213+
import (
1214+
"fmt"
1215+
"math/cmplx"
1216+
)
1217+
12071218
func Cbrt(x complex128) complex128 {
1219+
z := complex128(2)
1220+
s := complex128(0)
1221+
for {
1222+
z = z - (cmplx.Pow(z,3) - x)/(3 * (z * z))
1223+
if cmplx.Abs(s-z) < 1e-17 {
1224+
break
1225+
}
1226+
s = z
1227+
}
1228+
return z
12081229
}
12091230

12101231
func main() {
12111232
fmt.Println(Cbrt(2))
12121233
}
1213-
728c04ea-5783-482e-9c11-276683aacc18︡{"stderr":"# command-line-arguments\n./12caf984-f34b-48cb-97be-3c32f3615f38.go:5: missing return at end of function\n"}︡
1234+
9401f4ba-4a57-4e59-a195-0e919e2e7103︡︡{"stdout":"(1.2599210498948732+0i)\n","done":false}︡{"done":true}
12141235
d308b105-6ccb-4ac7-bb75-8a14c88fcc2fi︠
12151236
%md
12161237
# Methods and Interfaces
@@ -1631,7 +1652,7 @@ Implement a `rot13Reader` that implements `io.Reader` and reads from an `io.Read
16311652

16321653
The `rot13Reader` type is provided for you. Make it an `io.Reader` by implementing its `Read` method.
16331654
f79d5829-49de-4f62-bd15-447963d64575︡{"html":"<h1>Exercise: Rot13 Reader</h1>\n\n<p>A common pattern is an <code>io.Reader</code> that wraps another <code>io.Reader</code>, modifying the stream in some way.</p>\n\n<p>For example, the <code>gzip.NewReader</code> function takes an <code>io.Reader</code> (a stream of gzipped data) and returns a <code>*gzip.Reader</code> that also implements <code>io.Reader</code> (a stream of the decompressed data).</p>\n\n<p>Implement a <code>rot13Reader</code> that implements <code>io.Reader</code> and reads from an <code>io.Reader</code>, modifying the stream by applying the ROT13 substitution cipher to all alphabetical characters.</p>\n\n<p>The <code>rot13Reader</code> type is provided for you. Make it an <code>io.Reader</code> by implementing its <code>Read</code> method.</p>\n"}︡
1634-
f264369f-15eb-4b63-966b-38c3f7e3eebc︠
1655+
f264369f-15eb-4b63-966b-38c3f7e3eebcs︠
16351656
%go
16361657

16371658

@@ -1645,13 +1666,25 @@ type rot13Reader struct {
16451666
r io.Reader
16461667
}
16471668

1669+
func (rot *rot13Reader) Read(p []byte) (n int, err error) {
1670+
n,err = rot.r.Read(p)
1671+
for i := 0; i < len(p); i++ {
1672+
if (p[i] >= 'A' && p[i] < 'N') || (p[i] >='a' && p[i] < 'n') {
1673+
p[i] += 13
1674+
} else if (p[i] > 'M' && p[i] <= 'Z') || (p[i] > 'm' && p[i] <= 'z'){
1675+
p[i] -= 13
1676+
}
1677+
}
1678+
return
1679+
}
1680+
16481681
func main() {
16491682
s := strings.NewReader(
16501683
"Lbh penpxrq gur pbqr!")
16511684
r := rot13Reader{s}
16521685
io.Copy(os.Stdout, &r)
16531686
}
1654-
fc208306-a04b-48a9-be12-462cf4b1d8ce︡{"stderr":"# command-line-arguments\n./cfc58d42-5c15-4fbe-a1fc-cd76012e544f.go:18: cannot use &r (type *rot13Reader) as type io.Reader in function argument:\n\t*rot13Reader does not implement io.Reader (missing Read method)\n"}︡
1687+
34a2c829-73e0-4a6c-a5e1-52348dd91f94︡︡{"stdout":"You cracked the code!","done":false}︡{"done":true}
16551688
109c999b-00bf-4e6c-893a-b3260fb3a6aai︠
16561689
%md
16571690
# Concurrency

0 commit comments

Comments
 (0)