Learning Fennel from Clojure

submited by
Style Pass
2024-11-01 21:30:04

Fennel takes a lot of inspiration from Clojure. If you already know Clojure, then you'll have a good head start on Fennel. However, there are still a lot of differences! This document will guide you thru those differences and get you up to speed from the perspective of someone who already knows Clojure.

Fennel and Lua are minimalist languages, and Clojure is not. So it may take some getting used to when you make assumptions about what should be included in a language and find that it's not. There's almost always still a good way to do what you want; you just need to get used to looking somewhere different. With that said, Fennel is easier to learn since the conceptual surface area is much smaller.

Clojure and Fennel are both languages which have very close integration with their host runtime. In the case of Clojure it's Java, and in the case of Fennel it's Lua. However, Fennel's symbiosis goes beyond that of Clojure. In Clojure, every function implements the interfaces needed to be callable from Java, but Clojure functions are distinct from Java methods. Clojure namespaces are related to Java packages, but namespaces still exist as a distinct concept from packages. In Fennel you don't have such distinctions. Every Fennel function is indistinguishable from a Lua function, and every Fennel module is indistinguishable from a Lua module.

Clojure runs on the JVM, but it also has its own standard library: the clojure.core namespace as well as supplemental ones like clojure.set or clojure.java.io provide more functions. In Fennel, there are no functions whatsoever provided by the language; it only provides macros and special forms. Since the Lua standard library is quite minimal, it's common to pull in 3rd-party things like Lume, LuaFun, or Penlight for things you might expect to be built-in to the language, like merge or keys. There's also an experimental Cljlib library, that implements a lot of functions from clojure.core namespace, and has a set of macros to make writing code more familiar to Clojure programmers, like adding syntax for defining multi-arity functions, or multimethods, also providing deep comparison semantics, sequence abstraction, and some addition data structures, like sets.

Leave a Comment