Skip to content

Instantly share code, notes, and snippets.

@samdvr

samdvr/with.ex Secret

Created August 6, 2017 17:42
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 samdvr/4e140780ffc23b6740696d5864718e81 to your computer and use it in GitHub Desktop.
Save samdvr/4e140780ffc23b6740696d5864718e81 to your computer and use it in GitHub Desktop.
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