Created
August 11, 2013 12:23
-
-
Save agumonkey/6204654 to your computer and use it in GitHub Desktop.
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
(defvar versions '("2.2" "2.0.4.1" "3.4.5.1" "4.3.2" "0.4.1-b" "2.2.3" "4.2.3" "1.0.4" "0.1a" "1.9.3" "3.1.1" "2.5.5" "2.2.6")) | |
(defun parse-version (v) | |
(mapcar #'string-to-int (split-string (replace-regexp-in-string "[^[:digit:]]" " " v) " "))) | |
(defun take-until (l p) | |
(if (or (null l) | |
(funcall p (car l))) | |
'() | |
(cons (car l) (take-until (cdr l) p)))) | |
(take-until '(1 2) (lambda (e) (= e 0))) | |
(take-until '(1 2 0 0 0 1) (lambda (e) (= e 0))) | |
(mapcar (lambda (l) (take-until l (lambda (e) (= e 0)))) (mapcar #'parse-version versions)) | |
;;; ((2 2) (2) (3 4 5 1) (4 3 2) nil (2 2 3) (4 2 3) (1) nil (1 9 3) (3 1 1) (2 5 5) ...) | |
;;; wrong, (2) should be (2 0 4 1), need a reverse predicate | |
;;; filtering, similar to subsets | |
;;; formatting tests | |
(mapcar (lambda (p) (mapconcat #'int-to-string p "")) (mapcar #'parse-version versions)) | |
(mapcar #'ints-to-string (mapcar #'parse-version versions)) | |
;;; using dash.el thread macro | |
(--> versions | |
(mapcar #'parse-version it) | |
(mapcar (lambda (p) (mapconcat #'int-to-string p "")) it)) | |
;;; wishing for a list monadic --> | |
;;; threading [A] -> [B] | |
;;; [Int] -> [Str] | |
(defun ints-to-string (ints) (mapconcat #'int-to-string ints "")) | |
;;; [[Str]] -> [[Int]] -> [Str] | |
(--> versions | |
#'parse-version | |
#'ints-to-string) | |
;;; sicp.subsets inspired fn | |
(defun trim-right (l p) | |
(if (null l) | |
'() | |
(let ((sub (trim-right (cdr l) p))) | |
(append (if (funcall p (car l)) '() (list (car l))) sub)))) | |
(trim-right '(1 2 3 4 0 0 0) (lambda (e) (= e 0))) | |
;;; (1 2 3 4) | |
;;; right recursion | |
;; stub | |
;; (defun rr (l comb) | |
;; (if (null l) | |
;; '() | |
;; (let ((r <rec>)) | |
;; (<comb> (car l) r)))) | |
(defun rr (l comb) | |
(if (null l) | |
'() | |
(let ((r (rr (cdr l) comb))) | |
(funcall comb (car l) r)))) | |
(defun rr/trim-right (l p) | |
(rr l (lambda (e r) (append (if (funcall p e) '() (list e)) r)))) | |
(rr/trim-right '(1 2 3 4 0 0 0) (lambda (e) (= e 0))) | |
;;; (1 2 3 4) ; it actually works.. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment