GithubHelp home page GithubHelp logo

slice-chain's Introduction

chain style operation for golang's slice just like javascript array

support operation:

  • Map:
    • convert slice of any type A to slice of any type B
  • Filter:
    • delete elements of a slice you do not need
  • Sort:
    • sort a slice of any type on your rule
  • Find:
    • find the element in the slice you need
  • Reverse:
    • reverse the element
  • Reduce:
    • walk the slice reduce to a single value

像 JavaScript 操作数组一样链式操作 golang 的slice

支持的操作:

  • Map:
    • 从一种类型的切片转换成另一种类型的切片
  • Filter:
    • 过滤切片中不符合条件的元素, 返回新的切片
  • Sort:
    • 自定义规则对切片进行排序
  • Find:
    • 找出切片中符合条件的第一个元素, 返回该元素和该元素的索引
  • Reverse:
    • 颠倒切片元素先后顺序
  • Reduce:
    • 遍历数组, 归并成一个值

eg: run:

package main

import (
	"fmt"
	"github.com/peterq/slice-chain"
	"strconv"
	"strings"
)

func main() {
	var a = []string{"1", "2", "3", "4", "5"}

	slice_chain.Collect(a).
		Map(func(s string) int { i, _ := strconv.Atoi(s); return i }).
		Filter(func(i int) bool { return i%2 == 0 }).
		Map(func(i int) string { return fmt.Sprintf("%d * %d = %d", i, i, i*i) }).
		SaveTo(&a)

	println(strings.Join(a, "\n"))
}

got output:

2 * 2 = 4
4 * 4 = 16

more usage you can find in the chain_test.go file

issue and pr are welcomed ;)

love golang, love china. may the world peace :)

slice-chain's People

Contributors

peterq avatar

Watchers

James Cloos 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.