• Stars
    star
    1,655
  • Rank 28,254 (Top 0.6 %)
  • Language
    Go
  • License
    MIT License
  • Created over 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ• Enjoy a slice! A utility library for dealing with slices and maps that focuses on type safety and performance.

πŸ• 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.

More Repositories

1

c2go

βš–οΈ A tool for transpiling C to Go.
Go
1,954
star
2

orderedmap

πŸ”ƒ An ordered map in Go with amortized O(1) for Set, Get, Delete and Len.
Go
614
star
3

sshtunnel

πŸš‡ Ultra simple SSH tunnelling for Go programs.
Go
253
star
4

vsql

✌️ Single-file or PostgreSQL-server compatible transactional SQL database written in pure V.
V
236
star
5

dingo

🐺 Easy, fast and type-safe dependency injection for Go.
Go
185
star
6

redismock

πŸ•‹ Mocking Redis in unit tests in Go.
Go
139
star
7

tf

βœ”οΈ tf is a microframework for parameterized testing of functions and HTTP in Go.
Go
136
star
8

pepper

🌢️ Create reactive frontends without ever writing frontend code.
Go
110
star
9

phpserialize

πŸ“‘ PHP serialize() and unserialize() for Go
Go
108
star
10

mbzdb

🎡 Port of the MusicBrainz database to run on other RDBMSs with replication (previously named MB_MySQL.)
Perl
88
star
11

gedcom

πŸ‘ͺ A Go library and CLI tools for encoding, decoding, traversing, merging, comparing, querying and publishing GEDCOM files.
Go
72
star
12

ghost

πŸ‘» Locate and fix overly complex lines of code in Go.
Go
53
star
13

concise

βœ… Concise is test framework for using plain English and minimal code, built on PHPUnit.
PHP
47
star
14

CollectionFactory

🏭 Translation between native collections in Objective-C and serialized formats like JSON.
Objective-C
41
star
15

sqltest

πŸ“ A comprehensive suite of SQL tests for testing the conformance of databases.
Python
39
star
16

bento

🍱 bento is an English-based automation language designed to be used by non-technical people.
Go
32
star
17

redis-usage

πŸ‘οΈ A non-blocking way to count the number of keys or size of Redis key prefixes
Go
30
star
18

vlang-sublime

Sublime Text support for the V programming language
Python
29
star
19

reflect

πŸͺž Runtime reflection for V (vlang)
V
27
star
20

ok

πŸ†— - a strongly-duck-typed language.
Go
17
star
21

mocksqs

πŸ“€ In-memory implementation of SQS ideal for unit testing.
Go
12
star
22

sqlite3x

100% compatible sqlite3 fork with more features
C
10
star
23

gedcompare

Compare GEDCOM files
Python
9
star
24

go-named-params

Named parameters for Go functions
Go
9
star
25

testify-stats

πŸ”’ testify: print test and assertions statistics at the end of the test suite
Go
7
star
26

independentreserve

πŸ’Έ PHP API for independentreserve.com
PHP
6
star
27

tesseract

πŸ”³ tesseract is a SQL object database with Redis as the backend, think of it like a document store that you run SQL statements against.
Python
6
star
28

GoogleMusicClient

🎢 Google Music Client in Objective-C
Objective-C
6
star
29

tui

🎨 Simple Go library for building complex text user interfaces
Go
5
star
30

wikitranslate

Easier translation of Wikipedia pages with CAT tools.
Go
5
star
31

multiselector

Multi Selector for Paw
JavaScript
4
star
32

iterator

Iterator builders for PHP
PHP
4
star
33

postgresql-partitioning

πŸ–– Automatic tools for managing partitions
4
star
34

switch-check

Validate switch statements contain all enum values.
Go
4
star
35

jsonrpc

πŸ’¬ Simple JSON-RPC server for Go
Go
3
star
36

vscode-ok

Language highlighting for the ok programming language in VSCode
2
star
37

tracklist-editor

A tool for editing track lists
JavaScript
2
star
38

sql2mql

β˜• Pure coffeescript SQL to MongoDB statement parser.
CoffeeScript
2
star
39

eagle

πŸ¦… Eagle is a highly parallel column-oriented embedded SQL database engine.
C
2
star
40

chartbrainz

The unofficial way to view charts from MusicBrainz.
Vue
2
star
41

Z

πŸ‡ΏJava to native C compiler.
Java
2
star
42

Hoard

🐿️ A PSR-compliant caching library for holding objects in nested pools with scripting ability.
PHP
2
star
43

Sentinel

Non-blocking Java web server.
Java
2
star
44

dandy

πŸ”¬ The handy, dandy test generation tool for Go.
Go
2
star
45

play.getok.dev

πŸ•ΉοΈ play.getok.dev
HTML
1
star
46

Pluralizer

Objective-C Library/Cocoa Pod for Simple String Pluralization
Objective-C
1
star
47

maven

⚚ maven programming language
C++
1
star
48

csv

Reading and writing CSV files in OK.
1
star
49

delta

πŸ”Ί The fastest scripting language on the planet based on PHP (pre-alpha)
C
1
star
50

construe

Language conversion and maintaining tool.
CoffeeScript
1
star
51

toy

🧸 A toy language
JavaScript
1
star
52

EagleDB

🐦 Concept column-oriented MVCC SQL daemon.
Java
1
star
53

intuit-quickbooks

The PHP SDK for QuickBooks v3 is set of PHP classes that make it easier to call QuickBooks APIs.
PHP
1
star
54

independentreserve-python

Python API for independentreserve.com
1
star
55

codekata.io

Online and realtime Kata for TDD
PHP
1
star
56

rateyourmusic-todo

Discussion and queue for entities that need editing help
1
star