JSPM

  • Created
  • Published
  • Downloads 7
  • Score
    100M100P100Q31612F
  • 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

(let hello "Hello World")
(log (concatenate hello "!!!"))
(function fibonacci n
  (if (< n 2)
      n
      (+ (fibonacci (- n 1))
         (fibonacci (- n 2)))))

(fibonacci 10)
; 55
; Define reusable modules
(function binary-search
        array target (block
  (loop search
        arr target start end (block
    (if (<= start end) (block
        (let index (floor (* (+ start end) 0.5)))
        (let 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))))
(let is-odd (lambda x i o (= (mod x 2) 1)))
(let mult_2 (lambda x i o (* x 2)))
(let sum (lambda a x i o (+ a x)))
; Pipe the first to a series of composed functions
; (arg (arg .. ) (arg .. ) (ar . . . . ))
(do
  (Array 1 2 3 4 5 6 7 101)
  (remove is-odd)
  (map mult_2)
  (reduce sum 0))

Simple CLI usage - create main.js

import lisp from './node-lisper'
lisp.cli()
"scripts": {
  "lisp": "node main.js"
}

interpred

yarn lisp -file <filepath> -r

or compile

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

show help

yarn lisp -help
-------------------------------------
-help
-------------------------------------
-std             list std functions
-------------------------------------
-import          log import for std
-------------------------------------
-s                   prepare a file
-------------------------------------
-d               file to compile js
-------------------------------------
-c                    compile to js
-------------------------------------
-r                  interpret & run
-------------------------------------
-p      interpret & run with 0 deps
-------------------------------------
-repl    start Read Eval Print Loop
-------------------------------------

Parse, Interpred & Compile

import lisp from './node-lisper'
lisp.parse('(+ 1 2)') // [[{  type: 'apply', value: '+' }, { type: 'atom', value: 1 }, { type: 'atom', value: 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")
(do
  (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 sum-below number sum (block
(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))