purefunction

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2024 License: BSD-3-Clause Imports: 5 Imported by: 0

README

purefunction GoDoc License Go Report Card

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

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 or 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).
Examples of pure functions

Example of a pure function:

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

Another example of a pure function (even though it is a "method"):

func (c *Config) Add(a, b int) int {
    return a + b
}
Features and limitations
  • Functions are filtered out if they have non-pure indicators. The ones that are left are considered pure.
  • Functions that read from a file, read from the keyboard, uses randomness or the system time are not pure, but may be detected as such.
Approach
Memoization

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

Benchmark output for "fibonacci" vs "memoized fibonacci":

goos: linux
goarch: amd64
pkg: github.com/xyproto/purefunction/example
BenchmarkFib10-8                   30586             38894 ns/op
BenchmarkFibMemoized-8          84117813                14.2 ns/op
PASS
ok      github.com/xyproto/purefunction/example 2.798s

See issue #1.

Requirements
  • Go 1.10 or later.
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 FunctionDecls added in v1.0.1

func FunctionDecls(filename string) ([]*ast.FuncDecl, error)

FunctionDecls returns the pure *ast.FuncDecl functions from the given source file

func Pure added in v1.0.1

func Pure(funcDecl *ast.FuncDecl, existingPureFuncDecls ...*ast.FuncDecl) bool

Pure examines if the given function is pure or not. No need to pass in existing pure function declarations, it can be skipped.

func PureFunctions

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

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

Example
filename := "testdata/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 testdata/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
cmd
pure command
pure2 command

Jump to

Keyboard shortcuts

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