JSPM

  • Created
  • Published
  • Downloads 7
  • Score
    100M100P100Q31628F
  • License MIT

A Lisp for Node.js

Package Exports

  • node-lisper
  • node-lisper/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (node-lisper) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Node Lisper

A Lisp for Node

(defvar hello "Hello World")
(log (concatenate hello "!!!"))
; naive exponential time complexity
(defun fibonacci n
  (if (< n 2)
      n
      (+ (fibonacci (- n 1))
         (fibonacci (- n 2)))))

(fibonacci 10) ; 55

; to use memo (hashmap) you need to import ALL of these functions
(import std "index-of" "find" "find-index" "hash-table-set"
            "push" "map" "array-in-bounds-p"
            "hash-index" "hash-table-has" "hash-table-get" "hash-table")
(import math "min" "euclidean-mod")

(defun fibonacci-memoized n memo (if (< n 2) n
  (if (hash-table-has memo n) (hash-table-get memo n)
  (do
    (defconstant cache (+ (fibonacci-memoized (- n 1) memo) (fibonacci-memoized (- n 2) memo)))
    (hash-table-set memo n cache)
    cache))))

(fibonacci-memoized 10 (hash-table 10)) ; 55
; Define reusable functions
(defun binary-search
        array target (do
  (loop defun search
        arr target start end (do
    (when (<= start end) (do
        (defvar index (floor (* (+ start end) 0.5)))
        (defvar current (get arr index))
        (if (= target current) target
          (if (> current target)
            (search arr target start (- index 1))
            (search arr target (+ index 1) end)))))))
   (search array target 0 (length array))))
(defconstant is-odd (lambda x . . (= (mod x 2) 1)))
(defconstant mult_2 (lambda x . . (* x 2)))
(defconstant sum (lambda a x . . (+ a x)))
; Pipe the first to a series of composed functions
; (arg (arg .. ) (arg .. ) (ar . . . . ))
(go
  (Array 1 2 3 4 5 6 7 101)
  (remove is-odd)
  (map mult_2)
  (reduce sum 0))
(import std "push""reduce")
(import math "range" "product-array")
(defun factorial n
  (go
    (range 1 n)
    (product-array)))
(factorial 10)

Simple CLI usage - create main.js

import lisp from 'node-lisper'
lisp.cli()
"type": "module",
"scripts": {
  "lisp": "node index.js"
}

interpred

yarn

yarn lisp -file <filepath> -r

npm

npm run lisp -- -file <filepath> -r

or compile

yarn

yarn lisp -s <filepath lisp> -d <filepath js> -c

npm

npm run lisp -- -s <filepath lisp> -d <filepath js> -c

show help

yarn

yarn lisp -help

npm

npm run lisp -- -help
-------------------------------------
-help
-------------------------------------
-lib                      target lib
-------------------------------------
-doc              list lib functions
-------------------------------------
-import           log import for lib
-------------------------------------
-s                    prepare a file
-------------------------------------
-d               file to compile js
-------------------------------------
-c                    compile to js
-------------------------------------
-r                  interpret & run
-------------------------------------
-p      interpret & run with 0 deps
-------------------------------------
-m                      minify code
-------------------------------------
-repl    start Read Eval Print Loop
-------------------------------------

Search available functions in libraries

yarn lisp -lib std -doc binary

Parse, Interpred & Compile

import lisp from 'node-lisper'
lisp.parse('(+ 1 2)') // [[{  t: 'f', v: '+' }, { t: 'a', v: 1 }, { t: 'a', v: 2 }]]
lisp.interpred('(+ 1 2)') // 3
lisp.compile('(+ 1 2)') // 3 but faster!
lisp.js(lisp.parse('(+ 1 2)')).program // (1 + 2); as js

Compiles to JavaScript

(import std "remove" "map" "reduce")
(go
  (Array 1 2 3 4 5 6 7 101)
  (remove (lambda x . . (= (mod x 2) 1)))
  (map (lambda x . . (* x 2)))
  (reduce (lambda a x . . (+ a x)) 0))
reduce(
  map(
    remove([1, 2, 3, 4, 5, 6, 7, 101], (x, _1, _2) => {
      return +(x % 2 === 1)
    }),
    (x, _1, _2) => {
      return x * 2
    }
  ),
  (a, x, _2, _3) => {
    return a + x
  },
  0
)
; Tail Call Optimization
(loop defun sum-below number sum (do
(if (= number 0) sum (sum-below (- number 1) (+ sum number)))))
(log (sum-below 10000 0))
var log = (msg) => { console.log(msg) return msg },
    tco = (fn) => (...args) => {
      let result = fn(...args)
      while (typeof result === 'function') result = result()
      return result
    }
var sumBelow, rec_32721849989891052
;(sumBelow = tco(
  (rec_32721849989891052 = (number, sum) => {
    return +(number === 0)
      ? sum
      : () => rec_32721849989891052(number - 1, sum + number)
  }),
  rec_32721849989891052
)),
  sumBelow
log(sumBelow(10000, 0))

Types validated by interpretation

(deftype find-bag (Lambda
                  ; ((("golden" "yellow") ((0 "dark" "blue") (2 "silver" "gray") ... )) ... )
                  (Or (Array (Array (Array (String) (String)) (Array (Array (Number) (String) (String))))))
                  (Or (String)) ; "golden"
                  (Or (String)) ; "yellow"
                  (Or
                    ; ((0 "dark" "blue") (2 "silver" "gray") ... )
                    (Array (Array (String) (String)) (Array (Array (Number) (String) (String))))
                    ; 0 - couldn't find
                    (Number))))
(defun find-bag bags left right
          (find bags
            (lambda x . . (and
              (= (car (car x)) left)
              (= (car (cdr (car x))) right)))))