Skip to content

Commit

Permalink
בְּרֵאשִׁ֖ית
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenhombre committed Sep 18, 2019
0 parents commit 853de7b
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.fasl
*.dx32fsl
*.dx64fsl
*.lx32fsl
*.lx64fsl
*.x86f
*~
.#*
6 changes: 6 additions & 0 deletions README.org
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.

9 changes: 9 additions & 0 deletions cl-oju.asd
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")
76 changes: 76 additions & 0 deletions src/main.lisp
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)))

0 comments on commit 853de7b

Please sign in to comment.