ZLib

is a high-level wrapper around zlib.

Installation

(load "https://veitheller.de/git/carpentry/zlib.git@0.0.1")

Usage

The ZLib module provides only two functions, inflate and deflate. These functions work in tandem to provide you with data compression.

; deflate returns a Result of either binary data or an error message
(let [deflated (ZLib.deflate "mystring")]
  (match deflated
    ; inflate returns a Result of either a string or an error message
    (Success bin) (println* &(inflate bin))
    (Error msg) (IO.errorln &msg)))

Because it’s a Result type, we can apply combinators to it.

(=> (ZLib.deflate "mystring")
    (Result.and-then &ZLib.inflate)
    (Result.map-error &(fn [msg] (do (println* &msg) msg)))
)

You can also choose different levels of compression using inflate-with. The levels are defined in ZLib.ZLevel, and are NoCompression, BestSpeed, BestCompression, and DefaultCompression, which is, well, the default.

ZBytes

module

Module

ZLevel

module

Module

is a type used in conjunction with deflate-with. It controls the compression level.

The constructors are NoCompression, BestSpeed, BestCompression, and DefaultCompression, which is, well, the default.

deflate

defn

(λ [&String] (Result ZBytes String))

                    (deflate s)
                

takes a bytes object s and returns a Result.

The Result will be a Success containing the deflated bytes if all goes well, and an Error returning an error message otherwise.

It is equivalent to calling deflate-with with (ZLevel.DefaultCompression).

deflate-with

defn

(λ [&String, ZLevel] (Result ZBytes String))

                    (deflate-with s level)
                

takes a bytes object s, a Zlevel level and returns a Result.

The Result will be a Success containing the deflated bytes if all goes well, and an Error returning an error message otherwise.

inflate

defn

(λ [ZBytes] (Result String String))

                    (inflate s)
                

takes a bytes object s and returns a Result.

The Result will be a Success containing the inflated string if all goes well, and an Error returning an error message otherwise.