We have variants of Python that can use Python libs: welcome to Dogelang, Mochi, Hy, Coconut and Hask.
We can also have languages that target the Python platform without being necessarily compatible with Python, and domain-specific languages.
For more ressources related to functional programming in Python, see the Awesome Functional Python list.
Table of Contents
The following languages can make use of the Python libraries.
Dogelang | |
---|---|
sources | https://github.com/pyos/dg |
doc | https://pyos.github.io/dg/ |
v1 ? | yes, april 2015 |
created | june, 2012 |
print "wow" "two lines" sep: "\n"
print $ "> {}: {}".format "Karkat" "Reference something other than Doge"
print <| 'What' + 'ever.' : 'This is the same thing ' + 'in a different direction.' |> print
->
notation)function = arg1 arg2 -> : print (arg1.replace "Do " "Did ") arg2 sep: ", " end: ".\n"
function "Do something" "dammit"
<-
)f = (+)
f 1 2 == 3
bind
is functools.partial
)f = (2 *)
f 10 == 20
foldl
and foldl1
, scanl
, flip
,
takewhile
and dropwhile
(from itertools
), take
and drop
,
iterate
, head
and fst
, tail
, snd
, last
and init
.decorators don't need special syntax, they're just called with a function
wtf = the~decorator~ $ ->
pip3 install git+<https://github.com/pyos/dg>
Editor | |
---|---|
Gedit | https://github.com/pyos/dg-gedit/ |
Sublime | https://github.com/pyos/dg-textmate/ |
Pygments support.
Project | |
---|---|
dogeweb , a functional web framework atop asyncio | https://pyos.github.io/dogeweb/ |
Hissp is a modular Lisp implementation that compiles to a functional subset of Python—Syntactic macro metaprogramming with full access to the Python ecosystem!
Hissp | |
---|---|
sources | https://github.com/gilch/hissp |
doc | https://hissp.readthedocs.io/ |
v1 ? | no, v0.2 as of May 2021 |
created | 2019 |
discuss | gitter |
The Hissp compiler is written in Python 3.8.
The Hissp compiler should include what it needs to achieve its goals, but no more. Bloat is not allowed.
Hissp compiles to an unpythonic functional subset of Python.
Hissp's basic macros are meant to be just enough to bootstrap native unit tests and demonstrate the macro system. They may suffice for small embedded Hissp projects, but you will probably want a more comprehensive macro suite for general use.
You do not need Hissp installed to run the final compiled Python output
(defmacro attach (target : :* args)
"Attaches the named variables as attributes of the target.
Positional arguments use the same name as the variable.
Names after the ``:`` are key-value pairs.
"
(let (iargs (iter args)
$target `$#target)
(let (args (itertools..takewhile (lambda (a)
(operator..ne a ':))
iargs))
`(let (,$target ,target)
,@(map (lambda (arg)
`(setattr ,$target ',arg ,arg))
args)
,@(map (lambda (kw)
`(setattr ,$target ',kw ,(next iargs)))
iargs)
,$target))))
Hy | |
---|---|
sources | https://github.com/hylang/hy/ |
doc | http://hylang.org/ |
v1 ? | no |
created | december, 2012 |
online REPL | https://try-hy.appspot.com/ |
discuss | google group |
IRC | hy on freenode |
->
and ->>
(similar to
pipes)(-> (read) (eval) (print) (loop))
(import [sh [cat grep wc]])
(-> (cat "/usr/share/dict/words") (grep "-E" "^hy") (wc "-l")) ; => 210
(require hy.contrib.anaphoric)
(list (ap-map (* it 2) [1 2 3])) ; => [2, 4, 6]
pip install hy
Editor | |
---|---|
Emacs | https://github.com/hylang/hy-mode |
All | lisp modes for any editor |
Project | |
---|---|
Github trending | https://github.com/trending/hy |
Live coding Blender | https://github.com/chr15m/blender-hylang-live-code |
Title | |
---|---|
How Hy backported "yield from" to Python 2 | http://dustycloud.org/blog/how-hy-backported-yield-from-to-python2/ |
Basilisp | |
---|---|
sources | https://github.com/basilisp-lang/basilisp |
doc | https://basilisp.readthedocs.io/en/latest/ |
v1 ? | no (as of 2024, Jan) but "generally stable at this point" |
created | initial development release in 2018 |
Planned features:
Basilisp is still young and so lacks many features that more mature languages and runtimes might include. There are many such planned features that will hopefully improve the ergonomics of the project for new developers.
fundamental differences and omissions in Basilisp that make it differ from Clojure:
(def
^{:doc "Returns the second element in a Seq."
:arglists '([seq])}
second
(fn* second [seq] (first (rest seq))))
Mochi | |
---|---|
sources | https://github.com/i2y/mochi |
doc | many examples |
v1 ? | no |
created | v0.1 on december, 2014 |
range(1, 31)
|> map(fizzbuzz)
|> pvector()
|> print()
->
notation and
$1
for the arguments) pip3 install mochi
Editor | |
---|---|
Atom | https://github.com/i2y/language-mochi |
Coconut | |
---|---|
sources | https://github.com/evhub/coconut |
doc | https://coconut.readthedocs.io |
v1 ? | yes, on june, 2016 |
created | february, 2015 (v0.1) |
Coconut compiles to Python (not CPython bytecode, so it supports other Python implementations: PyPy, Jython, etc)
Coconut code runs on any major Python version, 2 or 3
all valid Python 3 is valid Coconut: you can write standard Python3 in Coconut.
ipython / jupyter support (installed by default)
(1, 2) |*> (+) |> sq |> print
For multiline pipes, surround them with parenthesis (python rule that every newline inside parenthesis is ignored):
(
"hello"
|> print
)
match x in value:
), guards$
sign right after a function name)expnums = map(pow$(2), range(5))
expnums |> list |> print
(|
and |)
)..
)
fog = f..g
->
syntax)10_000_000
)@ wrapper1 .. wrapper2 $(arg)
pip install coconut
Editor | |
---|---|
Emacs | https://github.com/NickSeagull/coconut-mode |
Sublime | https://github.com/evhub/sublime-coconut |
Vim | https://github.com/manicmaniac/coconut.vim |
Hask | |
---|---|
sources | https://github.com/billpmurphy/hask |
doc | on github |
v1 ? | no |
created | july, 2015 |
Hask is a pure-Python, zero-dependencies library that mimics most of the core language tools from Haskell, including:
@sig(H/ "a" >> "b" >> "a")
def const(x, y):
return x
def fib(x):
return ~(caseof(x)
| m(0) >> 1
| m(1) >> 1
| m(m.n) >> fib(p.n - 1) + fib(p.n - 2))
Features not yet implemented, but coming soon:
- Python 3 compatibility
- Better support for polymorphic return values/type defaulting
- Better support for lazy evaluation (beyond just the List type and pattern matching)
- More of the Haskell standard library (Control.* libraries, QuickCheck, and more)
- Monadic, lazy I/O
git clone https://github.com/billpmurphy/hask
python setup.py install
Rabbit | |
---|---|
sources | https://github.com/evhub/rabbit |
doc | no doc |
v1 ? | yes, on oct, 2014. DISCONTINUED |
created | v0.1 on may, 2014 |
From the author's words: (src)
Coconut is my attempt to fix the mistakes I thought I made with Rabbit, namely:
- Coconut is compiled, while Rabbit is interpreted, making Coconut much faster
- Coconut is an extension to Python, while Rabbit is a replacement, making Coconut much easier to use
Quicksort:
qsort(l) = (
qsort: (as ~ \x\(x @ x<=a)) ++ a ++ qsort: (as ~ \x\(x @ x>a))
$ a,as = l
) @ len:l
MakrellPy | |
---|---|
sources | https://github.com/hcholm/makrell-py |
v1 ? | no |
created | February, 2024 |
MakrellPy, part of the Makrell language family, is a general-purpose, functional and homoiconic programming language with two-way Python interoperability, metaprogramming support and simple syntax. The language family is based on the Makrell Base Format, a general data format that can be used both for programming languages and data interchange. Other family members include MRON, a lightweight alternative to JSON, and MRML, a lightweight alternative to XML and HTML.
{fun add [x y]
x + y}
a = {add 2 3}
{print a} # 5
a | print # same, with pipe operator
f = [x y] -> {do
{print "multiline lambda here"}
x * y
}
{print {f 2 3}} # function call
add3 = {f 3 _} # partial application
2 | {+ 3} | {* 5} # operators as functions
add3mul5 = add3 >> {* 5} # function composition
pip install makrell
Editor | |
---|---|
Visual Studio Code | https://marketplace.visualstudio.com/items?itemName=hcholm.vscode-makrell |
Other | MakrellPy is supported by the Language Server Protocol, so it should work with any editor that supports LSP. |
A statically typed language that can deeply improve the Python ecosystem
Erg | |
---|---|
sources | https://github.com/erg-lang/erg |
doc | https://erg-lang.github.io/ |
v1 ? | no, v0.4.2 as of September 2022 |
created | 2022 |
The Erg compiler is written in Rust.
Erg is a pure object-oriented language. Everything is an object; types, functions, and operators are all objects. On the other hand, Erg is also a functional language. Erg requires some kinds of markers to be placed on code that causes side effects or changes internal state, which can localize the complexity of code. This will greatly improve the maintainability of your code.
Erg is internally compatible with Python and can import the Python API at zero cost.
# Functional style (immutable), same as `sorted(list)` in Python
immut_arr = [1, 3, 2]
assert immut_arr.sort() == [1, 2, 3]
# Object-oriented style (mutable)
mut_arr = ![1, 3, 2]
mut_arr.sort!()
assert mut_arr == [1, 2, 3]
i = !1
i.update! old -> old + 1
assert i == 2
# Functions cannot cause side effects
inc i: Int! =
i.update! old -> old + 1
# SyntaxError: cannot call a procedural method in a function
# hint: only methods of mutable types can change the state of objects
# Code that uses a lot of side effects is redundant, so you will naturally write pure code
Counter! = Inherit Int!
Counter!.
new i: Int = Self!::__new__ !i
inc! ref! self =
self.update! old -> old + 1
c = Counter!.new 1
c.inc!()
assert c == 2
Haxe | |
---|---|
sources | https://github.com/HaxeFoundation/haxe |
official website | https://haxe.org/ |
doc | https://haxe.org/documentation/introduction/ |
online REPL | _http://try.haxe.org/ |
v1 ? | v3 |
Haxe is an open source toolkit that allows you to easily build cross-platform tools and applications that target many mainstream platforms (Python, ActionScript3, C++, C#, Flash, Java, Javascript, NekoVM, PHP, Lua).
class Test {
static function main() {
var people = [
"Elizabeth" => "Programming",
"Joel" => "Design"
];
for (name in people.keys()) {
var job = people[name];
trace('$name does $job for a living!');
}
}
}
Probabilistic logic programs are logic programs in which some of the facts are annotated with probabilities.
ProbLog | |
---|---|
official website | https://dtai.cs.kuleuven.be/problog/ |
sources | https://bitbucket.org/problog/problog |
doc | http://problog.readthedocs.io/en/latest/ |
v1 ? | yes, even v2 |
online tutorial and REPL | https://dtai.cs.kuleuven.be/problog/tutorial.html |
ProbLog is built with Python. Its only requirement is Python2.7 or 3.
One can interact with ProbLog from within Python code.
pip install problog
PyDatalog | |
---|---|
official website | https://sites.google.com/site/pydatalog/ |
sources | https://github.com/pcarbonn/pyDatalog |
doc | https://sites.google.com/site/pydatalog/Online-datalog-tutorial |
v1 ? | v0.17 (january, 2016) |
PyPy ? | yes |
pyDatalog adds the logic programming paradigm to Python. Logic programmers can now use the extensive standard library of Python, and Python programmers can now express complex algorithms quickly.
from pyDatalog import pyDatalog
pyDatalog.create_terms('factorial, N')
factorial[N] = N*factorial[N-1]
factorial[1] = 1
print(factorial[3]==N) # prints N=6
pip install pyDatalog
pip install sqlalchemy
No examples found, only testimonials.
RBQL | |
---|---|
official website | https://rbql.org |
sources | https://github.com/mechatroner/RBQL |
v1 ? | no |
PyPy ? | pip install rbql |
RBQL is both a library and a command line tool which provides SQL-like language with Python expressions
RBQL is integrated into "Rainbow CSV" text editor plugins available for VSCode, Vim, Sublime, Atom
Main Features:
import rbql
input_table = [
['Roosevelt',1858,'USA'],
['Napoleon',1769,'France'],
['Dmitri Mendeleev',1834,'Russia'],
['Jane Austen',1775,'England'],
['Hayao Miyazaki',1941,'Japan'],
]
user_query = 'SELECT a.name, "birth century: {}".format(a.DOB // 100 + 1) WHERE a.name == "Roosevelt" or re.search("an", a.country, re.IGNORECASE) is not None ORDER BY random.random()'
output_table = []
warnings = []
rbql.query_table(user_query, input_table, output_table, warnings, input_column_names=['name', 'DOB', 'country'])
for record in output_table:
print(','.join([str(v) for v in record]))
Monte is a "nascent dynamic programming language reminiscent of Python and E. It is based upon The Principle of Least Authority (POLA), which governs interactions between objects, and a capability-based object model, which grants certain essential safety guarantees to all objects".
Monte | |
---|---|
Sources | https://github.com/monte-language |
Doc | https://monte.readthedocs.io/en/latest/intro.html |
v0.1 ? | yes, v2016.1 |
Built on Pypy.
Pixie | |
---|---|
Sources | https://github.com/pixie-lang/pixie |
Doc | Examples: https://github.com/pixie-lang/pixie/tree/master/examples |
v0.1 ? | no |
REPL, installer, test runner,… | https://github.com/pixie-lang/dust |
IRC | #pixie-lang on Freenode |
Pixie is built in RPython, the same language PyPy is written in, and as such "supports a fairly fast GC and an amazingly fast tracing JIT".
Inspired by Clojure.
From the FAQ:
;; This code adds up to 10000 from 0 via calling a function that takes a variable number of arguments.
;; That function then reduces over the argument list to add up all given arguments.
(defn add-fn [& args]
(reduce -add 0 args))
(loop [x 0]
(if (eq x 10000)
x
(recur (add-fn x 1))))
RSqueak | |
---|---|
Sources | https://github.com/HPI-SWA-Lab/RSqueak |
Doc | http://rsqueak.readthedocs.io |
with all-in-one multiplatform bundles and 32 bits binaries.