Code Monkey home page Code Monkey logo

logic's Introduction

Logic

GitHub npm build David

What is Logic?

Logic is a library for handling logical operations with functional programming.

Installation

To use with node:

npm install lgc

To access the library:

const lgc = require('lgc')

Usage

Logic

In the heart of the library, there lies Logic function. Not to be confused, it is not a constructor. However it is the function that we use to create logical fragments. You can import Logic directly from library:

const { Logic } = require('lgc')

Logic is a function that utilises functional chaining:

const amIHappy = Logic(isTodayHoliday()).or(isWeatherSunny()).value

Logic also supports autoboxing Logic objects. Hence you have to use value property when you want to get the boolean value since each operation returns another Logic Object instead of Boolean value:

const result = Logic(Logic(true).xor(false)).value

Logic supports 4 basic logical operators: and, or, xor and not

not Operator

Not operator is a special case. It is the only unary function. Using it is a little bit different than others:

const { __ } = require('lgc')
const gte = (x, y) => x >= y
const result = Logic(__, gte(3, 5)).value

__ is a special object which contains one key as @@logical/not. When it is used as first parameter, Logic function -instead of returning Logic object- returns the result of not operation. Also you can use not as a normal operator. However keep in mind that using not explicitly will remove early value which comes from the function chain. Because of that I highly discourage you from using it explicitly.

// Even though we have true as our previous value, it will be discarded in not operation
const isTrue = Logic(true).not(false).value

A Basic Example

Let us try to convert (x || y) && !(x ^ (y && !x)) to a reusable logical fragment chain:

// L is just a shorthand version of Logic
const { L } = require('lgc')
const myLogicalFragment = (x, y) => L(L(x).or(y)).and(L(__, L(L(x).xor(L(y).and(L(__, x))))))

Quite a mess, isn't it? No worry! There is another way of computing logical operations using Logic library. However by chaining we can access and use previous values. This is a big plus which makes up for the enormous complexity.

Pure Operators

Logic library is not only restricted with Logic function. There is a purer(!) way of using logical operators:

const { and, or, not, xor } = require('lgc')
console.log(and(true)(false))
console.log(or(false, false))
console.log(not(false))
console.log(xor(true)(true))

Using those basic functions you can easily compute the result of a logical operation. They are also curried which means they can be either used as op(x, y) or op(x)(y) which let's you those operators with a constant parameter:

const alwaysFalse = and(false)
console.log(alwaysFalse(true))

They can't be chained contrary to Logic object.

Utility Functions

ternary(predicate: (any) => Boolean, trueFunction: Function, falseFunction: Function, deps: Array): Functional implementation of ternary if operator.
Bool(v: any): Takes a value and converts it to the boolean. Applicable for Logic objects.
T: true: A variable for true
F: false: A variable for false

License

The project is licensed under MIT Open-source License.

logic's People

Contributors

berakoc avatar

Stargazers

Roman 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.