Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/kvartborg/maduce

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/kvartborg/maduce

  • v0.0.0-20210619161751-2d7e8ae25cd2
  • Source
  • Go
  • Socket score

Version published
Created
Source

maduce

maduce is an implementation of the functional concepts filter, map and reduce found in other languages like python, javascript, etc.

This package purposely diverge from core principals of how go code should be written, you should therefore think twice before you consider using this package, in most cases for loops is the way to go.

The reason for the existence of this package is that it allows for better composability and allows datasets to be more easily explored and evaluated in Go. It's specifically designed as a tool to be used for experimenting with datasets and not as a library intented for production use where performance is critical.

The API of the maduce package is completely dynamic which has the down side of no compile time garuantees about the function signatures given to filter, map or reduce.

Each method on a maduce.Collection have a description of the handlers they support. Because go doesn't support generics yet, i have create my own notation where <Type> can be replaced with what ever type you want. The <Type> in the function argument has to be the same as in the collection. The output type could be something else or the same as the input, it depends on what you want to achieve.

// example of function signature with generic types
func(item <Type>, index int) <Type>

// example of function that maps over a collection of float64 and castst them
// to a string in a new collection, this example satisfies the function
// signature from above
func(item float64, index int) string {
	return fmt.Sprintf("%.2f", item)
}

This package is heavily based on reflection and type assertions which can result in runtime panics if used wrongly.

TODO(@kvartborg): would like to experiment with a streaming implementation based on the io.Reader interface at some point.

Usage

Below is a simple example of how a slice of float64 can be filtered, mapped and reduced into a string.

package main

import (
    "fmt"

    "github.com/kvartborg/maduce"
)

func main() {
  collection := maduce.From([]float64{0, 1.8, 2, 3.3, 4, 5})

  var result string
  collection.
    Filter(func(n float64) bool {
      return n > 0
    }).
    Map(func(n float64) string {
      return fmt.Sprintf("%.2f", n)
    }).
    Reduce(&result, func(s, result string, index int) string {
      if result == "" {
        return fmt.Sprintf("%d: %s", index, s)
      }
      return fmt.Sprintf("%s\n%d: %s", result, index, s)
    })

  fmt.Println(result)
}

Documentation

The full documentation of the package can be found on godoc.

License

This project is licensed under the MIT License.

FAQs

Package last updated on 19 Jun 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc