Created
August 5, 2010 21:31
-
-
Save bitemyapp/510409 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(defn ellip-2 [words] | |
(str (->> words (re-split #"\s+") (take 3) (str-join " ")) "...")) | |
; What does this do? Well, it ellipsizes sentences. it's an improvement upon the book's version. | |
; we send it strings like "Yo this is a test sentence!" and we get back "Yo this is..." | |
; legitimately useful for creating things like short descriptions to articles. | |
; you call it like | |
(ellip-2 "Yo this is a test sentence!") | |
; But how does it work? | |
(defn ellip-2 | |
; defn is how you define functions, ellip-2 is the function name | |
[words] | |
; function arguments, specifically, we're looking for strings like the test sentence above. Doesn't | |
; have to be a literal, does have to be a string though. | |
; Now we just have to unwrap this. | |
(str (->> words (re-split #"\s+") (take 3) (str-join " ")) "...")) | |
; With lisp, it's sometimes easier to work inside out, since top to bottom right to left | |
; doesn't always mean anything. | |
; there's a str call wrapped around the results of everything we're doing, it's actually what | |
; adds "..." to the final result | |
(str | |
; ->> is technically a unidirectional pipe of sorts. I call it "The Winchester" because | |
; it shoots shit that way -> | |
(->> words | |
; in that ^ case we're shooting words -> that way | |
; it's flying through | |
(re-split #"\s+") | |
; splits the string into a sequence of words like so, ("Yo" "this" "is" "a" "test" "sentence") | |
; then | |
(take 3) | |
; takes the first 3 words of the sequence which returns ("Yo" "this" "is") | |
; then | |
(str-join " ") | |
; joins the sequence into a string again, separating them with spaces, "Yo this is" | |
; and then the wrapping (str "...") call adds "..." | |
; so the function finally returns "Yo this is..." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment