8.18
top
← prev up next →

Extra SRFI LibrariesπŸ”— i

While Racket comes with a number of SRFI libraries, it’s missing quite a lot of useful ones. This collection adds some.

A note on licensings: Most of the included SRFIs are the reference implementations adapted to Racket, and retain the original licenses.

Typical changes to the reference versions include adding contracts and removing now-redundant type checking, avoiding things Scheme allows that Racket doesn’t like if missing an else clause, reorganization of source files, etc.

Modules that are noted as providing an unsafe option have a submodule named unsafe that exports functions without contracts and potentially other run time checks. Passing those functions invalid or out of range arguments can have unpredictable results. Use with caution.

1SRFI-1 List LibraryπŸ”— i

1.1 Typed RacketπŸ”— i

Reference documentation.

Imports SRFI-1 functions into a Shallow Typed Racket module. Circular and dotted lists aren’t always accepted where the documentation says they should be; I still need to work out if it’s even possible to represent a circular list in TR.

The linear update procedures ending with a ! are just aliases for the normal ones, thanks to Racket’s immutable cons cells. This behavior is allowed by the SRFI.

type

( Circular-Listofa)

The type of a homogeneous circular list as returned by circular-list.

1.2 Mutable listsπŸ”— i

SRFI-1 for mutable lists. Procedures have the same name with an m prepended, except ones with list or pair in the name, which turns into mlist or mpair. So, for example, make-mlist, not-mpair? and mxcons.

Also available in an unsafe module with (require (submod srfi/1munsafe)).

1.2.1Extra functionsπŸ”— i

procedure

( list->mlistlst)proper-mlist?

lst:list?
Convert a normal immutable list into one made of mutable pairs.

procedure

( mlist->listmlst)list?

mlst:proper-mlist?
Convert a mutable list into a normal one made of immutable pairs.

2 SRFI-13 String LibrariesπŸ”— i

Reference documentation.

Notes: While Racket comes with a SRFI-13 implementation, it’s only for normal Racket, not Typed Racket. This module can be used instead of having to "require/typed" specific functions from it that you might need in Typed Racket.

3 SRFI-27 Sources of Random BitsπŸ”— i

Reference documentation.

Notes: While Racket comes with a SRFI-27 implementation, it’s only for normal Racket, not Typed Racket. This module can be used instead of having to "require/typed" specific functions from it that you might need in Typed Racket.

4 SRFI-74 Octet-Addressed Binary BlocksπŸ”— i

Reference documentation.

Notes: The version of this module in srfi-lib is just the reference implementation and has, as of Racket 8.15, a serious bug in that the "native" endianness is always big-endian. This, besides being having a version written in Typed Racket, fixes that, and uses a more Racket-specific implementation. You should probably use always use this one, at least until the bug is fixed.

5SRFI-87 => in case clausesπŸ”— i

Reference documentation.

While Racket comes with a SRFI-87 implementation in srfi-lib, trying to use it causes syntax errors. This version actually works, and it follows the Racket convention of using equal? instead of eqv? to test values in case .

6SRFI-111 BoxesπŸ”— i

Reference documentation.

Racket natively supports SRFI-111 single-valued boxes, but this module re-exports SRFI-195 versions of functions to comply with that document.

7SRFI-112 Environment InquiryπŸ”— i

Reference documentation.

Notes: (os-version) always returns #f, but the other functions are all implemented.

8 SRFI-117 Queues based on (mutable) listsπŸ”— i

Reference documentation.

The SRFI uses normal lists, which are immutable in Racket. Therefore, this implemention uses mutable lists of mcons pairs. The single argument form of make-list-queue and two argument form of list-queue-set-list! can be passed normal lists, though. list-queue-list returns a mutable list.

Also available in an unsafe module with (require (submod srfi/117unsafe)).

procedure

( in-list-queuelq)sequence?

lq:list-queue?
Return a sequence that iterates over the elements of the queue. List queues can also be used directly as sequences.

9 SRFI-127 Lazy SequencesπŸ”— i

Reference documentation.

Notes: The lazy sequences described in the SRFI are built on normal, mutable Scheme cons cells. Racket cons cells are in theory immutable, so this implementation instead uses mcons cells; lists of which aren’t compatible with list functions. There’s a few extra functions to help make that easier to work with, as well as the srfi/1m module.

procedure

( list->lseqlst)lseq?

lst:list?
Convert a list into an lseq.

procedure

( make-lseqval...)lseq?

val:any/c
Converts its arguments into an lseq.

procedure

( in-lseqlseq)sequence?

lseq:lseq?
Create a Racket sequence that iterates over the lseq.

10 SRFI-128 Comparators (reduced)πŸ”— i

Reference documentation. Also includes SRFI-162 Comparators sublibrary and SRFI-228 Composing Comparators routines and variables.

The make-eq-comparator, make-eqv-comparator, make-equal-comparator and make-equal-always functions return comparators using the standard Racket eq-hash-code etc. hash functions instead of default-hash.

make-comparator takes an optional keyword argument, "#:secondary-hash", whose value has the same signature as the "hash" one - either (-> any/c exact-integer? ) or #f. This is used for better compability with Racket’s custom hash tables, which take two hash functions. Hash functions can also return negative numbers, contrary to the SRFI spec.

Comparators can be used as flat contracts (And thus predicates) that test their argument against their type test predicate.

10.1Additional definitionsπŸ”— i

procedure

( comparator-secondary-hash-functioncmp)

cmp:comparator?
Returns the secondary hash function associated with the comparator.

procedure

( comparator-secondary-hashcmpobj)exact-integer?

cmp:comparator?
obj:any/c
Returns the secondary hash code of the given value.

procedure

( make-equal-always-comparator)comparator?

Return a comparator that uses equal-always? for equality and equal-always-hash-code /equal-always-secondary-hash-code for hashing.

value

equal-always-comparator:comparator?

A comparator returned by make-equal-always-comparator.

11 SRFI-132 Sort LibrariesπŸ”— i

Reference documentation.

vector-sort and vector-sort! conflict with the ones in racket/vector - the order of the vector and ordering predicate is reversed.

The side-effect-enabled list functions list-merge! and list-delete-neighbor-dups! currently use unsafe-immutable-set-cdr! to modify the lists in place. The test cases pass, but if this becomes an issue in practice (The function has lots of warnings attached), I’ll switch them to just being aliases for the non-side-effect versions.

Available as an unsafe module via (require (submod srfi/132unsafe)).

12 SRFI-133 Vector Library (R7RS-compatible)πŸ”— i

Reference documentation.

See also SRFI-43 included with Racket. Notable differences are functions that take callbacks not passing the current index like they do in 43.

Also available in a Typed Racket version and an unsafe version as (require (submod srfi/133unsafe)).

13SRFI-134 Immutable DequesπŸ”— i

Reference documentation.

Notes:

Deques are equal? if they are the same length and all corresponding elements are equal? .

Deques are streams and sequences.

As of Racket 8.13, implemented with treelists instead of the banker’s deque reference implementation.

Extra functions:

procedure

( in-idequedq)sequence?

dq:ideque?
Returns a sequence that iterates through the deque from front to back. A ideque? can also be used directly as a sequence.

procedure

( in-ideque-backwardsdq)sequence?

dq:ideque?
Returns a sequence that iterates through the deque in reverse order, back to front.

syntax

( for/ideque(sequence-binding...)(body...))

A for comprehension that returns a queue built from the value returned by each iteration of the body.

syntax

( for*/ideque(sequence-binding...)(body...))

A for* comprehension that returns a queue built from the value returned by each iteration of the body.

14SRFI-140 Immutable StringsπŸ”— i

Reference documentation.

Notes:

This module is equivalent to the R7RS (srfi140istrings) library. The other variations laid out in the SRFI are not present. The mutable string functions string-append! and string-replace!, which require resizable strings, are not provided.

Extra functions:

Stuff used by the UTF-16 conversion routines that might be more broadly useful.

procedure

( string-utf-16-lengths[startend])exact-nonnegative-integer?

Returns the number of bytes it takes to encode s as UTF-16. This length does not include a Byte Order Mark (BOM).

procedure

( bytes-utf-16-length b
[ start
end
#:big-endian?big-endian?])
b:bytes?
big-endian?:(or/c boolean? 'check-bom)='check-bom
Returns the number of codepoints in the UTF-16 data in b. The length includes the BOM if present.

If big-endian? is ' check-bom and there is no BOM present, the native system endianness is used.

procedure

( bytes-utf-16-endiannessb[startend])
b:bytes?
Attempts to discover the endianness of the UTF-16 data in b. Returns two values; if it’s big endian or not, and if a BOM was present. If there is no BOM, the endianness is the native system one.

15 SRFI-141 Integer divisionπŸ”— i

Reference documentation.

Notes:

The functions in the typed version are constrained to only take and return exact integers. The regular package’s will accept inexact integers. The typed module also has Fixnum versions with a "fx" prefix - fxfloor/, for example.

16 SRFI-143 FixnumsπŸ”— i

Reference documentation.

17 SRFI-145 AssumptionsπŸ”— i

Reference documentation.

struct

(struct exn:fail:contract:assumeexn:fail:contract ()
#:extra-constructor-namemake-exn:fail:contract:assume
#:transparent)
The type of exception raised by assume.

18 SRFI-146 MappingsπŸ”— i

Reference documentation.

mapping? objects are also ordered-dict? s and many functions in this module can be used with other ordered dicts or even unordered-ones for operations where order doens’t matter. The mapping implementation uses scapegoat trees.

The current make-mapping-comparator and mapping-comparator do not provide ordering.

The linear update versions of functions with names ending in !, if passed an external dict object that supports side effect operations, will update it in place and return it. If passed an immutable dict, they will just return a new object like the non-linear versions. Basically, as long as you follow the guideline of never using a particular dict object again after passing it to a linear update function, everything will Just Work no matter what.

Additional functions:

procedure

( in-ordered-dictod[starting-pos])sequence?

starting-pos:any/c =(dict-iterate-least od)
Return a sequence of key/value values that starts with the least key (Or the given iteration position). The dict-iterate-next implementation of the ordered dict is assumed to return elements in ascending order of keys.

procedure

( in-ordered-dict-keysod[starting-pos])sequence?

starting-pos:any/c =(dict-iterate-least od)
Like in-ordered-dict but only returns the keys.

procedure

( in-ordered-dict-valuesod[starting-pos])sequence?

starting-pos:any/c =(dict-iterate-least od)
Like in-ordered-dict but only returns the values.

hashmap? objects are also dict? s and many functions in this module can be used with other types of dicts. The hashmap implementation uses Racket’s immutable hash tables, but also support mutable side-effect functionality.

19 SRFI-151 Bitwise OperationsπŸ”— i

Reference documentation.

Notes:

Written in Typed Racket. Hopefully the type signatures should be obvious and intuitive. If performance matters, code that uses these routines should also be written in Typed Racket.

20 SRFI-158 Generators and AccumulatorsπŸ”— i

Reference documentation. Also includes SRFI-221 Generator/accumulator sub-library routines.

These generators are not compatible with the ones in "racket/generator". There is an adaptor function provided to wrap Racket generators in SRFI-158 ones, but beware of conflicting generator identifiers in the two modules.

Available as an unsafe module via (require (submod srfi/158unsafe)).

procedure

( generator? obj)boolean?

obj:any/c
Returns #t if the object appears to be a generator - a procedure that can be called with 0 arguments, and, if the number of values it returns is known, only returns 1.

procedure

( rkt-generator->srfi-generatorg)(-> any/c )

Adapt a Racket generator to a SRFI-158 generator. Generators that are called with arguments are not supported.

procedure

( sequence->generator s)(-> any/c )

Adapt a Racket sequence to a SRFI-158 generator.

21 SRFI-160 Homogenous numeric vector librariesπŸ”— i

Reference documentation. In addition to all the numeric types in the SRFI, functions for flvector and fxvector vectors are also provided, with a "fl" and "fx" prefix respectively. If Racket CS ever gains support for 80-bit extflonum? numbers on x86, I’ll add support for extflvector? and f80vector? vectors too (And might adjust the f32vector? contracts to explicitly work with single-flonum? values if CS ever gets them).

All these modules have an unsafe submodule.

Additional functions for converting between flvectors and SRFI-4 vectors:

procedure

( flvector->f32vectorfl[startend])f32vector?

procedure

( flvector->f64vectorfl[startend])f64vector?

procedure

( f32vector->flvectorf32[startend])flvector?

procedure

( f64vector->flvectorf64[startend])flvector?

Additional functions:

procedure

( in-@vectorvec[startend])sequence?

vec:@vector?
end:exact-nonnegative-integer? =(@vector-lengthvec)
Sequence constructor that iterates over the given range of the numeric vector.

22 SRFI-171 TransducersπŸ”— i

Reference documentation.

Notes: The bytevector-u8-* functions have been renamed bytes-* to better match Racket conventions, though the original names are still available as aliases.

Available as an unsafe module via (require (submod srfi/171unsafe)).

Additional functions:

procedure

( set-transducexformfset)any/c

xform:procedure?
set:set?
(set-transducexformfidentityset)any/c
xform:procedure?
identity:any/c
set:set?
Transducer that reduces over the elements of a set, in unspecified order.

procedure

( treelist-transducexformftl)any/c

xform:procedure?
(treelist-transducexformfidentitytl)any/c
xform:procedure?
identity:any/c
Transducer that reduces over the elements of a treelist.

procedure

( hash-transducexformfht)any/c

xform:procedure?
ht:hash?
(hash-transducexformfidentityht)any/c
xform:procedure?
identity:any/c
ht:hash?
Transducer that reduces over the elements of a hashtable. Each element is a cons pair where the car is the key and the cdr the value. Iterator order is unspecified.

procedure

( sequence-transducexformfseq)any/c

xform:procedure?
seq:sequence?
(sequence-transducexformfidentityseq)any/c
xform:procedure?
identity:any/c
seq:sequence?
Transducer that reduces over the elements of a single-valued sequence.

23SRFI-173 HooksπŸ”— i

Reference documentation.

Notes: A hook object is callable as a procedure; (hook-objargs... ) is the same as (hook-runhook-objargs... ).

24SRFI-174 POSIX TimespecsπŸ”— i

Reference documentation.

Notes:

Implemented as a distinct type (A transparent structure), with the range of the seconds values the same as the range of Racket integers.

timespec-hash uses equal-hash-code and might return negative values contrary to the SRFI description of the function.

25 SRFI-175 ASCII character libraryπŸ”— i

Reference documentation.

What the SRFI documentation calls a bytevector is what Racket calls a byte string.

Available as an unsafe module via (require (submod srfi/175unsafe)).

26SRFI-180 JSONπŸ”— i

Reference documentation.

You can never have too many JSON parsers available for a language.

27SRFI-190 Coroutine GeneratorsπŸ”— i

Reference documentation.

Notes: The yield syntax conflicts with "racket/generator".

28SRFI-193 Command lineπŸ”— i

Reference documentation.

Notes:

The heuristics for telling if a command or script or neither is being executed could probably stand to be improved.

The command-line procedure conflicts with the one in "racket/cmdline".

29SRFI-194 Random data generatorsπŸ”— i

Reference documentation.

Notes:

Written in Typed Racket.

make-ellipsoid-generator, make-ball-generator and make-sphere-generator have variants flmake-ellipsoid-generator, flmake-ball-generator and flmake-sphere-generator that return flvectors. The ellipsoid generator functions can take a flvector or a vector of reals. The ball generator functions can take a flvector, vector of reals, or an integer.

30SRFI-195 Multiple-value BoxesπŸ”— i

Reference documentation.

Notes:

Native Racket single-valued boxes are accepted by these functions, and multiple-valued boxes of arity 1 created by this SRFI’s box use them.

Multiple-valued boxes can be compared and hashed with equal? . The usual caveats about modifying a box used as a key in a hash table apply.

30.1Additional procedures and formsπŸ”— i

syntax

( mvboxmatch-pat...)

A match expander to use multiple-valued boxes in match clauses.

(match (box 123)
[(mvboxabc)(list abc)]);'(123)

procedure

( box-immutable arg...)box?

arg:any/c
Creates an immutable box; using set-box! or set-box-value! with it is an error. Only single-valued boxes make immutable? return true.

31SRFI-196 Range ObjectsπŸ”— i

Reference documentation.

Notes: The "range" function from this module conflicts with the one from "racket/list".

31.1Additional functionsπŸ”— i

procedure

( range-empty?r)boolean?

r:range?
Returns true if the range’s length is 0, otherwise false.

procedure

( in-range-objectr)sequence?

r:range?
Returns a sequence that iterates over the range. Range objects can also be used directly as sequences.

32SRFI-202 Pattern-matching Variant of the and-let* Form that Supports Multiple ValuesπŸ”— i

Reference documentation.

Notes:

The reference implementations for this SRFI include one for Racket, but this one is original, using syntax-parse macros. It’s also a lot simpler, which makes me wonder, but it passes all the test cases...

Uses match style pattern matching.

33 SRFI-207 String-notated bytevectorsπŸ”— i

Reference documentation.

The u8"..." reader syntax and I/O functions are not supported.

Available in an unsafe version as (require (submod srfi/207unsafe)).

34 SRFI-208 NaN ProceduresπŸ”— i

Reference documentation.

Notes:

Currently only works with double-precision flonums, though the SRFI allows for other floating point types. While Racket BC supports single precision flonums, Racket CS doesn’t, and I don’t have a version of CS installed that supports extflonums (maybe it doesn’t support them at all either?). If either condition changes I might go back and add support for those types.

The main difference between the untyped and typed versions are that the former includes NaN checking of arguments in the contracts; the typed one has an explicit check to raise an error if passed a non-NaN number. I might remove that check in the future and just say it’s undefined what happens when they’re passed a non-NaN.

35SRFI-210 Procedures and Syntax for Multiple ValuesπŸ”— i

Reference documentation.

Notes:

apply/mv, call/mv, and with-values have been extended to work with Racket’s keyword arguments by taking optional keyword+value arguments after the last documented ones that are passed to the consumer procedure. For example,

(apply/mv~a#\a(values #\b#\c)#:separator",")

The forms and functions that take/return boxes use SRFI-195 multiple-value ones.

35.1Additional functionsπŸ”— i

procedure

( bind/vectorvectransducer...)any

vec:vector?
transducer:procedure?
Like bind/list but takes a vector instead of a list.

36SRFI-214 FlexvectorsπŸ”— i

Reference documentation.

Notes: The impelmentation is built on "data/gvector" and flexvectors are also gvector s.

36.1Additional functionsπŸ”— i

procedure

( flexvectorofc#:flat-contractboolean?)contract?

boolean?:#f
Returns a contract that validates flexvectors whose every element satisfies c.

procedure

( build-flexvectorlenproc)flexvector?

Return a new flexvector len elements long, populated with the results of calling proc for each index.

procedure

( flexvector->bytesfv[startend])bytes?

fv:(flexvectorofbyte? )
start:exact-integer? =0
end:exact-integer? =(flexvector-lengthfv)
Convert a flexvector of bytes to a byte string.

procedure

( bytes->flexvectorbs[startend])(flexvectorofbyte? )

bs:bytes?
start:exact-integer? =0
Convert a byte string to a flexvector.

procedure

( flexvector-bisect-leftfvvalless?[lohi])integer?

fv:flexvector?
val:any/c
less?:(-> any/c?any/c?any/c )
lo:integer? =0
hi:integer? =(flexvector-lengthfv)
Do a binary search in fv for val per SRFI-223 bisect-left.

procedure

( flexvector-bisect-rightfvvalless?[lohi])integer?

fv:flexvector?
val:any/c
less?:(-> any/c?any/c?any/c )
lo:integer? =0
hi:integer? =(flexvector-lengthfv)
Do a binary search in fv for val per SRFI-223 bisect-right.

procedure

( in-flexvectorfv)sequence?

fv:flexvector?
Returns a sequence that iterates through the elements of the flexvector.

37SRFI-217 Integer SetsπŸ”— i

Reference documentation.

"iset" objects also support the Racket gen:set interface and can be used with "racket/set" functions. The set-theory functions like set-union only work when all sets are isets (The same restriction applies to other types of sets).

They also support equal? and are hashable so they can be used as keys in hash tables and sets. The usual warnings about mutating such sets apply.

The reference implementation, using Patricia trees, is currently being used. I’m considering replacing it with one that can more compactly store ranges of numbers.

Available as an unsafe module via (require (submod srfi/217unsafe)).

38 SRFI-223 Generalized binary search proceduresπŸ”— i

Reference documentation. This and the following modules are written in Typed Racket.

SRFI-223 procedures specialized for byte strings.

procedure

( bytes-bisect-leftbsvalless?[lohi])integer?

bs:bytes?
val:byte?
less?:(-> byte? byte? any/c )
lo:integer? =0
Do a binary search in bs for val per SRFI-223 bisect-left.

procedure

( bytes-bisect-rightbsvalless?[lohi])integer?

bs:bytes?
val:byte?
less?:(-> byte? byte? any/c )
lo:integer? =0
Do a binary search in bs for val per SRFI-223 bisect-right.

SRFI-223 procedures specialized for flvectors.

procedure

( flvector-bisect-leftfvvalless?[lohi])integer?

val:flonum?
lo:integer? =0
Do a binary search in fv for val per SRFI-223 bisect-left.

procedure

( flvector-bisect-rightfvvalless?[lohi])integer?

val:flonum?
lo:integer? =0
Do a binary search in fv for val per SRFI-223 bisect-right.

SRFI-223 procedures specialized for fxvectors.

procedure

( fxvector-bisect-leftfvvalless?[lohi])integer?

val:fixnum?
lo:integer? =0
Do a binary search in fv for val per SRFI-223 bisect-left.

procedure

( fxvector-bisect-rightfvvalless?[lohi])integer?

val:fixnum?
lo:integer? =0
Do a binary search in fv for val per SRFI-223 bisect-right.

39SRFI-224 Integer MappingsπŸ”— i

Reference documentation.

"fxmapping" objects support the gen:dict interface and can thus be used with "racket/dict" functions. They also support equal? and are hashable so they can be used as keys in hash tables and sets. The usual warnings about mutating values stored in such mappings apply.

Available as an unsafe module via (require (submod srfi/224unsafe)).

40SRFI-229 Tagged proceduresπŸ”— i

Reference documentation.

41SRFI-232 Flexible curried proceduresπŸ”— i

Reference documentation.

42SRFI-235 CombinatorsπŸ”— i

Reference documentation.

Notes:

The conjoin and disjoin functions conflict with the ones in "racket/function" and group-by with the one in "racket/list".

case-procedure uses equal? to compare values instead of eqv? to match Racket’s case .

Procedures that take or return procedures that take arbitary arguments will work with keyword arguments for the most part.

43 SRFI-238 CodesetsπŸ”— i

Reference documentation.

The only predefined codeset is ' errno with POSIX errno values.

43.1Additional functionsπŸ”— i

procedure

( register-codeset!namedata)void?

name:symbol?
Define and register a new codeset with the given names, numbers and messages. The message is optional, and either the name or number is, but not both.

44 SRFI-239 Destructuring ListsπŸ”— i

Reference documentation.

Normally you’d use match in Racket, but this is a lightweight alternative when just dealing with lists.

44.1Additional functionsπŸ”— i

syntax

( mlist-casemlist-exprlist-case-clause...)

Like list-case but for mutable lists.

procedure

( exn:fail:list-case?obj)boolean?

obj:any/c
Predicate for the exceptions raised by list-case when no matching clause is provided.

45SRFI-258 Uninterned SymbolsπŸ”— i

Reference documentation.

Just repackages (And renames) standard Racket functions. Intended to help with porting code to Racket.

46SRFI-259 Tagged procedures with type safetyπŸ”— i

Reference documentation.

46.1Additional formsπŸ”— i

syntax

( define-procedure-tagnameconstructor-namepredicate-nameaccessor-name)

name : identifier?
constructor-name : identifier?
predicate-name : identifier?
accessor-name : identifier?
A version with a generic name argument that is used to improve error messages and the printed representation of tagged procedures.

top
← prev up next →

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /