gofp

command
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2023 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Generates functional code locally for user defined data type.

```

  1. Install "gofp" to generate code go get github.com/logic-building/functional-go/gofp go get -u github.com/logic-building/functional-go/gofp go install github.com/logic-building/functional-go/gofp
  1. Add this line in a file where user defined data type exists //go:generate gofp -destination <file> -pkg <pkg> -type <Types separated by comma>

example:

 package employee

//go:generate gofp -destination fp.go -pkg employee -type "Employee, Teacher"
type Employee struct {
	id     int
	name   string
	salary float64
}

type Teacher struct {
	id     int
	name   string
	salary float64
}

Note:

A. fp.go               :  generated code
B. employee            :  package name
C. "Employee, Teacher" :  User defined data types
  1. Generate functional code go generate ./...

4. Now write your code

 emp1 := employee.Employee{1, "A", 1000}
	emp2 := employee.Employee{1, "A", 1000}
	emp3 := employee.Employee{1, "A", 1000}

	empList := []employee.Employee{emp1, emp2, emp3}

	newEmpList := employee.Map(incrementSalary, empList) //  Returns: [{1 A 1500} {1 A 1500} {1 A 1500}]

func incrementSalary(emp employee.Employee) employee.Employee {
     emp.Salary = emp.Salary + 500
     return emp
}

```

Jump to

Keyboard shortcuts

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