Giter Site home page Giter Site logo

enumerati's Introduction

Enumerati

Build Status hex.pm version

Filter and order an enumerable collection of structs

Installation

def deps do
  [{:enumerati, "~> 0.0.8"}]
end

Usage

Filter

defmodule Person do
  defstruct ~w(first_name last_name)a
end

rick_ross = %Person{first_name: "rick", last_name: "ross"}
rick_james = %Person{first_name: "rick", last_name: "james"}
lebron_james = %Person{first_name: "lebron", last_name: "james"}
charles_barkley = %Person{first_name: "charles", last_name: "barkley"}
people = [rick_ross, charles_barkley, lebron_james, rick_james]

# match and
and_matches = Enumerati.filter(people, [first_name: "lebron", last_name: "james"])
Enum.count(and_matches) == 1
Enum.member?(and_matches, lebron_james) == true

# match or
or_matches = Enumerati.filter(people, [first_name: ["lebron", "charles"]])
Enum.count(or_matches) == 2
Enum.member?(or_matches, lebron_james) == true
Enum.member?(or_matches, charles_barkley) == true

Order

defmodule Product do
  defstruct ~w(name price)a
end

product_1 = %Support.Product{name: "Product 1", price: Decimal.new("0.4668")}
product_2 = %Support.Product{name: "Product 2", price: Decimal.new("-0.6142")}
product_3 = %Support.Product{name: "Product 3", price: Decimal.new("0.6468")}
product_4 = %Support.Product{name: "Product 4", price: Decimal.new("-0.7109")}
products = [product_1, product_2, product_3, product_4]

# ascending
Enumerati.order(products, [:price]) == [
 product_4,
 product_2,
 product_1,
 product_3
]

Enumerati.order(products, [{:price, :asc}]) == [
 product_4,
 product_2,
 product_1,
 product_3
]

# descending
Enumerati.order(products, [{:price, :desc}]) == [
 product_3
 product_1,
 product_2,
 product_4,
]

enumerati's People

Contributors

rupurt avatar dependabot[bot] avatar dependabot-preview[bot] avatar

Stargazers

Joshua ChenZhao avatar

Watchers

 avatar  avatar

Forkers

yurikoval

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.