Package: clj
cljcljis a collection of functions to work with lazy iterators in Python.It’s a module for those times when you did too much Clojure and came back to Python wondering where are all thesedistinct,drop-while,cycle,first, etc.The library is oriented toward laziness and performance. Functions are implemented with as little overhead as possible.Installpip install cljSince 0.2.0, only Python 3.7+ is supported.UsageThe library is considered stable and is used in production.Example;; Clojure(->>coll(mapinc)(filtereven?)distinct countprintln); which expands to:(println(count(distinct(filtereven?(map inccoll)))))# Pythonfromcljimportcount,distinct,inc,is_evenprint(count(distinct(filter(is_even,map(inc,coll)))))Note thatcount()works on both sequences in generators; in the latter case it doesn’t load everything in memory like
e.g.len(list(g))would do.Core IdeasLazy by default. All functions should work on arbitrary iterators and return generators.This is Python. We keep Python’s semantics instead of trying to reproduce Clojure in Python (e.g.0and[]are
logically true in Clojure but false in Python;Noneis not equivalent to an empty collection).Don’t Reinvent the Wheel. We don’t reimplement built-in functions unless they miss something, likerangethat can’t
be called without argument to yield an infinite sequence.SupportThe general naming scheme is: use underscores instead of hyphens; start the function withis_if its Clojure
counterparts ends with a?.SequencesWe aim to implement all Clojure functions that operate on sequences
(seethe list here). They all work on iterables and return generators by default (Python’s closest equivalent of
lazy seqs). We don’t support transducers.ClojurecljCommentdistinctdistinctfilterfilterAlias to Python’s built-infilter.removeremovekeepkeepkeep-indexedkeep_indexedconsconsconcatconcatEquivalent toitertools.chain.lazy-cat-Use Python’sitertools.chain.mapcatmapcatcyclecycleinterleaveinterleaveinterposeinterposerestrestnext-Userest.fnext-Usesecond.nnext-Userest(rest(…))dropdropdrop-whiledrop_whileEquivalent toitertools.dropwhile.nthnext-Usedrop.taketaketake-nthtake_nthtake-whiletake_whileEquivalent toitertools.takewhile.butlastbutlastdrop-lastdrop_lastflattenflattenreversereversesort-Use Python’s built-insort.sort-by-Usesort(…, key=your_function).shuffleshufflesplit-atsplit_atsplit-withsplit_withpartitionpartition(partition n step pad coll)becomespartition(coll, n, step, pad). Only the casestep=nis supported for now.partition-allpartition-bypartition_bymapmapAlias to Python’s built-inmap.pmap-replacereplacereductionsreductions(reductions f i c)becomesreductions(f, c, i).map-indexedmap_indexedseque-firstfirstNoneis not a valid parameter.ffirstffirstNoneis not a valid parameter.nfirstnfirstsecondsecondnthnthlastlastrand-nth-Use Python’srandom.choice.zipmapzipmapinto-reduce-Use Python’sfunctools.reduce.set-Use Python’sset.vec-Use Python’slist.into-array-Use Python’slist.to-array-2d-frequencies-Use Python’scollections.Counter.group-bygroup_byapply-Use thef(*args)construct.not-empty-somesomeseq?is_seqevery?everynot-every?not_everynot-any?not_anyempty?-emptyemptydoseq-Usefor … in.dorundorundoall-Use Python’slist.realized?-seq-Use Python’slist.vals-Use Python’sdict.values.keys-Use Python’sdict.keys.rseq-subseqrsubseqrepeatedlyrepeatedlyiterateiteraterepeatrepeat(repeat n x)becomesrepeat(x, n). Equivalent toitertools.repeat.rangerangePrefer Python’srangefor everything but infinite generators.line-seq-Loop over anio.BufferedReader.resultset-seq-re-seq-Use Python’sre.finditer.tree-seqtree_seqfile-seq-Use Python’sos.walk.xml-seq-iterator-seq-enumeration-seq-hash-map-Use Python’sdict.array-map-Use Python’sdict.sorted-map-Usecollections.OrderedDict.sorted-map-byhash-set-Use Python’sset.set-Use Python’sset.sorted-setsorted-set-bydedupededupeWe also implementedcount, which uses Python’slenwhen possible and fallbacks on aforloop for other cases.FunctionsWe also provide miscellaneous functions as well as functions that work on functions.ClojurecljCommentidentityidentitypartial-Use Python’sfunctools.partialcompcompcomplementcomplementconstantlyconstantlyjuxtjuxtdistinct?is_distinctClojurecljCommentincincdecdec