All Projects → kulshekhar → Fungen

kulshekhar / Fungen

Licence: mit
Replace boilerplate code with functional patterns using 'go generate'

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Fungen

stream
Go Stream, like Java 8 Stream.
Stars: ✭ 60 (-50.82%)
Mutual labels:  map, functional, filter
Itiriri
A library built for ES6 iteration protocol.
Stars: ✭ 155 (+27.05%)
Mutual labels:  map, filter
Torchdata
PyTorch dataset extended with map, cache etc. (tensorflow.data like)
Stars: ✭ 226 (+85.25%)
Mutual labels:  map, filter
leaflet-tag-filter-button
Adds tag filter control for layers (marker, geojson features etc.) to LeafLet.
Stars: ✭ 48 (-60.66%)
Mutual labels:  map, filter
morphmorph
😱 Isomorphic transformations. Map, transform, filter, and morph your objects
Stars: ✭ 26 (-78.69%)
Mutual labels:  functional, filter
Pgo
Go library for PHP community with convenient functions
Stars: ✭ 51 (-58.2%)
Mutual labels:  map, filter
Phunctional
⚡️ λ PHP functional library focused on simplicity and performance
Stars: ✭ 243 (+99.18%)
Mutual labels:  functional, map
linq
A familiar set of functions that operate on JavaScript iterables (ES2015+) in a similar way to .NET's LINQ does with enumerables.
Stars: ✭ 39 (-68.03%)
Mutual labels:  map, filter
ustd
Micro-standard-library providing minimal and portable array, queue and map for attiny avr, arduinos, esp8266/32 and linux, mac
Stars: ✭ 14 (-88.52%)
Mutual labels:  map, functional
Performance Analysis Js
Map/Reduce/Filter/Find Vs For loop Vs For each Vs Lodash vs Ramda
Stars: ✭ 532 (+336.07%)
Mutual labels:  map, filter
Itiriri Async
A library for asynchronous iteration.
Stars: ✭ 78 (-36.07%)
Mutual labels:  map, filter
Wavelets.jl
A Julia package for fast discrete wavelet transforms and utilities
Stars: ✭ 118 (-3.28%)
Mutual labels:  filter
Jsonapi.rb
Lightweight, simple and maintained JSON:API support for your next Ruby HTTP API.
Stars: ✭ 116 (-4.92%)
Mutual labels:  filter
Geojson2svg
Converts geojson to svg string given svg viewport size and maps extent.
Stars: ✭ 117 (-4.1%)
Mutual labels:  map
Active hash relation
ActiveHash Relation: Simple gem that allows you to run multiple ActiveRecord::Relation using hash. Perfect for APIs.
Stars: ✭ 115 (-5.74%)
Mutual labels:  filter
Ng2 Smart Table
Angular Smart Data Table component
Stars: ✭ 1,590 (+1203.28%)
Mutual labels:  filter
React Mapbox Gl
A React binding of mapbox-gl-js
Stars: ✭ 1,683 (+1279.51%)
Mutual labels:  map
Alembic
⚗️ Functional JSON Parser - Linux Ready 🐧
Stars: ✭ 115 (-5.74%)
Mutual labels:  functional
Geo Data Viewer
🗺️ Geo Data Viewer w/0 Py 🐍 || pyWidgets ⚙️ || pandas 🐼 || @reactjs ⚛️ required to gen. some snazzy maps 🗺️ with keplerGL ...
Stars: ✭ 115 (-5.74%)
Mutual labels:  map
Processing Postfx
A shader based postFX library for processing.
Stars: ✭ 114 (-6.56%)
Mutual labels:  filter

fungen Build Status

fungen is a tool that can be used with go generate to automate the creation of list like types and methods like Map, Filter, Reduce, etc on these types.

This allows you to use constructs like the following on lists of any types:


myList.Map(func(t type1) type1 {

...

}).Filter(func(t type1) bool {

...

}).Take(3).Reduce(initialValue type1, func (prev type1, current type1) type1 {

...

})

The Map and Filter methods have parallel counterparts PMap and PFilter which can be used to parallelize time consuming operations.

Note: This tool uses standard golang constructs to make this functionality available on any type - standard and custom. Interfaces have NOT been used.

Motivation

While the for loop in Go is quite nice, it still involves some boilerplate code. In my particular case, I realized that I could eliminate a majority of that code by using the following constructs:

  • Map (apply a function to each member of a list and return the resulting list - of either the same type or a different type)
  • PMap (parallel map)
  • Filter (apply a function to each member of a list to retrieve only the ones that satisfy some criteria)
  • PFilter (parallel filter)
  • FilterMap (applies the filter(s) and map to the list members in a single loop and returns the resulting list containing members of the mapped type)
  • PFilterMap (parallel FilterMap)
  • Reduce (perform aggregation functions on a list)
  • ReduceRight
  • Each (execute any function on each element of a list)
  • EachI
  • Take (create a new list containing only the first n elements of another list)
  • TakeWhile (take the first elements that satisfy a particular criteria)
  • Drop (create a new list by excluding the first n elements of another list)
  • DropWhile (exclude the first elements that satisfy a particular criteria)
  • All (returns true if all the members of the list satisfy a function or if the list is empty)
  • Any (returns true if at least one member of the list satisfies a function)

How to Use

Assuming you have installed fungen (see below), you can start by including the comment in your file for go generate to process. Let's assume we want to use the above methods with a list of strings (type []string).

Add the following comment in your source code:

//go:generate fungen -types string

Now run the go generate command in the same directory where this source file is located. This will generate a file named fungen_auto.go in this directory. This file will contain the type and method definitions that you'll need.

If you take a look at the file, you'll see that a new type has been created:

type stringList []string

If you now use this type (stringList) in place of []string, you'll be able to use all the above mentioned methods on that list of string.


words := stringList{"calculator", "skip", "grape", "obtainable", "inexpensive", "panoramic", "powerful"}

wordsWithLengthLessThan7 := words.Filter(func(w string) bool {
  return len(w) < 7  
})
// wordsWithLengthLessThan7 = [skip grape]

Installation

go install github.com/kulshekhar/fungen

Code Generation

The complete command to generate the code:

fungen -package PackageName -types comma,Separated,Types,With,Optional:Opt,short:Sh,names:n

Use from source code:

//go:generate fungen -types string,int

followed by

go generate

on the command line, in the directory where you wish the generated file to be stored.

Use From the command line:

fungen -types string,int

This tool will generate as a file named fungen_auto.go.

Explanation of Options

-package PackageName

The -package parameter is optional. If specified, it will generate the code file by using package PackageName at the top. If omitted, it will generate the code file by using package main at the top.

-types comma,Separated,Types,With,Optional:Opt,short:Sh,names:n

The -types parameter takes a comma separated list of types for which the list type and methods on this type should be generated. eg: (int,string,uint,customType)

Each of the comma separated values can themselves optionally be a colon separated value. If this is the case, the first part (before the colon) should be a valid type name (built in or custom) and the second is the name used in the names of the methods.

-filename filename.go

Filename for generated package (default "fungen_auto.go"). The -filename parameter is optional.

-methods Map,Filter

Comma separated list of methods to generate. By default generate all methods.

Valid methods is: Map,PMap,Filter,PFilter,Reduce,ReduceRight,Each,EachI,Take,TakeWhile,Drop,DropWhile,All,Any,FilterMap,PFilterMap

Example 1

If -types int,string is used, the types generated will be:

type intList []int
type stringList []string

The following methods will be available on both these types:

Map
PMap
Filter
PFilter
Reduce
ReduceRight
Take
TakeWhile
Drop
DropWhile
Each
EachI
All
Any

Additionally, the intList type will have the following methods:

MapString
PMapString
FilterMapString
PFilterMapString

And the stringList type will have the following methods:

MapInt
PMapInt
FilterMapInt
PFilterMapInt

Example 2

If -types int:I,string:Str is used, the names of the common set of methods generated will be the same as above. However, the types and the additional methods generated will have different names.

The types will be named as follows:

type IList []int
type StrList []string

The IList type will have the following methods:

MapStr
PMapStr

And the StrList type will have the following methods:

MapI
PMapI

Feedback, critique and contributions are all welcome.

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].