Git Product home page Git Product logo

gleam-ask's Introduction

ask

Package Version Hex Docs CI

ask is a Gleam library tailored for simplifying the management of equivalences, orderings, and predicates. Equivalences allow for the comparison of two values to determine if they're equivalent, while orderings provide a systematic way to compare values in terms of their relative magnitudes or positions. Predicates, on the other hand, assess if a given value meets specific conditions, offering a flexible mechanism for expressing logical constraints or requirements.

Overview

ask provides developers with a set of functions to create, combine, and transform equivalences, orderings, and predicates. Whether you're performing basic value comparisons or intricate logical operations, ask furnishes the necessary tools to handle a variety of scenarios effectively.

Installation

gleam add ask

Usage

Equivalences

import ask/eq

// Defining custom equivalences
let eq1 = fn(x, y) { x % 3 == y % 3 }
let eq2 = fn(x, y) { x % 5 == y % 5 }

// Combining equivalences using logical operations
let combined_eq = eq.and(eq1, eq2)

// Using equivalence to compare values
let result = combined_eq(15, 30) // Returns True

Equivalences are expected to follow these friendly rules:

  • Reflexivity: Every value is equivalent to itself. It's like saying, "Hey, you're always equal to yourself!"
  • Symmetry: If one value is equivalent to another, then it's a two-way street! If X is like Y, then Y is like X. It's all about fairness!
  • Transitivity: Imagine a chain of equivalence! If X is equivalent to Y, and Y is equivalent to Z, then it's like saying X is also buddies with Z. Friendship circles all around!

These rules help keep our equivalences reliable and predictable, making sure they play nice with each other.

Orderings

import ask/ord
import gleam/int
import gleam/list
import gleam/string

type User {
  User(name: String, age: Int)
}

let compare_name = ord.map_input(string.compare, fn(user: User) { user.name })
let compare_age = ord.map_input(int.compare, fn(user: User) { user.age })

// Sort by name, then by age
let compare_user = ord.combine(compare_name, compare_age)
let result =
  list.sort(
    [
      User(name: "alice", age: 32),
      User(name: "bob", age: 45),
      User(name: "alice", age: 24),
    ],
    by: compare_user,
  )

Predicates

import ask/predicate

// Defining custom predicates
let is_positive = fn(x) { x > 0 }
let is_even = fn(x) { x % 2 == 0 }

// Combining predicates using logical operations
let combined_pred = predicate.and(is_positive, is_even)

// Using predicate to evaluate values
let result = combined_pred(6) // Returns True
let result = combined_pred(-6) // Returns False

Conclusion

ask equips Gleam developers with a robust toolkit for managing equivalences, orderings, and predicates efficiently. Whether you're implementing algorithms, data validation systems, or decision-making processes, ask facilitates streamlined code development, allowing you to focus on problem-solving.

Further documentation can be found at https://hexdocs.pm/ask.

Development

gleam run   # Run the project
gleam test  # Run the tests
gleam shell # Run an Erlang shell

gleam-ask's People

Contributors

furrycatherder avatar

Stargazers

Benjamin Peinhardt avatar Sanjay Gubaju avatar Patrick G avatar

Watchers

 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.