Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

clj

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

clj

Utilities for lazy iterables

  • 0.4.0
  • PyPI
  • Socket score

Maintainers
1

clj

clj is 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 these distinct, drop-while, cycle, first, etc.

The library is oriented toward laziness and performance. Functions are implemented with as little overhead as possible.

Install

pip install clj

Python requirement:

  • 0.4.x: Python 3.9+
  • 0.3.x: Python 3.7+
  • 0.2.x: Python 3.7+
  • 0.1.x: Python 2.7, 3.3+

Usage

The library is considered stable and is used in production.

Example

;; Clojure
(->> coll (map inc) (filter even?) distinct count println)
; which expands to:
(println (count (distinct (filter even? (map inc coll)))))
# Python
from clj import count, distinct, inc, is_even

print(count(distinct(filter(is_even, map(inc, coll)))))

Note that count() 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 Ideas

  • Lazy 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. 0 and [] are logically true in Clojure but false in Python; None is not equivalent to an empty collection).
  • Don’t Reinvent the Wheel. We don’t reimplement built-in functions unless they miss something, like range that can’t be called without argument to yield an infinite sequence.

Support

The general naming scheme is: use underscores instead of hyphens; start the function with is_ if its Clojure counterparts ends with a ?.

Sequences

We aim to implement all Clojure functions that operate on sequences (see the list here). They all work on iterables and return generators by default (Python’s closest equivalent of lazy seqs). We don’t support transducers.

ClojurecljComment
distinctdistinct
filterfilterAlias to Python’s built-in filter.
removeremove
keepkeep
keep-indexedkeep_indexed
conscons
concatconcatEquivalent to itertools.chain.
lazy-cat-Use Python’s itertools.chain.
mapcatmapcat
cyclecycle
interleaveinterleave
interposeinterpose
restrest
next-Use rest.
fnext-Use second.
nnext-Use rest(rest(…))
dropdrop
drop-whiledrop_whileEquivalent to itertools.dropwhile.
nthnext-Use drop.
taketake
take-nthtake_nth
take-whiletake_whileEquivalent to itertools.takewhile.
butlastbutlast
drop-lastdrop_last
flattenflatten
reversereverse
sort-Use Python’s built-in sort.
sort-by-Use sort(…, key=your_function).
shuffleshuffle
split-atsplit_at
split-withsplit_with
partitionpartition(partition n step pad coll) becomes partition(coll, n, step, pad). Only the case step=n is supported for now.
partition-all
partition-bypartition_by
mapmapAlias to Python’s built-in map.
pmap-
replacereplace
reductionsreductions(reductions f i c) becomes reductions(f, c, i).
map-indexedmap_indexed
seque-
firstfirstNone is not a valid parameter.
ffirstffirstNone is not a valid parameter.
nfirstnfirst
secondsecond
nthnth
lastlast
rand-nth-Use Python’s random.choice.
zipmapzipmap
into-
reduce-Use Python’s functools.reduce.
set-Use Python’s set.
vec-Use Python’s list.
into-array-Use Python’s list.
to-array-2d-
frequencies-Use Python’s collections.Counter.
group-bygroup_by
apply-Use the f(*args) construct.
not-empty-
somesome
seq?is_seq
every?every
not-every?not_every
not-any?not_any
empty?-
emptyempty
doseq-Use for … in.
dorundorun
doall-Use Python’s list.
realized?-
seq-Use Python’s list.
vals-Use Python’s dict.values.
keys-Use Python’s dict.keys.
rseq-
subseq
rsubseq
repeatedlyrepeatedly
iterateiterate
repeatrepeat(repeat n x) becomes repeat(x, n). Equivalent to itertools.repeat.
rangerangePrefer Python’s range for everything but infinite generators.
line-seq-Loop over an io.BufferedReader.
resultset-seq-
re-seq-Use Python’s re.finditer.
tree-seqtree_seq
file-seq-Use Python’s os.walk.
xml-seq-
iterator-seq-
enumeration-seq-
hash-map-Use Python’s dict.
array-map-Use Python’s dict.
sorted-map-Use collections.OrderedDict.
sorted-map-by
hash-set-Use Python’s set.
set-Use Python’s set.
sorted-set
sorted-set-by
dedupededupe

We also implemented count, which uses Python’s len when possible and fallbacks on a for loop for other cases.

Functions

We also provide miscellaneous functions as well as functions that work on functions.

ClojurecljComment
identityidentity
partial-Use Python’s functools.partial
compcomp
complementcomplement
constantlyconstantly
juxtjuxt
distinct?is_distinct
ClojurecljComment
incinc
decdec

Keywords

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc