Thursday, October 15, 2015

TL;DW for Unknown pearls from the Clojure standard library

This brief and to-the-point talk by Renzo Borgatti can be seen here. He goes through 10 relatively unknown fns that are available without external dependencies, and interesting in some way. Here's the list:
  1. destructure—useful to debug destructuring. "It's like macroexpand, but for destructuring."
  2. reductions—like reduce, but also returns intermediate results
  3. test—docstring says it best: "test [v] finds fn at key :test in var metadata and calls it, presuming failure will throw exception". You might use this to document/demonstrate assertions about a var in the immediate context of the var's definition.
  4. clojure.pprint/cl-format—crazy-powerful formatting function from Common Lisp. Pluralization of English words! Roman numerals! Spelled out English representations of numbers! The docstring links to this documentation on format control strings.
  5.—programmatically open a URL in the system browser
  6.—quick peek into java docs
  7. clojure.reflect/reflect—deep reflection on types. Get variables, fields, methods supported, signatures of each method, etc.
  8. clojure.inspector/inspect-tree—visual inspector of data structures, handy for complex structures. Swing UI.
  9. clojure.lang.PersistentQueue—immutable FIFO queue, with buffers, schedulers, etc.
  10. fnil—nil-patch a fn. Useful to handle nil when it wouldn't be handled otherwise, or else to override nil-handling.
The following get honorable mentions:
  1. counted?—does coll implement 'count in constant time?
  2. reversible?—does coll implement Reversible?
  3. vector-of—uniform-type vectors of unboxed primitives
  4. clojure.set/rename-keys—rename keys in a map
  5.—Clojure data structure diffing
  6. munge—munge special characters in symbols or strings to _ENGLISH_ representations. (munge "!") -> "_BANG_"
  7. gensym—Returns a new symbol with a unique name, optionally prefixed.
  8. seque—Creates a queued seq on another (presumably lazy) seq s.
  9. zippersfunctional tree editing
These were highlights for me: destructure, test, cl-format, reflect, inspect-tree, munge

No comments:

Post a Comment