curry

command module
v0.0.0-...-d6b5ee3 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2017 License: BSD-3-Clause Imports: 15 Imported by: 0

README

curry — Curried function generator for Go

by Brandon Dyck <brandon@dyck.us>

For documentation, see http://godoc.org/bitbucket.org/brandondyck/curry.

Documentation

Overview

Curry generates curried wrappers for functions. It is meant to be used with `go generate`.

Curry parses all Go source files in the current working directory; looks for a function/method with the given name and, if applicable, receiver type; and generates a file containing a curried wrapper for the function/method and all necessary imports. To simplify its implementation, curry does not allow files with dot imports.

For example, given a file containing

package circus

import "image/color"

type Clown struct {
	name string
	hair color.Color
	height uint
}

func NewClown(name string, hair color.Color, height uint) (*Clown, error) {
	return &Clown{name, hair, height}, nil
}

running the command

curry -fromFunc NewClown -toFunc NewClownC

will generate a file containing

package circus

import "image/color"

func NewClownC(name string) func(color.Color) func(uint) (*Clown, error) {
	return func(hair color.Color) func(uint) (*Clown, error) {
		return func(height uint) (*Clown, error) {
			return NewClown(name, hair, height)
		}
	}
}

To curry a method, use dotted notation to prepend the receiver type to the source function name. If the method has a pointer type, do not include the asterisk. For example, given the method

func (c *Clown) HonkNose(...) {
	...
}

the command

curry -fromFunc Clown.HonkNose -toFunc HonkNoseC

would generate the wrapper

func(c *Clown) HonkNoseC(...) {
	...
}

The output file defaults to ./func_curry.go or ./recv_meth_curry.go, where func/meth is the name of the function/method and recv is the receiver type. To override this behavior, specify a filename with the -output option.

I do not expect curry to be especially useful, as I have never actually needed to heavily use partial application in Go programs. I just wanted to explore the go/ast and go/parser packages a little.

Jump to

Keyboard shortcuts

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