Giter Site home page Giter Site logo

peds's Introduction

Statically type safe persistent/immutable/functional data structures for Go.

Inspired by Clojures data structures and the work done in Pyrsistent for Python.

This is an experiment in how close to generics that code generation can take you. There's currently a vector, a slice, a map and a set implemented.

What's a persistent data structure?

Despite their name persistent data structures usually don't refer to data structures stored on disk. Instead they are immutable data structures which are copy-on-write. Eg. whenever you mutate a persistent data structure a new one is created that contains the data in the original plus the mutation while the original is left untouched.

To make this reasonably efficient, "structural sharing" is used between the data structures. Here's a good introduction to how this is done in Clojure: http://hypirion.com/musings/understanding-persistent-vector-pt-1

Installation

go get github.com/tobgu/peds/cmd/peds

Usage

Generate statically type safe code for persistent data structures.

USAGE
peds

FLAGS        EXAMPLE
  -file      path/to/file.go
  -imports   import1;import2
  -maps      Map1<int,string>;Map2<float,int>
  -pkg       package_name
  -sets      Set1<int>
  -vectors   Vec1<int>

Examples

There are a couple of generated example collections in examples/collections.go.

The go:generate command used can be found in examples/types.go.

This illustrates the core usage pattern:

//go:generate peds -vectors=IntVector<int> -pkg=my_collections -file=collections/my_collections_gen.go

// Create a new vector
v := my_collections.NewIntVector(1, 2, 3)

// Create a copy of v with the first element set to 55, v is left untouched.
v2 := v.Set(0, 55)

Godoc

Generic types

https://godoc.org/github.com/tobgu/peds/internal/generic_types

Generated examples

https://godoc.org/github.com/tobgu/peds/examples

Experiences

There's an experience report based on the implementation of this library.

Caveats

  • Even though the data structures are immutable by most means it is not possible to use them as keys in hash maps for example. This is because they internally make use of slices, which are not comparable in Go.

Possible improvements

  • Investigate implementing the Map as a CHAMP tree.
  • Introspection of the contained types possible to refine the hash functions?
  • Get rid of Python dependency for developing peds (not needed to build or use peds).

Regenerate templates and run tests

make test

Want to contribute?

Great! Write an issue and let the discussions begin! Then file a PR!

peds's People

Contributors

tobgu avatar

Watchers

James Cloos avatar Leo Palomares avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.