Skip to content

Instantly share code, notes, and snippets.

@Dykam
Created January 30, 2011 23:03
Show Gist options
  • Select an option

  • Save Dykam/803378 to your computer and use it in GitHub Desktop.

Select an option

Save Dykam/803378 to your computer and use it in GitHub Desktop.
# get all tweets and the number of likes, which is from another source
data
from tweets, as tweet
zip facebook.likes, as likes
# is transformed to
from(tweets, (data, tweet) -> data.tweet = tweet)
.zip(facebook.likes, (data, likes) -> data.likes = likes)
# get all direct messages and bundle the senders last 10 tweets with it
from directMessages, as dm
combine tweets, as tweet
where tweet.sender is dm.sender
limitBy 10, tweet.sender
# is transformed to
from(directMessages, (data, dm) -> data.dm = dm)
.combine(tweets, (data, tweet) -> data.tweet = tweet)
.where((data) -> data.tweet.sender is data.dm.sender)
.limitBy((data) -> 10, (data) -> data.tweet.sender)
# get all tweets and facebook posts from John
from twitter.user("John").tweets, as tweet
concat facebook.user("John").posts, as post
orderBy post.date
# is transformed to
from(twitter.user("John").tweets, (data, tweet) -> data.tweet = tweet)
concat(facebook.user("John").posts, (data, fbpost) -> data.post = post)
orderBy((data) -> data.post.date)
# Prefixing lines with line numbers:
for line, linenumber in file.read "original"
file.write "modified", "#{linenumber}: #{line}"
# Prefixing lines with line numbers:
linesAndNumbers = for line, linenumber in file.read "original"
yield "#{linenumber}: #{line}"
for lineAndNumber in linesAndNumbers
console.log lineAndNumber
# do some funky work, file "original" is being modified
# run the code again.
for lineAndNumber in linesAndNumbers
console.log lineAndNumber
# from `chain; func`
chain
foo
# to
{}.foo()
# from `func expr`
foo bar
# to
source.foo((data) -> bar)
# from `func local_identifier`
foo bar
# to
source.foo((data) -> data.bar)
# from `as bar`
foo as bar
# to
source.foo((data, bar) -> data.bar = bar)
# get all tweets which are replies to John's tweets
from tweets, as tweet
where tweet.replyOn.user is "John"
# is transformed into
from(tweets, (data, tweet) -> data.tweet = tweet)
.where((data) -> data.tweet.replyOn.user is "John")
# get the most retweeted tweet in the last 24 hours
from tweets, as tweet
where (date.now - tweet.date).hours < 24
fold (highest) -> if highest.retweets < tweet.retweets then tweet else highest
# is transformed into
from(tweets, (data, tweet) -> data.tweet = tweet)
.where((data) -> (date.now - data.tweet.date).hours < 24)
.fold((data, highest) -> if highest.retweets < data.tweet.retweets then data.tweet else highest)
fixIdentifiers = (expression, identifiers) ->
transformCoffee = (chain) ->
data = []
transformed = for expression in chain.split '\n'
[func, args...] = expression.split ' '
transformedArgs = for arg in args
arg = arg.replace(/^\s+|\s+$/g, "")
if [_, identifier] = "as foo".match(/^as (.*)/)
data.push identifier
"(data, #{identifier}) -> data.#{identifier} = #{identifier}"
else
fixIdentifiers arg, data
["{}", transformed...]
chain = (expression) ->
coffee = transformCoffee expression
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment