Skip to content

Instantly share code, notes, and snippets.

@radex
Created March 23, 2013 09:53
Show Gist options
  • Save radex/5227166 to your computer and use it in GitHub Desktop.
Save radex/5227166 to your computer and use it in GitHub Desktop.
Playing around with CoffeeScript's comprehensions and inline conditions some more… Again, it's not the most readable and practical way to implement it, but it's fun to test the edges of what the language can do
text = """Messiah (HWV 56) is an English-language oratorio composed in 1741 by George Frideric Handel, with a scriptural text compiled by Charles Jennens from the King James Bible and the Book of Common Prayer. Messiah was first performed in Dublin in 1742, and received its London premiere the following year. After an initially modest public reception, the oratorio gained in popularity, becoming one of the most frequently performed choral works in Western music. Although its structure resembles that of opera, it is not in dramatic form, but a reflection on Jesus Christ as Messiah. Handel begins Part I with prophecies by Isaiah and others, and moves to the annunciation to the shepherds, the only "scene" taken from the Gospels. In Part II he concentrates on the Passion and ends with the Hallelujah Chorus. In Part III he covers the resurrection of the dead and Christ's glorification in heaven. Handel wrote Messiah for modest vocal and instrumental forces; after his death, the work was adapted for performance with much larger orchestras and choirs. Its orchestration was revised and amplified by (among others) Mozart. Since the late 20th century, the trend has been towards authenticity. (Full article...)"""
FindWords = do ->
alphabet = 'abcdefghijklmnoqprstuvwxyzABCDEFGHIJKLMNOQPRSTUVWXYZ '
text2 = ''
text2 += text[i] for i in [0...text.length] when text[i] in alphabet
words = (word for word in text2.split(' ') when word not in ['', ' '])
(options = {}) ->
{min, max, startsUp, startsDown, allUp} = options
(word for word in words when \
(if min? then word.length >= min else true) and
(if max? then word.length <= max else true) and
(if startsUp then word.charCodeAt(0) in [65..90] else true) and
(if startsDown then word.charCodeAt(0) in [97..122] else true) and
(if allUp then [0...word.length].reduce((r, l) ->
r and word.charCodeAt(l) in [65..90]
, yes) else true))
console.log FindWords allUp: yes
console.log FindWords startsDown: yes, min: 10
console.log FindWords startsUp: yes, max: 5
console.log FindWords min: 2, max: 2
console.log FindWords min: 4, max: 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment