Hey Jude : A functional programming tribute
You will be surprised, but Hey Jude by The Beatles can actually broken down into an algorithmic approach.
I have written a script in Elixir to generate the lyrics of Hey Jude. A partial flowchart I had for reference here.
You can grab the source code from here, but please, please, help me in improving the readbility and adhering to Elixir coding standards.
defmodule HeyJude do
@lines [[“make it bad”, “be afraid”, “let me down”], [“Take a sad song and make it better”, “You were made to go out and get her”, “You have found her, now go and get her”]] def lyrics do
[intro]
|> verse(0)
|> verse(1)
|> chorus(0)
|> verse(2)
|> chorus(1)
|> verse(3)
|> outro
|> List.flatten
|> Enum.join(“\n”)
end def intro do
“The Beatles: Hey Jude in Elixir (programming) Lyrics © SONY/ATV TUNES LLC; SONY BEATLES LTD”
end def verse(current_lyrics, verse_number) do
lines = (0..3)
|> Enum.map(&(line(verse_number, &1)))
[lines] ++ current_lyrics
|> Enum.reverse
end def line(verse_number, 2) do
case {verse_number, rem(verse_number + 1, 2)} do
{1, 0} -> “The minute you let her under your skin”
{1, _} -> “The minute you let her into your heart”
{_, 0} -> “Remember to let her under your skin”
{_, _} -> “Remember to let her into your heart”
end
end def line(verse_number, 3) do
case verse_number do
3 -> “Then you’ll”
_ -> “Then you”
end
<> case rem(verse_number, 2) do
0 -> “ can start “
_ -> “ begin “
end
<> “to make it “
<> case verse_number do
3 -> “\nBetter, “ <> String.duplicate(“better, “, 5) <> “oh”
_ -> “ better “
end
end def line(verse_number, line_number) do
temp_line = @lines
|> Enum.at(rem(line_number, 3))
|> Enum.at(rem(verse_number, 3))
case rem(line_number, 3) do
0 -> “Hey Jude, don’t “ <> temp_line
_ -> temp_line
end
end def chorus(current_lyrics, 0) do
[“And anytime you feel the pain, hey Jude, refrain”,
“Don’t carry the world upon your shoulders”,
“For well you know that it’s a fool who plays it cool”,
“By making his world a little colder”] ++ current_lyrics
|> Enum.reverse
end def chorus(current_lyrics, 1) do
[“So let it out and let it in, hey Jude, begin,”,
“You’re waiting for someone to perform with.”,
“And don’t you know that it’s just you, hey Jude, you’ll do,”,
“The movement you need is on your shoulder”] ++ current_lyrics
|> Enum.reverse
end def outro(current_lyrics) do
[String.duplicate(“Nah nah nah nah nah nah, nah nah nah, hey Jude \n”, 10)] ++ current_lyrics
|> Enum.reverse
end
endIO.puts HeyJude.lyrics
The lyrics output are as follows:
Hey Jude, don’t make it bad
Take a sad song and make it better
Remember to let her into your heart
Then you can start to make it better
Hey Jude, don’t be afraid
You were made to go out and get her
The minute you let her under your skin
Then you begin to make it better
And anytime you feel the pain, hey Jude, refrain
Don’t carry the world upon your shoulders
For well you know that it’s a fool who plays it cool
By making his world a little colder
Hey Jude, don’t let me down
You have found her, now go and get her
Remember to let her into your heart
Then you can start to make it better
So let it out and let it in, hey Jude, begin,
You’re waiting for someone to perform with.
And don’t you know that it’s just you, hey Jude, you’ll do,
The movement you need is on your shoulder.
Hey Jude, don’t make it bad
Take a sad song and make it better
Remember to let her under your skin
Then you’ll begin to make it
Better, better, better, better, better, better, oh.
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude
Nah nah nah nah nah nah, nah nah nah, hey Jude