Skip to content

Commit

Permalink
added a few more combinators and and an example
Browse files Browse the repository at this point in the history
  • Loading branch information
loskool committed Jul 11, 2020
1 parent 9882e08 commit 234a192
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
31 changes: 30 additions & 1 deletion examples.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,33 @@ vector VEC, one at a time."
(zip! (range) (file-lines file))))


;; find all export expressions in my bashrc file

;;; Silly Scrambler ;;;


(defun pad (str len &optional (pad-char #\Space]))
(let ((i 0))
(with-output-to-string (out)
(loop :for c :across str :do (incf i) (princ c out))
(loop :while (< i len) :do (incf i) (princ pad-char out)))))

(defun scramble (n str)
(assert (< n (length str)))
(let ((str (pad str (* n (ceiling (/ (length str) n))))))
(concatenate 'string
(apply #'nconc
(mapcar #'collect
(disperse! n (seq str)))))))

(defun chunk (n str)
(assert (zerop (mod (length str) n)))
(let ((size (/ (length str) n)))
(loop
:for i :below (length str) :by size
:collect (subseq str i (+ i size)) )))

(defun descramble (n str)
(concatenate 'string
(collect
(apply #'intersperse!
(mapcar #'seq (chunk n str))))))
20 changes: 20 additions & 0 deletions gtwiwtg.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,26 @@ Caveat:
(list (filter! pred gen1)
(filter! (complement pred) gen2))))

(defun intersperse! (gen1 gen2 &rest gens)
(inflate! #'seq (apply #'zip! gen1 gen2 gens)))

(defun truncate! (n gen)
(map! #'first (zip! gen (times n))))

(defun inject! (fn gen)
(map! (lambda (x) (funcall fn x) x) gen))

(defun disperse! (n gen)
(loop
:for i :below n
:for cloned :in (nfurcate! n gen)
:collect
(let ((j i))
(map! #'second
(filter! (lambda (pair) (= j (mod (first pair) n)))
(indexed! cloned))))))


;;; CONSUMERS

(defmacro for (var-exp gen &body body)
Expand Down

0 comments on commit 234a192

Please sign in to comment.