Skip to content

ktakashi/r6rs-ulid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

ULID for R6SA Scheme

This is an implementation of ULID in portable R6RS Scheme.

The library requires the following SRFIs:

  • SRFI-19
  • SRFI-27

Or implementation specific libraries for the below helper libraries:

  • (ulid time)
  • (ulid random)

This library is inspired by ULID for R7RS Scheme

Tested implementations

  • Sagittarius 0.9.8
  • Chez Scheme 9.5.1
  • Racket v8.3 (plt-r6rs)

How to use

(import (rnrs)
        (ulid))
(define gen-ulid (make-ulid-generator))

make-ulid-generator creates a new ULID generator. The procedure may take two optional arguments, random-generator and millisecond-generator.
The first one must take an argument which is an integer indicates how many bits of random integer must be returned.
The second one must be a thunk returning a current millisecond.

(gen-ulid) ;; -> #<ulid>

Calling the generator procedure created with make-ulid-generator returns a new ULID object. You can retrieve its timestamp and randomness fields by ulid-timestamp and ulid-randomness, both in exact nonnegative integers, respectively.

NOTE: This library does increment the randomness field if the timestamp of the previous ULID and creating ULID are the same as the ULID specification mentioned. Also, it takes a millisecond-generator which may return the constant value. If the randomness reaches to the maximum value, (expt 2 80) then &ulid will be raised. It is user's responsibility not to pass constant value generator.

(ulid->integer ulid)    ;; -> an exact integer
(ulid->bytevector ulid) ;; -> a bytevector
(ulid->string ulid)     ;; -> a Base32 encoded string

Above procedures convert the given ulid to an exact integer, bytevector or string, respectively.

(integer->ulid integer) ;; -> #<ulid>
(bytevector->ulid bv)   ;; -> #<ulid>
(string->ulid str)      ;; -> #<ulid>

Above procedures convert the given exect integer, bytevector or string to ULID object, respectively.

(ulid=? ulid0 ulid1 ulid*...)
(ulid<? ulid0 ulid1 ulid*...)
(ulid-hash ulid)

Equality predicate, ordering predicate and hash function.

Unlike the R7RS version of ULID library, this library doesn't provide ULID comparator.

Limitations

On Chez Scheme, it doesn't provide a good way of initialise a random seed. So, the default random generator generates the same value over and over again.

Default implementation of random generator uses SRFI-27, which doesn't require the implementation to provide secure random. Please check your implementation's document which psuedo random algorithm is used and replace with an appropreate alternative if needed.

Testing

If your R6RS implementation supports SRFI-64, you can run the tests/test.scm file.