Skip to content

fpjson-lang

FPJSON

FPJSON is a programming language agnostic JSON-based functional programming language.

  • The whole code is just a JSON array
  • Functional Programming
  • No overhead due to programming language implementation details

In other words, you don't have to worry about any programming language specifications.

Instead you can just focus on building pure logics for data manipuration.

Since it's just a JSON array, it can be ported to any programming language environment.

Installation

yarn add fpjson-lang

Basics

Compute any valid FPJSON logic.

import fpjson from "fpjson-lang"
 
fpjson(["add", 1, 2])
// add(1, 2) = 3
 
fpjson("difference", [1, 2, 3], [3, 4, 5]] )
// difference([1, 2, 3],[3, 4, 5]) = [1, 2]
 
fpjson([["map", ["inc"]], [1, 2, 3]])
// map(inc)([1, 2, 3]) = [2, 3, 4]
 
fpjson([["compose", ["map", ["inc"]], ["difference"]], [1, 2, 3], [3, 4, 5]])
// difference([1, 2, 3],[3, 4, 5]) = [1, 2], map(inc)([1, 2]) = [3, 4]

There are more than 250 functions available for building highly advanced complex logic.

CategoryFunctions
Typeis, isNil, propIs, type
FunctionaddIndex, always, andThen, ap, apply, applySpec, applyTo, ascend, binary, bind, call, comparator, compose, composeWith, construct, constructN, converge, curry, curryN, descend, empty, F, flip, identity, invoker, juxt, lift, liftN, memoizeWith, nAry, nthArg, o, of, on, once, otherwise, partial, partialObject, partialRight, pipe, pipeWith, promap, T, tap, thunkify, tryCatch, unapply, unary, uncurryN, useWith
Mathadd, dec, divide, inc, mathMod, mean, median, modulo, multiply, negate, product, subtract, sum
Listadjust, all, any, aperture, append, chain, collectBy, concat, count, drop, dropLast, dropLastWhile, dropRepeats, dropRepeatsWith, dropWhile, endsWith, filter, find, findIndex, findLast, findLastIndex, flatten, forEach, fromPairs, groupBy, groupWith, head, includes, indexBy, indexOf, init, insert, insertAll, intersperse, into, join, last, lastIndexOf, length, map, mapAccum, mapAccumRight, mergeAll, move, none, nth, pair, partition, pluck, prepend, range, reduce, reduceBy, reduced, reduceRight, reduceWhile, reject, remove, repeat, reverse, scan, sequence, slice, sort, splitAt, splitEvery, splitWhen, splitWhenever, startsWith, tail, take, takeLast, takeLastWhile, takeWhile, times, transduce, transpose, traverse, unfold, uniq, uniqBy, uniqWith, unnest, update, without, xprod, zip, zipObj, zipWith
LogicallPass, and, anyPass, both, complement, cond, defaultTo, either, ifElse, isEmpty, not, or, pathSatisfies, propSatisfies, unless, until, when, xor
Relationclamp, countBy, difference, differenceWith, eqBy, equals, gt, gte, identical, innerJoin, intersection, lt, lte, max, maxBy, min, minBy, pathEq, propEq, sortBy, sortWith, symmetricDifference, symmetricDifferenceWith, union, unionWith
Objectassoc, assocPath, clone, dissoc, dissocPath, eqProps, evolve, forEachObjIndexed, has, hasIn, hasPath, invert, invertObj, keys, keysIn, lens, lensIndex, lensPath, lensProp, mapObjIndexed, mergeDeepLeft, mergeDeepRight, mergeDeepWith, mergeDeepWithKey, mergeLeft, mergeRight, mergeWith, mergeWithKey, modify, modifyPath, objOf, omit, over, path, pathOr, paths, pick, pickAll, pickBy, project, prop, propOr, props, set, toPairs, toPairsIn, unwind, values, valuesIn, view, where, whereAny, whereEq
Stringmatch, replace, split, test, toLower, toString, toUpper, trim

Syntax

You should familiarize yourself with Ramda which enables Haskell-like functional programming with JS. You can use most of the powerful ramda functions with point-free style in JSON.

The first element in an array is a function.

["add", 1, 2] // add(1, 2)

To curry a function, nest it.

[["add", 1], 2] // add(1)(2)

A function always needs to be wrapped with [] and to be the first element in the array.

[["map", ["inc"]], [1, 2, 3]] // map(inc)([1, 2, 3])

This is an error because inc is imterpreted as String.

[["map", "inc"], [1, 2, 3]] // map("inc")([1, 2, 3])

Point-free style means you cannot write something like this with the JSON format.

sortBy((v)=> v.age)(people) // ramdajs

It's because you cannot write arbitrary JS lines such as (v)=> v.age.

Instead, you can achieve the same using another ramda funciton prop.

sortBy(prop("age"), people) // ramdajs
["sortBy",["prop", "age"], people] // FPJSON

Reserved First Words

By placing a reserved word in the first spot of an array, you can access the pre-built features.

There are just 6 of them.

"[]"

To create an array of functions without executing them, place "[]" in the first spot, otherwise the ["lte", 2] function will be executed with ["gt", 2] before -3 is passed.

[["anyPass", ["[]", ["lte", 2], ["gt", 2]]], -3] // anyPass(lte(2), gt(2))(-3)

"typ"

To create a type object such as Number, Boolean, String, Array, and Object.

["is", ["typ", "String"], "abc"] // is(String, "abc")

"reg"

To create a RegExp.

["test", ["reg", "a", "i"], "ABC"] // test(new RegExp("a", "i"), "ABC")

"quot;

You can pass a store object as the second argument to fpjson.

To access previously defined variables, use "quot;.

fpjson(["add", ["quot;, "num1"], 1], { "num": 1 }) // 2

"let"

Pure functional programming without any side-effects is easy to get extremely complex and entangled even for simple logics.

"let" inserts global variables to ease up the unnecessary complexisities.

["let", "num1", 1] // let var1 = 1

"var"

var works just like $ except that var needs another argument to invoke.

The last argument can be anything since it will be ignored.

Note that you cannot access a new value within the same composition where it was defined.

let vars = {}
fpjson(["let", "num1", 1], vars) // vars = { "num1" : 1 }
fpjson(["add", ["var", "num1", true], 1], vars) // 2

var is especially convenient in a composition to switch the tracked value..

fpjson([[
  "pipe",
  ["add", 1], // add 1 to 1
  ["let", "num1"], // store 2 to num1
  ["var", "num2"] // switch the ctx to num2, var("num2", 2), but 2 ignored
], 1]),{ num2: 4 }) // => 4 is the final result

This pipeline add 1 to the initial value 1, store it to num1, then switch the context to num2.

Dynamic Variables

Variable names can be dinamically specified with $dynamic_path.

let vars = {}
fpjson(["let", "num1", 1], vars) // vars = { "num1" : 1 }
fpjson(["let", "ln", "num1"], vars) // vars = { "num1" : 1, "ln" : "num1" }
fpjson(["add", ["$, "$ln"], 1], vars) // 2

Dot Notation

Nested fields can be accessed with ..

let vars = {}
fpjson(["let", "o", { num: 1 }], vars) // vars = { "o" : { "num": 1 } }
fpjson(["var", "o.num", true ], vars) // 1