Created
June 19, 2015 20:15
-
-
Save gdeer81/6848b0a8cd5505804063 to your computer and use it in GitHub Desktop.
destructuring options
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
;;given a url of this structure <base>/Publish<type>?id=<id>&update=<1 or 0> | |
(def content-url "http://www.somesite.com/PublishContent?id=42&update=1") | |
(def article-url "http://www.somesite.com/PublishArticle?id=42&update=1") | |
(let [[_ type] (re-find #"Publish([Content Article]*)" content-url) | |
id (last (re-find #"(id=)([a-zA-Z0-9]*)" content-url)) | |
update? (last (re-find #"(update=)(\d)" content-url))] | |
{:type type | |
:id id | |
:update? update?}) | |
;;output {:type Content :id 42 :update? 1} | |
(let [[_ type id update?] (re-find #"Publish([Content Article]*)\?id=([a-zA-Z0-9]*)\&update=(\d)" article-url)] | |
{:type type | |
:id id | |
:update? update?}) | |
;;output {:type Article :id 42 :update? 1} | |
;;DISCUSSION | |
;;In this case I have one string that I'm operating on | |
;;in the content.type example I used the vector destructuring to pull out the one part that I wanted | |
;;in the content.id and update? part I used last of the return value | |
;;in the article example I just wrote one regex and destructured it in one shot | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment