purefunction

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2018 License: MIT Imports: 4 Imported by: 0

README

purefunction Build Status GoDoc License Report Card

Given a Go source file, find the names of all functions that are known to be pure.

Pure functions, like the fibonacci function, has great potential for optimization by memoization.

Uses go/ast extensively.

A "pure function" for this module is, a function that:

  • Only calls functions that are known to be pure, if any.
  • Does not read or write to any global variables.
  • Does not have pointers of slices as function arguments.
  • Does not read or write to any memory location using pointers.
  • Ideally: Always returns the same answer, given the same input, but this is hard to test for (ref. halting problem).

Example of a pure function:

func add(a, b int) int {
    return a + b
}

Functions are filtered out if they have non-pure indicators. The ones that are left are considered pure. There may be false negatives, but not false positives.

General info

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var Verbose bool

Verbose can be set to true for output to stdout while processing source files

Functions

func PureFunctions

func PureFunctions(filename string) ([]string, error)

PureFunctions returns a slice with the function names that are considered pure

Example
filename := "test/main.go"
pureFunctions, err := PureFunctions(filename)
if err != nil {
	log.Fatalln(err)
}
fmt.Printf("Pure functions in %s:\n", filename)
sort.Strings(pureFunctions)
for _, name := range pureFunctions {
	fmt.Println(name)
}
Output:

Pure functions in test/main.go:
add
add2
mul
mul3

func SetVerbose

func SetVerbose(verbose bool)

SetVerbose can be used for enabling or disabling verbose output to stdout

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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