Automatic Imports in Eisen Modules - blancas/eisen GitHub Wiki

When Eisen code declares a module name, the resulting namespace gets alias definitions for a number of common Clojure functions whose names are not regular Eisen names, mostly because of dashes. Having these alias names allows user code to call many Clojure functions without explicit import declarations and without having to put those names in backquotes.

This is the list of names from clojure.core.

      assoc-in         assocIn
      drop-while       dropWhile
      file-seq         fileSeq
      get-in           getIn
      group-by         groupBy
      hash-map         hashMap
      hash-set         hashSet
      lazy-seq         lazySeq
      line-seq         lineSeq
      merge-with       mergeWith
      not-any?         notAny?
      not-empty        notEmpty
      not-every?       notEvery?
      partition-all    partitionAll
      partition-by     partitionBy
      pr-str           prStr
      print-str        printStr
      println-str      printlnStr
      re-find          reFind
      re-groups        reGroups
      re-matcher       reMatcher
      re-matches       reMatches
      re-pattern       rePattern
      re-seq           reSeq
      read-line        readLine
      ref-set          refSet
      select-keys      selectKeys
      sort-by          sortBy
      sorted-map       sortedMap
      sorted-map-by    sortedMapBy
      sorted-set       sortedSet
      sorted-set-by    sortedSetBy
      split-at         splitAt
      split-with       splitWith
      take-last        takeLast
      take-nth         takeNth
      take-while       takeWhile
      tree-seq         treeSeq
      update-in        updateIn
      xml-seq          xmlSeq

Names from clojure.string:

blank?, join, split, trim, 
split-lines -> splitLines

Names from clojure.java.io:

file, reader, writer

From clojure.xml:

parse

Going to eisen.user we can use any of the alias names as Eisen names.

(use 'blancas.eisen.core)
(init-eisen)
(eisen-repl)
;; user:
module user.code
takeLast 5 [1 999]
-- (995 996 997 998 999)
join '.' ["now", "is", "the", "time"]
-- now.is.the.time

Customizing the Imports

The automatic imports listed above are installed by the call to init-eisen, which calls function add-auto-decl. A host program may write a custom version of init-eisen where different imports are defined or even none at all. Each call to add-auto-decl installs use declarations to take effect when an Eisen module is created. The arguments are a symbol for the target namespace and a map that contains filters, where the key is either :rename or :only, and the value is a map or a list, respectively.

For example, this is how init-eisen installs a use form for the clojure.string namespace:

  (add-auto-decl
    'clojure.string
    { :only '(blank? join split trim)
      :rename {'split-lines 'splitLines} })