1.0.1 • Published 3 months ago

mind-flow-js v1.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
3 months ago

MindFlowJS

MindFlowJS adds logic programming to JavaScript.

Installation

npm install mind-flow-js

getVar

returns logical variable

ask

returns a generator of answers

import {ask, getVar, equal, or} from 'mind-flow-js'

const name = getVar()
const people = (name) => {
    return or(
        equal(name, 'James'),
        equal(name, 'Robert'),
        equal(name, 'John')
    )
}

console.log(...ask([name], people(name)))
// [ 'James' ] [ 'Robert' ] [ 'John' ]

check

check if there is answer

import {check, equal, or} from 'mind-flow-js'

const people = (name) => {
    return or(
        equal(name, 'James'),
        equal(name, 'Robert'),
        equal(name, 'John')
    )
}

console.log(check(people('James')))
// true

console.log(check(people('Michael')))
// false

custom recursion function example

import {ask, equal, getVar, sub, mul, and, oneOf, lessOrEqual} from 'mind-flow-js'

const x = getVar()

const fact = (x, result) => {
    const x1 = getVar()
    const result1 = getVar()
    return () => 
      oneOf(
          and(
              lessOrEqual(x, 1),
              equal(result, 1)
          ),
          and(
              sub(x, 1, x1),
              fact(x1, result1),
              mul(x, result1, result)
          )
      )()
    
}

console.log(...ask([x], fact(10, x)))
// [ 3628800 ]

add

import {ask, getVar, add} from 'mind-flow-js'

const x = getVar()

const toSolve = add(2, 3, x)

console.log(...ask([x], toSolve))
// [ 5 ]

and

import {ask, getVar, and, equal} from 'mind-flow-js'

const x = getVar()
const y = getVar()
const z = getVar()

const toSolve = and(equal(x, y), equal(y, z), equal(z, 5))

console.log(...ask([x, y, z], toSolve))
// [ 5, 5, 5 ]

between

import {ask, getVar, between} from 'mind-flow-js'

const x = getVar()

const toSolve = between(1, 4, x)

console.log(...ask([x], toSolve))
// [ 1 ] [ 2 ] [ 3 ] [ 4 ]

div

import {ask, getVar, div} from 'mind-flow-js'

const x = getVar()

const toSolve = div(5, 2, x)

console.log(...ask([x], toSolve))
// [ 2.5 ]

equal

import {ask, getVar, equal} from 'mind-flow-js'

const x = getVar()

const toSolve = equal(x, 5)

console.log(...ask([x], toSolve))
// [ 5 ]

execFunc

import {ask, getVar, execFunc} from 'mind-flow-js'

const x = getVar()

const toSolve = execFunc(x, (a, b) => a + b, 5, 4)

console.log(...ask([x], toSolve))
// [ 9 ]

execMethod

import {ask, getVar, execMethod} from 'mind-flow-js'

const x = getVar()

const toSolve = execMethod([1, 2, 3], x, 'map', el => el * 2)

console.log(...ask([x], toSolve))
// [ [ 2, 4, 6 ] ]

getMember

import {ask, getVar, getMember} from 'mind-flow-js'

const x = getVar()

const toSolve = getMember([1, 2, 3], x, 'length')

console.log(...ask([x], toSolve))
// [ 3 ]

getMember

import {ask, getVar, getMember} from 'mind-flow-js'

const x = getVar()

const toSolve = getMember([1, 2, 3], x, 'length')

console.log(...ask([x], toSolve))
// [ 3 ]

greater

import {check, greater} from 'mind-flow-js'

console.log(check(greater(1, 5)))
// false
console.log(check(greater(7, 3)))
// true

greaterOrEqual

import {check, greaterOrEqual} from 'mind-flow-js'

console.log(check(greaterOrEqual(5, 5)))
// true
console.log(check(greaterOrEqual(3, 5)))
// false

int

import {ask, getVar, int} from 'mind-flow-js'

const x = getVar()

const toSolve = int(x)

console.log(...ask([x], toSolve, 5))
// [ 0 ] [ 1 ] [ -1 ] [ 2 ] [ -2 ]

less

import {check, less} from 'mind-flow-js'

console.log(check(less(5, 1)))
// false
console.log(check(greater(3, 7)))
// true

lessOrEqual

import {check, lessOrEqual} from 'mind-flow-js'

console.log(check(lessOrEqual(5, 5)))
// true
console.log(check(lessOrEqual(5, 3)))
// false

member

import {ask, getVar, member} from 'mind-flow-js'

const x = getVar()

const toSolve = member([1, 2, 3], x)

console.log(...ask([x], toSolve))
// [ 1 ] [ 2 ] [ 3 ]

mod

import {ask, getVar, mod} from 'mind-flow-js'

const x = getVar()

const toSolve = mod(25, 7, x)

console.log(...ask([x], toSolve))
// [ 4 ]

mul

import {ask, getVar, mul} from 'mind-flow-js'

const x = getVar()

const toSolve = mul(4, 8, x)

console.log(...ask([x], toSolve))
// [ 32 ]

int

import {ask, getVar, nat} from 'mind-flow-js'

const x = getVar()

const toSolve = nat(x)

console.log(...ask([x], toSolve, 5))
// [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ]

not

import {ask, getVar, not, equal, and, or} from 'mind-flow-js'

const x = getVar()

const toSolve = and(
    or(equal(x, 1), equal(x, 2)),
    not(equal(x, 1))
)

console.log(...ask([x], toSolve))
// [ 2 ]

notEqual

import {ask, getVar, notEqual, equal, and, or} from 'mind-flow-js'

const x = getVar()

const toSolve = and(
    or(equal(x, 1), equal(x, 2)),
    notEqual(x, 1)
)

console.log(...ask([x], toSolve))
// [ 2 ]

num

import {ask, getVar, nat} from 'mind-flow-js'

const x = getVar()

const toSolve = num(x)

console.log(...ask([x], toSolve, 5))
// [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ]

oneOf

import {ask, getVar, oneOf, equal, and, greaterOrEqual} from 'mind-flow-js'

const x = getVar()

const toSolve = oneOf(
    and(greaterOrEqual(1, 2), equal(x, 1)),
    and(greaterOrEqual(2, 2), equal(x, 2)),
    and(greaterOrEqual(3, 2), equal(x, 3)),
)

console.log(...ask([x], toSolve))
// [ 2 ]

or

import {ask, getVar, or, equal, and, greaterOrEqual} from 'mind-flow-js'

const x = getVar()

const toSolve = or(
    and(greaterOrEqual(1, 2), equal(x, 1)),
    and(greaterOrEqual(2, 2), equal(x, 2)),
    and(greaterOrEqual(3, 2), equal(x, 3)),
)

console.log(...ask([x], toSolve))
// [ 2 ] [ 3 ]

add

import {ask, getVar, sub} from 'mind-flow-js'

const x = getVar()

const toSolve = sub(2, 3, x)

console.log(...ask([x], toSolve))
// [ -1 ]

tuple

import {ask, getVar, tuple} from 'mind-flow-js'

const x = getVar()
const y = getVar()
const z = getVar()

const toSolve = tuple(x, y, z)

console.log(...ask([x, y, z], toSolve, 10))
// [ 0, 0, 0 ] [ 0, 0, 1 ] [ 0, 1, 0 ] [ 1, 0, 0 ] [ 0, 0, 2 ]
// [ 0, 1, 1 ] [ 0, 2, 0 ] [ 1, 0, 1 ] [ 1, 1, 0 ] [ 2, 0, 0 ]
1.0.1

3 months ago

1.0.0

3 months ago

0.1.2

3 months ago

0.1.1

3 months ago

0.1.0

4 months ago

0.0.1

5 months ago