Skip to Content
Learning Functional Programming in Go
book

Learning Functional Programming in Go

by Lex Sheehan
November 2017
Intermediate to advanced
670 pages
17h 35m
English
Packt Publishing
Content preview from Learning Functional Programming in Go

Testing iterators for element equality

First, let's create the testIterEq function to test whether two collections are equivalent:

func testIterEq(t *testing.T, it1, it2 Iter) {     t.Log("Start")     for el1 := range it1 {            if el2, ok := <- it2; !ok {                   t.Error("it2 shorter than it1!", el1)                   return            } else if !reflect.DeepEqual(el1, el2) {                   t.Error("Elements are not equal", el1, el2)            } else {                   t.Log(el1, el2)            }     }     if el2, ok := <- it2; ok {            t.Error("it1 shorter than it2!", el2)     }     t.Log("Stop")}

In our test function TestMap, we define a mapper function literal that is passed to our Map function to perform the transformation. The mapper function returns the length of each string passed to it:

func TestMap(t *testing.T) { mapper := func (i interface{}) interface{} ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Functional Programming

Learning Functional Programming

Jack Widman

Publisher Resources

ISBN: 9781787281394Supplemental Content