Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed src to be well done with tests #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions about_allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ package go_koans
func aboutAllocation() {
a := new(int)
*a = 3
assert(*a == __int__) // new() creates a pointer to the given type, like malloc() in C
assert(*a == 3) // new() creates a pointer to the given type, like malloc() in C

type person struct {
name string
age int
}
bob := new(person)
assert(bob.age == __int__) // it can allocate memory for custom types as well
assert(bob.age == 0) // it can allocate memory for custom types as well

slice := make([]int, 3)
assert(len(slice) == __int__) // make() creates slices of a given length
assert(len(slice) == 3) // make() creates slices of a given length

slice = make([]int, 3, __positive_int__) // but can also take an optional capacity
slice = make([]int, 3, 20) // but can also take an optional capacity
assert(cap(slice) == 20)

m := make(map[int]string)
assert(len(m) == __int__) // make() also creates maps
assert(len(m) == 0) // make() also creates maps
}
6 changes: 3 additions & 3 deletions about_anonymous_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ func aboutAnonymousFunctions() {
}
increment()

assert(i == __int__) // closures function in an obvious way
assert(i == 2) // closures function in an obvious way
}

{
Expand All @@ -18,12 +18,12 @@ func aboutAnonymousFunctions() {
}
increment(i)

assert(i == __int__) // although anonymous functions need not always be closures
assert(i == 1) // although anonymous functions need not always be closures
}

{
double := func(x int) int { return x * 2 }

assert(double(3) == __int__) // they can do anything our hearts desire
assert(double(3) == 6) // they can do anything our hearts desire
}
}
35 changes: 17 additions & 18 deletions about_arrays.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,31 @@ import "fmt"
func aboutArrays() {
fruits := [4]string{"apple", "orange", "mango"}

assert(fruits[0] == __string__) // indexes begin at 0
assert(fruits[1] == __string__) // one is indeed the loneliest number
assert(fruits[2] == __string__) // it takes two to ...tango?
assert(fruits[3] == __string__) // there is no spoon, only an empty value
assert(fruits[0] == "apple") // indexes begin at 0
assert(fruits[1] == "orange") // one is indeed the loneliest number
assert(fruits[2] == "mango") // it takes two to ...tango?
assert(fruits[3] == "") // there is no spoon, only an empty value
assert(len(fruits) == 4) // the length is what the length is
assert(cap(fruits) == 4) // it can hold no more

assert(len(fruits) == __int__) // the length is what the length is
assert(cap(fruits) == __int__) // it can hold no more

assert(fruits == [4]string{}) // comparing arrays is not like comparing apples and oranges
assert(fruits != [4]string{}) // comparing arrays is not like comparing apples and oranges

tasty_fruits := fruits[1:3] // defining oneself as a variation of another
assert(fmt.Sprintf("%T", tasty_fruits) == __string__) //and get not a simple array as a result
assert(tasty_fruits[0] == __string__) // slices of arrays share some data
assert(tasty_fruits[1] == __string__) // albeit slightly askewed
assert(fmt.Sprintf("%T", tasty_fruits) == "[]string") //and get not a simple array as a result
assert(tasty_fruits[0] == "orange") // slices of arrays share some data
assert(tasty_fruits[1] == "mango") // albeit slightly askewed

assert(len(tasty_fruits) == __int__) // its length is manifest
assert(cap(tasty_fruits) == __int__) // but its capacity is surprising!
assert(len(tasty_fruits) == 2) // its length is manifest
assert(cap(tasty_fruits) == 3) // but its capacity is surprising!

tasty_fruits[0] = "lemon" // are their shared roots truly identical?

assert(fruits[0] == __string__) // has this element remained the same?
assert(fruits[1] == __string__) // how about the second?
assert(fruits[2] == __string__) // surely one of these must have changed
assert(fruits[3] == __string__) // but who can know these things
assert(fruits[0] == "apple") // has this element remained the same?
assert(fruits[1] == "lemon") // how about the second?
assert(fruits[2] == "mango") // surely one of these must have changed
assert(fruits[3] == "") // but who can know these things

veggies := [...]string{"carrot", "pea"}

assert(len(veggies) == __int__) // array literals need not repeat an obvious length
assert(len(veggies) == 2) // array literals need not repeat an obvious length
}
30 changes: 17 additions & 13 deletions about_basics.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
package go_koans

func aboutBasics() {
assert(__bool__ == true) // what is truth?
assert(__bool__ != false) // in it there is nothing false
assert(__bool__ != true) // what is truth?
assert(__bool__ == false) // in it there is nothing false

var i int = __int__
assert(i == 1.0000000000000000000000000000000000000) // precision is in the eye of the beholder
assert(i != 1.0000000000000000000000000000000000000) // precision is in the eye of the beholder
assert(i == -1)

k := __int__ //short assignment can be used, as well
assert(k == 1.0000000000000000000000000000000000000)
assert(k != 1.0000000000000000000000000000000000000)
assert(k == -1)

assert(5%2 == __int__)
assert(5*2 == __int__)
assert(5^2 == __int__)
assert(5%2 != __int__)
assert(5*2 != __int__)
assert(5^2 != __int__)

var x int
assert(x == __int__) // zero values are valued in Go
assert(x != __int__) // zero values are valued in Go
assert(x == 0)

var f float32
assert(f == __float32__) // for types of all types
assert(f != __float32__) // for types of all types
assert(f == 0)

var s string
assert(s == __string__) // both typical or atypical types
assert(s == "") // both typical or atypical types

var c struct {
x int
f float32
s string
}
assert(c.x == __int__) // and types within composite types
assert(c.f == __float32__) // which match the other types
assert(c.s == __string__) // in a typical way
assert(c.x == 0) // and types within composite types
assert(c.f == 0) // which match the other types
assert(c.s == "") // in a typical way
}
22 changes: 16 additions & 6 deletions about_channels.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
package go_koans

//import "fmt"

func aboutChannels() {
ch := make(chan string, 2)

assert(len(ch) == __int__) // channels are like buffers
assert(len(ch) == 0) // channels are like buffers

ch <- "foo" // i mean, "metaphors are like similes"

assert(len(ch) == __int__) // they can be queried for queued items
assert(len(ch) == 1) // they can be queried for queued items

assert(<-ch == __string__) // items can be popped out of them
assert(<-ch == "foo") // items can be popped out of them

assert(len(ch) == __int__) // and len() always reflects the "current" queue status
assert(len(ch) == 0) // and len() always reflects the "current" queue status

// the 'go' keyword runs a function-call in a new "goroutine"
// which executes "concurrently" with the calling "goroutine"
go func() {
// your code goes here
<- ch
}()

assert(__delete_me__) // we'll need to make room for the queue, or suffer deadlocks
// or for more than one over cap item
//go func() {
// for {
// select {
// case x := <-ch:
// }
// }
//
//}()

ch <- "bar" // this send will succeed
ch <- "quux" // there's enough room for this send too
Expand Down
4 changes: 2 additions & 2 deletions about_common_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func aboutCommonInterfaces() {
$ open http://localhost:8080/pkg/bytes/
*/

assert(out.String() == "hello world") // get data from the io.Reader to the io.Writer
assert(out.String() == "") // get data from the io.Reader to the io.Writer
}

{
Expand All @@ -27,6 +27,6 @@ func aboutCommonInterfaces() {

out := new(bytes.Buffer)

assert(out.String() == "hello") // duplicate only a portion of the io.Reader
assert(out.String() == "") // duplicate only a portion of the io.Reader
}
}
8 changes: 4 additions & 4 deletions about_concurrency.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ func isPrimeNumber(possiblePrime int) bool {

func findPrimeNumbers(channel chan int) {
for i := 2; ; /* infinite loop */ i++ {
// your code goes here

if isPrimeNumber(i) {
channel <- i
}
assert(i < 100) // i is afraid of heights
}
}

func aboutConcurrency() {
ch := make(chan int)

assert(__delete_me__) // concurrency can be almost trivial
// your code goes here
go findPrimeNumbers(ch)

assert(<-ch == 2)
assert(<-ch == 3)
Expand Down
18 changes: 9 additions & 9 deletions about_control_flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import "fmt"
func aboutControlFlow() {
{
a, b, c := 1, 2, 3
assert(a == __int__) // multiple assignment
assert(b == __int__) // can make
assert(c == __int__) // life easier
assert(a == 1) // multiple assignment
assert(b == 2) // can make
assert(c == 3) // life easier
}

var str string
Expand All @@ -18,14 +18,14 @@ func aboutControlFlow() {
} else {
str = "baby dont hurt me"
}
assert(str == __string__) // no more
assert(str == "baby dont hurt me") // no more

if length := len(str); length == 17 {
str = "to be"
} else {
str = "or not"
}
assert(str == __string__) // that is the question
assert(str == "to be") // that is the question
}

{
Expand All @@ -39,23 +39,23 @@ func aboutControlFlow() {
case fmt.Sprintf("%s%s", hola1, hola2):
str = "senor"
}
assert(str == __string__) // cases can be of any type, even arbitrary expressions
assert(str == "hi") // cases can be of any type, even arbitrary expressions

switch {
case false:
str = "first"
case true:
str = "second"
}
assert(str == __string__) // in the absence of value, there is truth
assert(str == "second") // in the absence of value, there is truth
}

{
n := 0
for i := 0; i < 5; i++ {
n += i
}
assert(n == __int__) // for can have the structure with which we are all familiar
assert(n == 10) // for can have the structure with which we are all familiar
}

{
Expand All @@ -66,6 +66,6 @@ func aboutControlFlow() {
break
}
}
assert(n == __int__) // though omitting everything creates an infinite loop
assert(n == 32) // though omitting everything creates an infinite loop
}
}
6 changes: 3 additions & 3 deletions about_defer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ func aboutDefer() {
defer increment(1)
}()

assert(acc == __int__) // defer function will be execute after main function body
assert(acc == 1) // defer function will be execute after main function body

func() {
acc = 0
defer increment(5)
defer decrement(3)
}()

assert(acc == __int__) // list of functions also allowed
assert(acc == 2) // list of functions also allowed

func() {
defer panicRecover()
acc = 0
panic("Expected error")
}()

assert(acc == __int__) // executed even in case of panic
assert(acc == 1) // executed even in case of panic

}
6 changes: 3 additions & 3 deletions about_enumeration.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ func aboutEnumeration() {
concatenated += v
}

assert(concatenated == __string__) // for loops have a modern variation
assert(total == __int__) // which offers both a value and an index
assert(concatenated == "hello world!") // for loops have a modern variation
assert(total == 3) // which offers both a value and an index
}

{
Expand All @@ -23,6 +23,6 @@ func aboutEnumeration() {
totalLength += len(v)
}

assert(totalLength == __int__) // although we may omit either value
assert(totalLength == 12) // although we may omit either value
}
}
2 changes: 1 addition & 1 deletion about_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ func aboutFiles() {
filename := "about_files.go"
contents, _ := ioutil.ReadFile(filename)
lines := strings.Split(string(contents), "\n")
assert(lines[5] == __string__) // handling files is too trivial
assert(lines[5] == "func aboutFiles() {") // handling files is too trivial
}
6 changes: 3 additions & 3 deletions about_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ func aboutInterfaces() {
bob := new(human) // bob is a kind of *human
rspec := new(program) // rspec is a kind of *program

assert(runner(bob) == __runner__) // conformed interfaces need not be declared, they are inferred
assert(runner(bob) == bob) // conformed interfaces need not be declared, they are inferred

assert(bob.milesCompleted == 0)
assert(rspec.executionCount == 0)

runTwice(bob) // bob fits the profile for a 'runner'
runTwice(rspec) // rspec also fits the profile for a 'runner'

assert(bob.milesCompleted == __int__) // bob is affected by running in his own unique way (probably fatigue)
assert(rspec.executionCount == __int__) // rspec can run completely differently than bob, thanks to interfaces
assert(bob.milesCompleted == 2) // bob is affected by running in his own unique way (probably fatigue)
assert(rspec.executionCount == 2) // rspec can run completely differently than bob, thanks to interfaces
}

// abstract interface and function that requires it
Expand Down
Loading