-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 853de7b
Showing
4 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
*.fasl | ||
*.dx32fsl | ||
*.dx64fsl | ||
*.lx32fsl | ||
*.lx64fsl | ||
*.x86f | ||
*~ | ||
.#* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
* =cl-oju= | ||
|
||
A few Clojure idioms I missed from Common Lisp. | ||
|
||
Probably there are better libraries than this you should use. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
(defsystem "cl-oju" | ||
:version "0.0.1" | ||
:author "John Jacobsen" | ||
:license "TBD" | ||
:depends-on ("trivialtests") | ||
:components ((:module "src" | ||
:components | ||
((:file "main")))) | ||
:description "Some Clojure-ish thingies") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
(defpackage cl-oju | ||
(:use :cl :trivialtests) | ||
(:export :slurp | ||
:spit | ||
:take | ||
:drop | ||
:frequencies | ||
:interpose | ||
:partition-all | ||
:rand-nth | ||
:rand-int | ||
:range | ||
:repeatedly)) | ||
(in-package :cl-oju) | ||
|
||
(defun slurp (infile) | ||
(with-open-file (instream infile :direction :input :if-does-not-exist nil) | ||
(when instream | ||
(let ((string (make-string (file-length instream)))) | ||
(read-sequence string instream) | ||
string)))) | ||
|
||
(defun spit (name s) | ||
(with-open-file (stream name | ||
:direction :output | ||
:if-exists :supersede) | ||
(write-string s stream))) | ||
|
||
(defun range (n) | ||
(loop for x upto (1- n) collect x)) | ||
|
||
(defun take (n l) | ||
(loop for x in l repeat n collect x)) | ||
|
||
(defun drop (n l) | ||
(nthcdr n l)) | ||
|
||
(dotests | ||
(test= (drop 3 (range 10)) | ||
'(3 4 5 6 7 8 9))) | ||
|
||
(defun rand-nth (l) | ||
(nth (random (length l)) l)) | ||
|
||
(defun interpose (sep coll) | ||
(cdr (loop for x in coll append (list sep x)))) | ||
|
||
(dotests | ||
(test= (interpose :sep nil) nil) | ||
(test= (interpose :sep '(1)) '(1)) | ||
(test= (interpose :sep '(1 2 3)) '(1 :SEP 2 :SEP 3))) | ||
|
||
(defun partition-all (cell-size step-size lst) | ||
(loop for cell on lst | ||
by #'(lambda (lst1) (nthcdr step-size lst1)) | ||
collecting (take cell-size cell))) | ||
|
||
(dotests | ||
(test= (partition-all 2 2 '(a b c d)) | ||
'((a b) (c d))) | ||
(test= (partition-all 2 1 '(a b c d)) | ||
'((a b) (b c) (c d) (d)))) | ||
|
||
;; Adapted from https://codereview.stackexchange.com/a/223128: | ||
(defun frequencies (lst) | ||
(let ((m (make-hash-table :test #'equalp))) | ||
(loop for e in lst | ||
do (incf (gethash e m 0))) | ||
(loop for k being the hash-key of m | ||
using (hash-value v) | ||
collect (list k v)))) | ||
|
||
(defun rand-int (n) (random n)) | ||
|
||
(defun repeatedly (n f) | ||
(loop repeat n collect (funcall f))) |