purefunction

Given a Go source code file, find all known pure functions

BSD-3-CLAUSE License

Stars
6
Committers
1

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