Giter Club home page Giter Club logo

pie's Introduction

๐Ÿ• github.com/elliotchance/pie

GoDoc Build Status codecov

Enjoy a slice! pie is a library of utility functions for common operations on slices and maps.

Quick Start

If you are using (or require) Go 1.17 or below, you will have to use v1.

pie can be used in two ways, the first is to use the regular parameterized functions:

Run this program

package main

import (
    "fmt"
    "strings"

    "github.com/elliotchance/pie/v2"
)

func main() {
    names := pie.FilterNot([]string{"Bob", "Sally", "John", "Jane"},
        func(name string) bool {
            return strings.HasPrefix(name, "J")
        })

    fmt.Println(names) // "[Bob Sally]"
}

Or, if you need to chain multiple operations you can use one of:

  • pie.Of - works with any element type, but functions are limited.
  • pie.OfOrdered - only works with numbers and strings, but has more functions.
  • pie.OfNumeric - only works with numbers, but has all functions.

Run this program

package main

import (
    "fmt"
    "strings"

    "github.com/elliotchance/pie/v2"
)

func main() {
    name := pie.Of([]string{"Bob", "Sally", "John", "Jane"}).
        FilterNot(func(name string) bool {
            return strings.HasPrefix(name, "J")
        }).
        Map(strings.ToUpper).
        Last()

    fmt.Println(name) // "SALLY"
}

You can find the full documentation here.

FAQ

What are the requirements?

pie v2 only supports Go 1.18+. If you have an older version you can use v1.

What are the goals of pie?

  1. Type safety. I never want to hit runtime bugs because I could pass in the wrong type, or perform an invalid type case out the other end.

  2. Performance. The functions need to be as fast as native Go implementations otherwise there's no point in this library existing.

  3. Nil-safe. All of the functions will happily accept nil and treat them as empty slices. Apart from less possible panics, it makes it easier to chain.

  4. Immutable. Functions never modify inputs (except in cases where it would be illogical), unlike some built-ins such as sort.Strings.

How do I contribute a function?

Pull requests are always welcome.

Here is a comprehensive list of steps to follow to add a new function:

  1. Create a new file for your function (tip: copy an existing file can be quicker). Add your implmentation and comment.

  2. Create appropriate tests.

  3. If your function accepts a slice, it should also be added to the OfSlice API (see of.go).

Why is the emoji a slice of pizza instead of a pie?

I wanted to pick a name for the project that was short and had an associated emoji. I liked pie, but then I found out that the pie emoji is not fully supported everywhere. I didn't want to change the name of the project to cake, but pizza pie still made sense. I'm not sure if I will change it back to a pie later.

pie's People

Contributors

elliotchance avatar zhiburt avatar deleplace avatar chocolacula avatar alessandrolorenzi avatar dylanmeeus avatar ougwen1235 avatar jerome-laforge avatar unique1o1 avatar electrofocus avatar danielpsf avatar misikdmytro avatar morrisxyang avatar jhuliano avatar kirillmorozov avatar istudko avatar kylemarek avatar orisano avatar kochurovro avatar samdfonseca avatar samlitowitz avatar leilei3167 avatar luoxin 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.