Skip to content

Commit aea6326

Browse files
Maria Neiseswannodette
authored andcommitted
CLJS-1100: Add docstrings to core protocols.
1 parent 4a97e04 commit aea6326

File tree

1 file changed

+193
-58
lines changed

1 file changed

+193
-58
lines changed

src/cljs/cljs/core.cljs

Lines changed: 193 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,9 @@
305305
"Marker protocol")
306306

307307
(defprotocol IFn
308+
"Protocol for adding the ability to invoke an object as a function.
309+
For example, a vector can also be used to look up a value:
310+
([1 2 3 4] 1) => 2"
308311
(-invoke
309312
[this]
310313
[this a]
@@ -330,84 +333,154 @@
330333
[this a b c d e f g h i j k l m n o p q r s t rest]))
331334

332335
(defprotocol ICloneable
333-
(^clj -clone [value]))
336+
"Protocol for cloning a value."
337+
(^clj -clone [value]
338+
"Creates a clone of value."))
334339

335340
(defprotocol ICounted
336-
(^number -count [coll] "constant time count"))
341+
"Protocol for adding the ability to count a collection in constant time."
342+
(^number -count [coll]
343+
"Calculates the count of coll in constant time. Used by cljs.core/count."))
337344

338345
(defprotocol IEmptyableCollection
339-
(-empty [coll]))
346+
"Protocol for creating an empty collection."
347+
(-empty [coll]
348+
"Returns an empty collection of the same category as coll. Used
349+
by cljs.core/count."))
340350

341351
(defprotocol ICollection
342-
(^clj -conj [coll o]))
352+
"Protocol for adding to a collection."
353+
(^clj -conj [coll o]
354+
"Returns a new collection of coll with o added to it. The new item
355+
should be added to the most efficient place, e.g.
356+
(conj [1 2 3 4] 5) => [1 2 3 4 5]
357+
(conj '(2 3 4 5) 1) => '(1 2 3 4 5)"))
343358

344359
#_(defprotocol IOrdinal
345360
(-index [coll]))
346361

347362
(defprotocol IIndexed
348-
(-nth [coll n] [coll n not-found]))
363+
"Protocol for collections to provide idexed-based access to their items."
364+
(-nth [coll n] [coll n not-found]
365+
"Returns the value at the index n in the collection coll.
366+
Returns not-found if index n is out of bounds and not-found is supplied."))
349367

350-
(defprotocol ASeq)
368+
(defprotocol ASeq
369+
"Marker protocol indicating an array sequence.")
351370

352371
(defprotocol ISeq
353-
(-first [coll])
354-
(^clj -rest [coll]))
372+
"Protocol for collections to provide access to their items as sequences."
373+
(-first [coll]
374+
"Returns the first item in the collection coll. Used by cljs.core/first.")
375+
(^clj -rest [coll]
376+
"Returns a new collection of coll without the first item. It should
377+
always return a seq, e.g.
378+
(rest []) => ()
379+
(rest nil) => ()"))
355380

356381
(defprotocol INext
357-
(^clj-or-nil -next [coll]))
382+
"Protocol for accessing the next items of a collection."
383+
(^clj-or-nil -next [coll]
384+
"Returns a new collection of coll without the first item. In contract to
385+
rest, it should return nil if there are no more items, e.g.
386+
(next []) => nil
387+
(next nil) => nil"))
358388

359389
(defprotocol ILookup
360-
(-lookup [o k] [o k not-found]))
390+
"Protocol for looking up a value in a data structure."
391+
(-lookup [o k] [o k not-found]
392+
"Use k to look up a value in o. If not-found is supplied and k is not
393+
a valid value that can be used for look up, not-found is returned."))
361394

362395
(defprotocol IAssociative
363-
(^boolean -contains-key? [coll k])
396+
"Protocol for adding associativity to collections."
397+
(^boolean -contains-key? [coll k]
398+
"Returns true if k is a key in coll.")
364399
#_(-entry-at [coll k])
365-
(^clj -assoc [coll k v]))
400+
(^clj -assoc [coll k v]
401+
"Returns a new collection of coll with a mapping from key k to
402+
value v added to it."))
366403

367404
(defprotocol IMap
405+
"Protocol for adding mapping functionality to collections."
368406
#_(-assoc-ex [coll k v])
369-
(^clj -dissoc [coll k]))
407+
(^clj -dissoc [coll k]
408+
"Returns a new collection of coll without the mapping for key k."))
370409

371410
(defprotocol IMapEntry
372-
(-key [coll])
373-
(-val [coll]))
411+
"Protocol for examining a map entry."
412+
(-key [coll]
413+
"Returns the key of the map entry.")
414+
(-val [coll]
415+
"Returns the value of the map entry."))
374416

375417
(defprotocol ISet
376-
(^clj -disjoin [coll v]))
418+
"Protocol for adding set functionality to a collection."
419+
(^clj -disjoin [coll v]
420+
"Returns a new collection of coll that does not contain v."))
377421

378422
(defprotocol IStack
379-
(-peek [coll])
380-
(^clj -pop [coll]))
423+
"Protocol for collections to provide access to their items as stacks. The top
424+
of the stack should be accessed in the most efficient way for the different
425+
data structures."
426+
(-peek [coll]
427+
"Returns the item from the top of the stack. Is used by cljs.core/peek.")
428+
(^clj -pop [coll]
429+
"Returns a new stack without the item on top of the stack. Is used
430+
by cljs.core/pop."))
381431

382432
(defprotocol IVector
383-
(^clj -assoc-n [coll n val]))
433+
"Protocol for adding vector functionality to collections."
434+
(^clj -assoc-n [coll n val]
435+
"Returns a new vector with value val added at position n."))
384436

385437
(defprotocol IDeref
386-
(-deref [o]))
438+
"Protocol for adding dereference functionality to a reference."
439+
(-deref [o]
440+
"Returns the value of the reference o."))
387441

388442
(defprotocol IDerefWithTimeout
389443
(-deref-with-timeout [o msec timeout-val]))
390444

391445
(defprotocol IMeta
392-
(^clj-or-nil -meta [o]))
446+
"Protocol for accessing the metadata of an object."
447+
(^clj-or-nil -meta [o]
448+
"Returns the metadata of object o."))
393449

394450
(defprotocol IWithMeta
395-
(^clj -with-meta [o meta]))
451+
"Protocol for adding metadata to an object."
452+
(^clj -with-meta [o meta]
453+
"Returns a new object with value of o and metadata meta added to it."))
396454

397455
(defprotocol IReduce
398-
(-reduce [coll f] [coll f start]))
456+
"Protocol for seq types that can reduce themselves.
457+
Called by cljs.core/reduce."
458+
(-reduce [coll f] [coll f start]
459+
"f should be a function of 2 arguments. If start is not supplied,
460+
returns the result of applying f to the first 2 items in coll, then
461+
applying f to that result and the 3rd item, etc."))
399462

400463
(defprotocol IKVReduce
401-
(-kv-reduce [coll f init]))
464+
"Protocol for associative types that can reduce themselves
465+
via a function of key and val. Called by cljs.core/reduce-kv."
466+
(-kv-reduce [coll f init]
467+
"Reduces an associative collection and returns the result. f should be
468+
a function that takes three arguments."))
402469

403470
(defprotocol IEquiv
404-
(^boolean -equiv [o other]))
471+
"Protocol for adding value comparison functionality to a type."
472+
(^boolean -equiv [o other]
473+
"Returns true if o and other are equal, false otherwise."))
405474

406475
(defprotocol IHash
407-
(-hash [o]))
476+
"Protocol for adding hashing functionality to a type."
477+
(-hash [o]
478+
"Returns the hash code of o."))
408479

409480
(defprotocol ISeqable
410-
(^clj-or-nil -seq [o]))
481+
"Protocol for adding the ability to a type to be transformed into a sequence."
482+
(^clj-or-nil -seq [o]
483+
"Returns a seq of o, or nil if o is empty."))
411484

412485
(defprotocol ISequential
413486
"Marker interface indicating a persistent collection of sequential items")
@@ -419,17 +492,33 @@
419492
"Marker interface indicating a record object")
420493

421494
(defprotocol IReversible
422-
(^clj -rseq [coll]))
495+
"Protocol for reversing a seq."
496+
(^clj -rseq [coll]
497+
"Returns a seq of the items in coll in reversed order."))
423498

424499
(defprotocol ISorted
425-
(^clj -sorted-seq [coll ascending?])
426-
(^clj -sorted-seq-from [coll k ascending?])
427-
(-entry-key [coll entry])
428-
(-comparator [coll]))
500+
"Protocol for a collection which can represent their items
501+
in a sorted manner. "
502+
(^clj -sorted-seq [coll ascending?]
503+
"Returns a sorted seq from coll in either ascending or descending order.")
504+
(^clj -sorted-seq-from [coll k ascending?]
505+
"Returns a sorted seq from coll in either ascending or descending order.
506+
If ascending is true, the result should contain all items which are > or >=
507+
than k. If ascending is false, the result should contain all items which
508+
are < or <= than k, e.g.
509+
(-sorted-seq-from (sorted-set 1 2 3 4 5) 3 true) => (3 4 5)
510+
(-sorted-seq-from (sorted-set 1 2 3 4 5) 3 false) => (3 2 1)")
511+
(-entry-key [coll entry]
512+
"Returns the key for entry.")
513+
(-comparator [coll]
514+
"Returns the comparator for coll."))
429515

430516
(defprotocol IWriter
431-
(-write [writer s])
432-
(-flush [writer]))
517+
"Protocol for writing. Currently only implemented by StringBufferWriter."
518+
(-write [writer s]
519+
"Writes s with writer and returns the result.")
520+
(-flush [writer]
521+
"Flush writer."))
433522

434523
(defprotocol IPrintWithWriter
435524
"The old IPrintable protocol's implementation consisted of building a giant
@@ -440,63 +529,109 @@
440529
(-pr-writer [o writer opts]))
441530

442531
(defprotocol IPending
443-
(^boolean -realized? [d]))
532+
"Protocol for types which can have a deferred realization. Currently only
533+
implemented by Delay."
534+
(^boolean -realized? [d]
535+
"Returns true if a value for d has been produced, false otherwise."))
444536

445537
(defprotocol IWatchable
446-
(-notify-watches [this oldval newval])
447-
(-add-watch [this key f])
448-
(-remove-watch [this key]))
538+
"Protocol for types that can be watched. Currently only implemented by Atom."
539+
(-notify-watches [this oldval newval]
540+
"Calls all watchers with this, oldval and newval.")
541+
(-add-watch [this key f]
542+
"Adds a watcher function f to this. Keys must be unique per reference,
543+
and can be used to remove the watch with -remove-watch.")
544+
(-remove-watch [this key]
545+
"Removes watcher that corresponds to key from this."))
449546

450547
(defprotocol IEditableCollection
451-
(^clj -as-transient [coll]))
548+
"Protocol for collections which can transformed to transients."
549+
(^clj -as-transient [coll]
550+
"Returns a new, transient version of the collection, in constant time."))
452551

453552
(defprotocol ITransientCollection
454-
(^clj -conj! [tcoll val])
455-
(^clj -persistent! [tcoll]))
553+
"Protocol for adding basic functionality to transient collections."
554+
(^clj -conj! [tcoll val]
555+
"Adds value val to tcoll and returns tcoll.")
556+
(^clj -persistent! [tcoll]
557+
"Creates a persistent data structure from tcoll and returns it."))
456558

457559
(defprotocol ITransientAssociative
458-
(^clj -assoc! [tcoll key val]))
560+
"Protocol for adding associativity to transient collections."
561+
(^clj -assoc! [tcoll key val]
562+
"Returns a new transient collection of tcoll with a mapping from key to
563+
val added to it."))
459564

460565
(defprotocol ITransientMap
461-
(^clj -dissoc! [tcoll key]))
566+
"Protocol for adding mapping functionality to transient collections."
567+
(^clj -dissoc! [tcoll key]
568+
"Returns a new transient collection of tcoll without the mapping for key."))
462569

463570
(defprotocol ITransientVector
464-
(^clj -assoc-n! [tcoll n val])
465-
(^clj -pop! [tcoll]))
571+
"Protocol for adding vector functionality to transient collections."
572+
(^clj -assoc-n! [tcoll n val]
573+
"Returns tcoll with value val added at position n.")
574+
(^clj -pop! [tcoll]
575+
"Returns tcoll with the last item removed from it."))
466576

467577
(defprotocol ITransientSet
468-
(^clj -disjoin! [tcoll v]))
578+
"Protocol for adding set functionality to a transient collection."
579+
(^clj -disjoin! [tcoll v]
580+
"Returns tcoll without v."))
469581

470582
(defprotocol IComparable
471-
(^number -compare [x y]))
583+
"Protocol for values that can be compared."
584+
(^number -compare [x y]
585+
"Returns a negative number, zero, or a positive number when x is logically
586+
'less than', 'equal to', or 'greater than' y."))
472587

473588
(defprotocol IChunk
474-
(-drop-first [coll]))
589+
"Protocol for accessing the items of a chunk."
590+
(-drop-first [coll]
591+
"Return a new chunk of coll with the first item removed."))
475592

476593
(defprotocol IChunkedSeq
477-
(-chunked-first [coll])
478-
(-chunked-rest [coll]))
594+
"Protocol for accessing a collection as sequential chunks."
595+
(-chunked-first [coll]
596+
"Returns the first chunk in coll.")
597+
(-chunked-rest [coll]
598+
"Return a new collection of coll with the first chunk removed."))
479599

480600
(defprotocol IChunkedNext
481-
(-chunked-next [coll]))
601+
"Protocol for accessing the chunks of a collection."
602+
(-chunked-next [coll]
603+
"Returns a new collection of coll without the first chunk."))
482604

483605
(defprotocol INamed
484-
(^string -name [x])
485-
(^string -namespace [x]))
606+
"Protocol for adding a name."
607+
(^string -name [x]
608+
"Returns the name String of x.")
609+
(^string -namespace [x]
610+
"Returns the namespace String of x."))
486611

487-
(defprotocol IAtom)
612+
(defprotocol IAtom
613+
"Marker protocol indicating an atom.")
488614

489615
(defprotocol IReset
490-
(-reset! [o new-value]))
616+
"Protocol for adding resetting functionality."
617+
(-reset! [o new-value]
618+
"Sets the value of o to new-value."))
491619

492620
(defprotocol ISwap
493-
(-swap! [o f] [o f a] [o f a b] [o f a b xs]))
621+
"Protocol for adding swapping functionality."
622+
(-swap! [o f] [o f a] [o f a b] [o f a b xs]
623+
"Swaps the value of o to be (apply f current-value-of-atom args)."))
494624

495625
(defprotocol IVolatile
496-
(-vreset! [o new-value]))
626+
"Protocol for adding volatile functionality."
627+
(-vreset! [o new-value]
628+
"Sets the value of volatile o to new-value without regard for the
629+
current value. Returns new-value."))
497630

498631
(defprotocol IIterable
499-
(-iterator [coll]))
632+
"Protocol for iterating over a collection."
633+
(-iterator [coll]
634+
"Returns an iterator for coll."))
500635

501636
;; Printing support
502637

0 commit comments

Comments
 (0)