Skip to content

Instantly share code, notes, and snippets.

@gdeer81
Created June 19, 2015 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdeer81/6848b0a8cd5505804063 to your computer and use it in GitHub Desktop.
Save gdeer81/6848b0a8cd5505804063 to your computer and use it in GitHub Desktop.
destructuring options
;;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