Create a gist now

Instantly share code, notes, and snippets.

@samdvr /with.ex Secret
Created Aug 6, 2017

What would you like to do?
with example
defmodule RetweetCalculator do
def calculate_total_retweets(user_id) do
with {:ok, handle} <- twitter_handle_for(user_id),
{:ok, tweets} <- all_tweets_for(handle),
do: { :ok, sum_retweets_for(tweets) }
end
defp twitter_handle_for(user_id) do
# implementation
{:ok, handle}
end
defp all_tweets_for(user) do
# implementation
{:ok, []}
end
defp sum_retweets_for(tweets) do
tweets
|> Enum.map(fn(tweet) -> tweet.retweets end) # => [2,3,5,7]
|> Enum.sum # => 17
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment