a25df85e26e5
Add take, fix a bug
author | Steve Losh <steve@stevelosh.com> |
---|---|
date | Mon, 08 Aug 2016 16:29:47 +0000 |
parents | e5c5cf8143a2 |
children | b1426e8648ef |
branches/tags | (none) |
files | losh.lisp package.lisp |
Changes
--- a/losh.lisp Mon Aug 08 13:39:27 2016 +0000 +++ b/losh.lisp Mon Aug 08 16:29:47 2016 +0000 @@ -136,6 +136,8 @@ plus)) + + ;;;; Functions (defun juxt (&rest fns) "Return a function that will juxtipose the results of `functions`. @@ -217,6 +219,25 @@ (collect `(zap% ,place ,function %))))) +;;;; Lists +(defun take (n list) + "Return a fresh list of the first `n` elements of `list`. + + If `list` is shorter than `n` a shorter result will be returned. + + Example: + + (take 2 '(a b c)) + => (a b) + + (take 4 '(1)) + => (1) + + " + (iterate (repeat n) + (for item :in list) + (collect item))) + ;;;; Hash Tables (defmacro gethash-or-init (key hash-table default-form) "Get `key`'s value in `hash-table`, initializing if necessary. @@ -390,6 +411,40 @@ (terminate)))))))) +;;;; Distributions +(defun prefix-sums (list) + "Return a list of the prefix sums of the numbers in `list`. + + Example: + + (prefix-sums '(10 10 10 0 1)) + => (10 20 30 30 31) + + " + (iterate + (for i :in list) + (sum i :into s) + (collect s))) + +(defun frequencies (seq &key (test 'eql)) + "Return a hash table containing the feqeuencies of the items in `seq`. + + Uses `test` for the `:test` of the hash table. + + Example: + + (frequencies '(foo foo bar)) + => {foo 2 + bar 1} + + " + (iterate + (with result = (make-hash-table :test test)) + (for i :in-whatever seq) + (incf (gethash i result 0)) + (finally (return result)))) + + ;;;; Hash Sets (defclass hash-set () ((data :initarg :data)))