All Projects → sasha-s → go-inline

sasha-s / go-inline

Licence: other
Generic Data Structures/Algorithms in golang.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to go-inline

Syncmap
A typed implementation of the Go sync.Map using code generation
Stars: ✭ 200 (+284.62%)
Mutual labels:  generics, code-generation
Goreuse
Generic Code for Go
Stars: ✭ 93 (+78.85%)
Mutual labels:  generics, code-generation
Gen
Type-driven code generation for Go
Stars: ✭ 1,246 (+2296.15%)
Mutual labels:  generics, code-generation
Gena
Generic pseudo-templated containers for C. Written entirely in C89 with design inspired by the C++ STL. /// DOCS ARE SLIGHTLY OUTDATED, PROJECT IS STABLE AND STILL IN ACTIVE DEVELOPMENT
Stars: ✭ 61 (+17.31%)
Mutual labels:  templates, generics
genx
GenX: Generics For Go, Yet Again.
Stars: ✭ 37 (-28.85%)
Mutual labels:  generics
go-callbag
golang implementation of Callbag
Stars: ✭ 19 (-63.46%)
Mutual labels:  generics
laboratory
Feature flags for multi-module Kotlin Android projects
Stars: ✭ 71 (+36.54%)
Mutual labels:  code-generation
rueidis
A Fast Golang Redis RESP3 client that supports Client Side Caching, Auto Pipelining, Generics OM, RedisJSON, RedisBloom, RediSearch, RedisAI, RedisGears, etc.
Stars: ✭ 422 (+711.54%)
Mutual labels:  generics
schema-gen
XML Schema code generator outputting Swift, Kotlin and Java
Stars: ✭ 25 (-51.92%)
Mutual labels:  code-generation
typing-jekyll-template
💉 Typing, is a template for Jekyll built especially for those who want to have a blog and pages quickly and lightly.
Stars: ✭ 84 (+61.54%)
Mutual labels:  templates
node
npm's fork of nodejs/node, for sending PRs to update deps/npm
Stars: ✭ 30 (-42.31%)
Mutual labels:  templates
openvalidation
Compose validation rules in the language you use every day, openVALIDATION handles code creation for you.
Stars: ✭ 62 (+19.23%)
Mutual labels:  code-generation
PyRates
Open-source, graph-based Python code generator and analysis toolbox for dynamical systems (pre-implemented and custom models). Most pre-implemented models belong to the family of neural population models.
Stars: ✭ 33 (-36.54%)
Mutual labels:  code-generation
create-graphql-app
Cli tool for bootstrapping serverless GraphQL api
Stars: ✭ 28 (-46.15%)
Mutual labels:  code-generation
libgen
Automatic C-bindings generator for the Crystal language
Stars: ✭ 71 (+36.54%)
Mutual labels:  code-generation
LinqToXsdCore
LinqToXsd ported to .NET Core (targets .NET Standard 2 for generated code and .NET Core 3.1, .NET 5+ 6 for the code generator CLI tool).
Stars: ✭ 23 (-55.77%)
Mutual labels:  code-generation
Pode.Web
Web template framework for use with the Pode PowerShell web server
Stars: ✭ 83 (+59.62%)
Mutual labels:  templates
pystella
A code generator for grid-based PDE solving on CPUs and GPUs
Stars: ✭ 18 (-65.38%)
Mutual labels:  code-generation
django-renderpdf
📄 A Django app to render django templates as PDF files.
Stars: ✭ 37 (-28.85%)
Mutual labels:  templates
layouts
Wraps templates with layouts. Layouts can use other layouts and be nested to any depth. This can be used 100% standalone to wrap any kind of file with banners, headers or footer content. Use for markdown, HTML, handlebars views, lo-dash templates, etc. Layouts can also be vinyl files.
Stars: ✭ 28 (-46.15%)
Mutual labels:  templates

go-inline

Generic Data Structures/Algorithms in golang via code generation (glorified copy&paste).

One might want to use goinline tool to generate specific implementation of generic data structures, such as map, vector, matrix, set. Or to generate specific implementation of generic algorithms, such as sort, binary search.

Goals:

  • Write code once
  • Simple, readable and tested tested go code (e.g. no templates/language extensions)
  • Type safety and speed all the way (e.g. no reflection)
  • No magic, everything is explicit
How-to

Install goinline.

go get github.com/sasha-s/go-inline/goinline/cmd/goinline
Creating a blueprint.

Blueprint is a package that implements a certain generic data type/algorithm.

Let us consider Vector[Number].

package vector

type Number float64

type Vector []Number

func (v Vector) Sum() Number {
    var s Number
    for _, x := range v {
        s += x
    }
    return s
}

func (v Vector) Dot(w Vector) Number {
    var s Number
    for i, x := range v {
        s += x * w[i]
    }
    return s
}
...

Number is a type parameter: we want to be able to use derived versions Vector[float32] or Vector[int].

Note, that Number is a standalone type. This makes it easy to generate the derived versions.

Since we are using + and * the Vector blueprint can be only used with ints and floats.

Have a look at more complete example

Generating derived code.

In case of Vector[Number]:

goinline -package=github.com/sasha-s/go-inline/examples/blueprints/vector --target-package-name=v -target-dir=. -w "Number->int"

This will use a blueprint from package github.com/sasha-s/go-inline/examples/blueprints/vector, looking for the it in $GOPATH, create a generated version in current (.) folder, will rename the package to v and will replace Number with int in the generated code.

Equivalent go:generate directive: //go:generate goinline -package=github.com/sasha-s/go-inline/examples/blueprints/vector --target-package-name=v -target-dir=. -w Number->int

See vector example.

Note, goinline does not check if the blueprint code compiles. Garbage in, garbage out.

Writing tests so they work after inlining types is tricky, so goinline does not generate tests by default.

goinline tool
goinline -h
Usage of goinline:
  -package="": package to use as a blueprint. Something like `github.com/sasha-s/go-inline/examples/blueprints/concurrentmap`
  -target-dir="": where to put the generated code. Will modify the blueprint (according to package) if empty
  -target-package-name="": package name for the generated code. Ignored if empty
  -tests=false: process tests as well
  -w=false: write result to a file instead of stdout

One could get similar results with some scripting around gofmt -r, given that the blueprints are well-structured.

FAQ
  • Why is there no generics in go?
  • How is it different from
  • copy&paste: c&p is painful to maintain, though if the blueprint is well-structured, the copy&paste is easy.
  • gen: gen works with text/template, goinline starts with working, testable go code, so creating the blueprints is easier and cleaner.
  • goast: goast tries to be smart and infer things, goinline is very explicit. Also, goinline code is shorter and simpler.
  • gotemplate: goinline is more explicit, works with multiple files per blueprint. More details.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].