Skip to content
Union types and some other functional-y stuff for Hy
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
LICENSE
README.rst
hyskell.hy
setup.py
test_hyskell.hy

README.rst

Hyskell

This brings two functional-ish things to Hy: accumulating for loops and unions/pattern matching. Just prefix your files with this:

(require hyskell)

accfor

accfor is a for loop that is actually an iterator. It's basically genexpr with a nicer syntax that can take anything; not just a single expression.

(print (list (accfor [x [1 2 3]] x))) ; prints [1 2 3]

defunion

Defines a union type:

(defunion Node
  (Nint val)
  (Nstr val))

This example defines three types: Node (a base class), Nint (a class with one attribute: val), and Nstr (same as Nint).

You can use the types like any other type:

(setv i (Nint 7))
(setv s (Nstr "abc"))

match

True ML-style pattern matching:

(match value
  [[1 2 3] (print "Got list [1 2 3]")] ; against a list
  [[:a 2 3] (print "Got list with a =" a)] ; grab values with :
  [(, 1 2) (print "Got tuple (1, 2)")] ; against a tuple
  [1 (print "Got 1")] ; against an int or string
  [(Nint :v) (print "Got Nint with v =" v)] ; against a union branch
  [(Nstr (:val "abc")) (print "Got Nstr with val of abc")] ; use : at the beginning of an expression to test attributes
  [(Nstr _) (print "Got Nstr")] ; use _ to ignore values
  [[1 2 ...] (print "Got list that starts with 1 and 2")] ; use ... to allow extra items at the end
  [[_ _ ...] (print "Got list with >= 2 elements")] ; use ... with _ to do cool stuff
  [_ (print "Got something weird!")]) ; you can also use _ for a fallthrough statement

If none of the branches match, a hyskell.MatchFailure exception is thrown.

Examples

See test_hyskell.hy for the unit tests, written using HyTest.

You can’t perform that action at this time.