Saturday, August 21, 2021

Janet Programming Language

Feel free to ask questions and join discussion on the Janet Gitter Channel. Gitter provides matrix and IRC bridges as well for users who prefer those.

Other Gitter Rooms

  • help: For getting help with specific problems in the Janet language.
  • website: For discussion and help related to this website and documentation.

Forum

We use GitHub Discussions as our forum, which is more suited to long-form discussion than Gitter.

Janet Docs

For help, you can also check out Janet Docs for Janet documentation with user-provided examples. Feel free to contribute your own examples here to help fellow programmers.

Use Cases

Janet makes a good system scripting language, or a language to embed in other programs. Think Lua or Guile. Janet also can be used for rapid prototyping, dynamic systems, and other domains where dynamic languages shine. Implemented mostly in standard C99, Janet runs on Windows, Linux and macOS. The few features that are not standard C (dynamic library loading, compiler specific optimizations), are fairly straightforward. Janet can be easily ported to new platforms.

Features

  • Minimal setup - one binary and you are good to go!
  • First class closures
  • Garbage collection
  • First class green threads (continuations)
  • Python style generators (implemented as a plain macro)
  • Mutable and immutable arrays (array/tuple)
  • Mutable and immutable hashtables (table/struct)
  • Mutable and immutable strings (buffer/string)
  • Macros
  • Byte code interpreter with an assembly interface, as well as bytecode verification
  • Tail call optimization
  • Direct interop with C via abstract types and C functions
  • Dynamically load C libraries
  • Functional and imperative standard library
  • Lexical scoping
  • Imperative and functional programming
  • REPL
  • Parsing Expression Grammars built in to the core library
  • 300+ functions and macros in the core library
  • Interactive environment with detailed stack traces
  • Export your projects to standalone executables with a companion build tool, jpm

Code Example

(defn sum3
  "Solve the 3SUM problem in O(n^2) time."
  [s]
  (def tab @{})
  (def solutions @{})
  (def len (length s))
  (for k 0 len
    (put tab (s k) k))
  (for i 0 len
    (for j 0 len
      (def k (get tab (- 0 (s i) (s j))))
      (when (and k (not= k i) (not= k j) (not= i j))
        (put solutions {i true j true k true} true))))
  (map keys (keys solutions)))

(let [arr @[2 4 1 3 8 7 -3 -1 12 -5 -8]]
  (printf "3sum of %j: " arr)
  (printf "%j" (sum3 arr)))

Try It

Usage

A REPL is launched when the janet binary is invoked with no arguments. Pass the -h flag to display the usage information. Individual scripts can be run with janet myscript.janet

If you are looking to explore, you can print a list of all available macros, functions, and constants by entering the command all-bindings into the REPL.

$ janet
Janet 1.0.0-dev-cc1ff91  Copyright (C) 2017-2019 Calvin Rose
janet:0:> (+ 1 2 3)
6
janet:10:> (print "Hello, world!")
Hello, world!
nil
janet:34:> (os/exit)
$ janet -h
usage: janet [options] script args...
Options are:
  -h : Show this help
  -v : Print the version string
  -s : Use raw stdin instead of getline like functionality
  -e code : Execute a string of janet
  -r : Enter the repl after running all scripts
  -p : Keep on executing if there is a top level error (persistent)
  -q : Hide prompt, logo, and repl output (quiet)
  -k : Compile scripts but do not execute
  -m syspath : Set system path for loading global modules
  -c source output : Compile janet source code into an image
  -n : Disable ANSI color output in the repl
  -l path : Execute code in a file before running the main script
  -- : Stop handling options

Modules and Libraries

See some auxiliary projects on GitHub. Here is a short list of libraries for Janet to help you get started with some interesting stuff. See the Janet Package Listing for a more complete list. Packages in the listing can be installed via jpm install pkg-name.

  • Circlet - An HTTP server for Janet
  • Joy Web Framework - Framework for web development in Janet
  • JSON - A JSON parser and encoder
  • SQLite3 - Bindings to SQLite
  • WebView - Spawn a browser window for creating HTML+CSS UIs on any platform
  • Jaylib - Bindings to Raylib for 2d and 3d game development
  • JHydro - Cryptography for Janet
  • JanetUI - Bindings to libui

For editor support:



from Hacker News https://janet-lang.org/

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.