Several functional programming supporting in golang

A golang library that makes operations on slice easilier

What can I do?

  • slice process
    • Map
    • Filter
    • Sort
    • Reverse
  • map process
    • Keys
    • Values
  • output (starting with "P" means parallel version)
    • ToSlice / PToSlice
    • ToMap / PToMap / ToMap2 / PToMap2
    • ToGroupMap / PToGroupMap / ToGroupMap2 / PToGroupMap2
    • Reduce / PReduce
    • Each / PEach
    • Some
    • Every

Installation

go get github.com/lennon-guan/pipe

Example

	src := []int{1, 2, 3}
	dst := pipe.NewPipe(src).
			Map(func(item int) string{ return fmt.Sprintf("#%d", item) }).
			ToSlice().([]string)
	// dst is []string{"#1", "#2", "#3"}
	src := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	dst := pipe.NewPipe(src).
			Filter(func(in int) bool { return in % 3 == 0 }).
			ToSlice().([]int)
	// dst is []int{3, 6, 9}
	src := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	sum := pipe.NewPipe(src).
			Reduce(0, func(s, item int) int{ return s + item }).(int)
	// sum == 55
	src := []int{3, 1, 4, 1, 5, 9}
	dst := pipe.NewPipe(src).
		Sort(func(a, b int) bool { return a < b }).
		ToSlice().([]int)
	// dst is []int{1, 1, 3, 4, 5, 9}
	src := []int{1, 2, 3}
	dst := pipe.NewPipe(src).
		Reverse().
		ToSlice().([]int)
	// dst is []int{3, 2, 1}
	src := []int{5, 4, 3, 2, 1}
	dst := pipe.NewPipe(src).
		ToMap(
			func(v int) string { return fmt.Sprintf("Key-%d", v) },
			func(v int) string { return fmt.Sprintf("Val-%d", v) },
		).(map[string]string)
	// dst is map[Key-1:Val-1 Key-5:Val-5 Key-4:Val-4 Key-3:Val-3 Key-2:Val-2]
	src := []int{5, 4, 3, 2, 1}
	dst := pipe.NewPipe(src).
		ToMap2(func(v int) (string, string) {
		return fmt.Sprintf("Key-%d", v), fmt.Sprintf("Val-%d", v)
	}).(map[string]string)
	// dst is map[Key-1:Val-1 Key-5:Val-5 Key-4:Val-4 Key-3:Val-3 Key-2:Val-2]
	src := []int{5, 4, 3, 2, 1}
	dst := NewPipe(src).
		ToGroupMap(
		func(v int) string {
			if v%2 != 0 {
				return "odd"
			} else {
				return "even"
			}
		},
		func(v int) int { return v },
	).(map[string][]int)
	// dst is map[odd:[5 3 1] even:[4 2]]
	src := []int{5, 4, 3, 2, 1}
	dst := pipe.NewPipe(src).
		ToGroupMap2(
		func(v int) (string, int) {
			if v%2 != 0 {
				return "odd", v
			} else {
				return "even", v
			}
		},
	).(map[string][]int)
	// dst is map[odd:[5 3 1] even:[4 2]]
	src := []int{1, 2, 3, 4, 5}
	dst := make([]int, 5)
	pipe.NewPipe(src).
		Map(func(i int) int { return i * i }).
		Each(func(item, index int) { dst[index] = item })
	if !intSliceEqual(dst, 1, 4, 9, 16, 25) {
		t.Error("values wrong")
	}
	src := []int{1, 2, 3, 4, 5}
	dst := make([]int, 5)
	pipe.NewPipe(src).
		Map(func(i int) int { return i * i }).
		PEach(func(item, index int) { dst[index] = item })
	if !intSliceEqual(dst, 1, 4, 9, 16, 25) {
		t.Error("values wrong")
	}

You can invoke map/filter function many times

	src := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	dst := pipe.NewPipe(src).
			Filter(func(in int) bool { return in % 3 == 0 }).
			Map(func(in int) int { return in * in }).
			ToSlice().([]int)
	// dst is []int{9, 36, 81}

More examples are avaliable in pipe_test.go

Similar Resources

functional programming in go

function programming experimental lib why another fp lib I like fp style and I haven’t found a lib with these features: streamingly, I can handle infi

Sep 1, 2022

Lithia is an experimental functional programming language with an implicit but strong and dynamic type system.

Lithia is an experimental functional programming language with an implicit but strong and dynamic type system. Lithia is designed around a few core concepts in mind all language features contribute to.

Dec 24, 2022

A library providing useful functional programming helpers for Go 1.18

Underscore underscore is a Go library providing useful functional programming helpers without extending any built-in objects. It is mostly a port from

Dec 29, 2022

Go module that provides primitive functional programming utilities.

Functional Functional provides a small set of pure functions that are common in functional programming languages, such as Reduce, Map, Filter, etc. Wi

Jun 12, 2022

Simple REST-API implementation using Golang with several packages (Echo, GORM) and Docker

Simple REST-API Boilerplate This is a simple implementation of REST-API using Golang and several packages (Echo and GORM). By default, I use PostgreSQ

Sep 13, 2022

🏋️ dbbench is a simple database benchmarking tool which supports several databases and own scripts

dbbench Table of Contents Description Example Installation Supported Databases Usage Custom Scripts Troubeshooting Development Acknowledgements Descri

Dec 30, 2022

gomerge is a tool to quickly bulk merge several pull requests from your terminal.

gomerge is a tool to quickly bulk merge several pull requests from your terminal.

Gomerge is a tool to quickly enable you to bulk merge Github pull requests from your terminal. The intention of this tool is to simplfy, and eventually automate the merging of github pull requests. This tool should be able to run on most systems.

Dec 28, 2022

hego aims to provide a consistent API for several metaheuristics

hego hego aims to provide a consistent API for several metaheuristics (black box optimization algorithms) while being performant. Even though most of

Dec 24, 2022

succinct provides several static succinct data types

succinct provides several static succinct data types Succinct Set Synopsis Performance Implementation License Succinct Set

Jan 5, 2023

gophertunnel is composed of several packages that may be of use for creating Minecraft related tools

gophertunnel is composed of several packages that may be of use for creating Minecraft related tools

gophertunnel is composed of several packages that may be of use for creating Minecraft related tools. A brief overview of all packages may be found here.

Dec 31, 2022

🛡 Several domain lists compiled into a database file used by V2Ray.

Domain list database This project is pulled from v2fly/domain-list-community, with only its data changed to use several blocklists. Purpose of this pr

May 22, 2022

🍪CookieMonster is a command-line tool and API for decoding and modifying vulnerable session cookies from several different frameworks.

🍪 CookieMonster CookieMonster is a command-line tool and API for decoding and modifying vulnerable session cookies from several different frameworks.

Jan 8, 2023

This repo includes several winrm applications like transfering files, running commands.

This repo includes several winrm applications like transfering files, running commands.

WinRM Tools This repo includes several WinRM tools written with Go: File transfering between two Powershell session. Running command on remote Powersh

Nov 26, 2022

Several Examples for building docker containers for your Go applications

go-docker Several Examples for building docker containers for your Go applicatio

Dec 27, 2021

Small tool to pull/push several projects in one go

gitTool Small tool to push and pull several projects in one go. Written in Go 1.17 by Roy Dybing Contact: location name/handle github: rDybing linked

Dec 28, 2021

Anaximander is an ISP probing tool implementing several reduction techniques to cut down the number of probes launched in order to map an Autonomous System

Anaximander is an ISP probing tool implementing several reduction techniques to cut down the number of probes launched in order to map an Autonomous System

Anaximander is an ISP probing tool implementing several reduction techniques to cut down the number of probes launched in order to map an Autonomous System, while still keeping high discovery levels.

Jun 21, 2022

Blockcain - Trust Wallet token repository - A comprehensive, up-to-date collection of information about several thousands (!) of crypto tokens

Blockcain - Trust Wallet token repository - A comprehensive, up-to-date collection of information about several thousands (!) of crypto tokens

Trust Wallet Assets Info Overview Trust Wallet token repository is a comprehensi

Feb 14, 2022

Gomap is a package that contains several functions to make it easier to work with maps in Go.

Gomap Gomap is a package that contains several functions to make it easier to work with maps in Go. Installation go get github.com/dimasadyaksa/gomap

Apr 22, 2023

Kazaam was created with the goal of supporting easy and fast transformations of JSON data with Golang

kazaam Description Kazaam was created with the goal of supporting easy and fast transformations of JSON data with Golang. This functionality provides

Sep 17, 2021
Related tags
Gonum is a set of numeric libraries for the Go programming language. It contains libraries for matrices, statistics, optimization, and more

Gonum Installation The core packages of the Gonum suite are written in pure Go with some assembly. Installation is done using go get. go get -u gonum.

Dec 29, 2022
Convert struct, slice, array, map or others for Golang

XConv zh-CN XConv is a golang type convertor. It convert any value between types (base type, struct, array, slice, map, etc.) Features Convert between

Dec 8, 2022
Data syncing in golang for ClickHouse.
Data syncing in golang for ClickHouse.

ClickHouse Data Synchromesh Data syncing in golang for ClickHouse. based on go-zero ARCH A typical data warehouse architecture design of data sync Aut

Jan 1, 2023
Basic Crud operation api's in golang

Basic Crud operation api's in golang

Nov 9, 2021
Tool that can parse Go files into an abstract syntax tree and translate it to several programming languages.
Tool that can parse Go files into an abstract syntax tree and translate it to several programming languages.

GoDMT GoDMT, the one and only Go Data Model Translator. The goal of this project is to provide a tool that can parse Go files that include var, const,

Nov 28, 2022
Code Generation for Functional Programming, Concurrency and Generics in Golang

goderive goderive derives mundane golang functions that you do not want to maintain and keeps them up to date. It does this by parsing your go code fo

Dec 25, 2022
Functional Programming support for golang.(Streaming API)

Funtional Api for Golang Functional Programming support for golang.(Streaming API) The package can only be used with go 1.18. Do not try in lower vers

Dec 8, 2021
Utilities and immutable collections for functional programming in Golang

Utilities and immutable collections for functional programming in Golang. This is an experimental library to play with the new Generics Feature in Go 1.18.

Sep 1, 2022
A simple package in Golang containing helpers for functional programming

go-functools Golang package containing functools using Go generics functools is

Nov 3, 2022
Functional programming library for Go including a lazy list implementation and some of the most usual functions.

functional A functional programming library including a lazy list implementation and some of the most usual functions. import FP "github.com/tcard/fun

May 21, 2022