gofunctional2

module
v0.0.0-...-5b5dadd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 23, 2013 License: BSD-3-Clause

README

gofunctional2

This API is now deprecated in favor of gofunctional3.

Functional programming in go. The main data type, Stream, is similar to a python iterator or generator. The methods found in here are similar to the methods found in the python itertools module. This is version 2 of gofunctional.

This API is now stable. Any future changes will be backward compatible with existing code. However, any future function or data structure in "draft" mode may change in incompatible ways. Such function or data structure will be clearly marked as "draft" in the documentation.

Using

import "github.com/keep94/gofunctional2/functional"
import "github.com/keep94/gofunctional2/consume"

Installing

go get github.com/keep94/gofunctional2/functional
go get github.com/keep94/gofunctional2/consume

Online Documentation

Online documentation is available here.

Real World Example

Suppose there are names and phone numbers of people stored in a sqlite database. The table has a name and phone_number column.

The person class would look like:

type Person struct {
  Name string
  Phone string
}

func (p *Person) Ptrs() {
  return []interface{}{&p.Name, &p.Phone}
}

To get the 4th page of 25 people do:

package main

import (
  "code.google.com/p/gosqlite/sqlite"
  "github.com/keep94/gofunctional2/functional"
)

func main() {
  conn, _ := sqlite.Open("YourDataFilePath")
  stmt, _ := conn.Prepare("select * from People")
  s := functional.ReadRows(stmt)
  s = functional.Slice(s, 3 * 25, 4 * 25)
      var person Person
      err := s.Next(&person)
      for ; err == nil; err = s.Next(&person) {
        // Display person here
      }
      if err != functional.Done {
        // Do error handling here

        s.Close()
      }
    }

Like python iterators and generators, Stream types are lazily evaluated, so the above code will read only the first 100 names no matter how many people are in the database.

See tests and the included example for detailed usage.

Directories

Path Synopsis
Package consume provides useful ways to consume streams.
Package consume provides useful ways to consume streams.
examples
chkbook
checkbook is a small program that prints a checkbook register from a database.
checkbook is a small program that prints a checkbook register from a database.
power
This program demonstrates creating a Stream that emits the elements in the power set of an arbitrarily large set.
This program demonstrates creating a Stream that emits the elements in the power set of an arbitrarily large set.
power2
This example is just like power, but does not make recursive calls to functional.NewGenerator.
This example is just like power, but does not make recursive calls to functional.NewGenerator.
puzzle
This program solves the following puzzle: Find the nth digit of the sequence 12345678910111213141516...
This program solves the following puzzle: Find the nth digit of the sequence 12345678910111213141516...
Package functional provides functional programming constructs.
Package functional provides functional programming constructs.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL