[Applause]
all right good afternoon everybody I
hope everyone's having a great time at
Gia's computer pest
my name is Adam DC I'm Adam DC dev on
Twitter and before I was a software
engineer I did attend school for music
specifically music composition and I was
always drawn to the analytical thinking
aspect of music theory and perhaps
surprising nobody I had a tough time
finding a job in music and so I went
into programming which I had some
experience with and years later when I
started to learn a functional
programming I found that the same itch
was scratched regarding the analytical
thinking so when I started to try to get
back into music as a hobby I decided to
use functional programming so here's my
introduction to programming my
introduction to functional programming
from a musical perspective so first we
must ask the question what is functional
programming my answer is its programming
with functions just like object-oriented
programs are oriented around objects
functions are the building block of
functional programming but that
naturally leads to the next question
what is a function a non functional
programming definition might be
something like a sequence of commands
abstract into a reusable unit but a more
functional definition is a relationship
between a set of inputs and outputs and
this is sometimes referred to as a pure
function and it comes from the
mathematical definition of a function
and these so-called pure functions are
the building block of functional
programming so what makes a function
pure well there's two main aspects given
the same arguments in it will return the
same value that means it shouldn't rely
on anything outside of itself such as a
random number generator or the current
time or
read from the database it also means it
shouldn't affect anything outside of its
return value such as manipulating the
Dom or writing to a database and
together this makes the function
referentially transparent this means
that a function call with the same
parameters can be replaced by its return
value so here we are defining middle c
with a name and an octave and increment
- octave here is a pure function it
takes a note and it returns a new note
with the octave incremented by one and I
see here is the result of that pure
function playing here is a function that
is an impure function because it has a
side-effect
it sends music to the browser you can
pass an either high C or increment
octave middle C and I'll have the same
effect because of the referential
transparency of the increment octave
function so we decided to learn
functional programming remove all side
effects and now your program is useless
it's important to note that side effects
are not bad HTTP requests Dom
manipulation or playing music are all
side effects they are not bad but they
are unpredictable and they are harder to
test so you so you need to use your
impure functions carefully and
predictably so now that we know what
functional programming is and what a
function is what are some tools one of
the main ones is immutability and that's
the idea that a variable state cannot be
modified after creation and you might be
thinking why why would you like to tie
one arm behind your back
but there is a reason for this there are
some advantages of immutability such as
it's less for your brain to track humans
have a working memory it's the
biological equivalent to RAM and if your
brain has to keep track of what state a
current variable is in
it's more time that your brain is
working on that and not spending on the
rest of your program
it also immutability prevents
unintentional side-effects you
accidentally changing a variable in a
function can be a really hard bug to
track down and it preserves state I like
to think of it as your bank account
balance when you sign into your bank
account you don't just see your single
balance you see your current balance
which is derived from the starting
balance and then the withdrawals and
deposits another technique of functional
programming is higher-order functions
and that's a function that either
accepts an argument in a function as an
argument or returns a function you might
be thinking this is something along the
lines of but here's an example that
might help here tone j/s is a framework
that's built on top of the Web Audio API
and playing out here is a higher order
function it accepts a note and it
returns a function play middle C here is
the result of that that function being
called with C for add eventlistener here
accepts a function as an argument so add
eventlistener and play note are both
higher-order play note because it
returns a function and add eventlistener
because it accepts a function now
manipulating arrays is an important part
of all paradigms and functional
programming has a lot of really great
tools for dealing with arrays and
there's two concepts that we already
cover that are important the
higher-order functions because each of
these methods accept a function and
immutability because each of these
methods that I'm mentioning will return
a new value rather than manipulating the
current one so array that filter it
accepts a function that's run against
every element that function receives the
element the index and the array and it
returns a subset of the original array
so we define a melody and we define a
function is high which takes a note and
returns a boolean if that value is equal
to or above middle C it is considered
high otherwise it is considered low so
high notes is the melody filtered to is
high and we get only the elements that
return a truth eval you so here we have
our unfiltered melody and when we filter
to just the high notes a regular map
accepts a function that's run against
every element as well it also accepts
the element index and array within that
flat function but rather than returning
a subset of the original array it
returns an array of the same length with
new values so we have our melody here
and we have our increment octave
function and then we map our melody to
increment Optive and now we have the a
copy of the array but with each note
incremented by one octave or the high
melody
and the last one I'll cover today is a
radar reduce and a radar reduced also
accepts a function that's run against
every element but in addition to the
accumulator I in addition to the element
index and array it also accepts an
accumulator value and it will return the
total accumulated value after passing
through every element and in some
languages this is known as fold and
accumulate and it is a more generic tool
than filter or map I often hear that
reduce takes an array and returns a
single value and this is true but I
don't consider it to be a terribly
useful distinction because all functions
in JavaScript return a single value so I
like to think of it as a generic utility
function that can return any type
whether it be another array in fact you
can write filter or map using a reduce
or it can return a single string or a
single integer so I find the best way to
learn reduce is really by by example
so here we are defining this melody with
note names and length then we have our
two length function that is going to be
passed to reduce total is the
accumulator and note is the array
element and then the function adds the
total to the value of the notes length
so length are a reduced accepts of
function and an initial value and then
the return value is the end accumulation
so starting with the initial value of 0
we go through and each notes length
that's added to the total so 0 plus
0.125 0.125 plus 0.125 is 0.25 0.375
until
we get to the total length of one point
six to five seconds so here with our
melody we get one point six two five
seconds all right now that we're done
with arrays partial application is an
incredibly useful tool for writing
reusable and generic functions and it is
technique to preset or fix some of the
arguments of a function so here we have
a transpose function it takes a
direction either up or down in interval
such as a major third or a minor seventh
and a note which will be transposed a
major third is a function that is a
partially applied transpose we set up
and we set major third but we wait to
receive the note so here when passing in
C for AB major third will return an e
for or a B flat two will return a d3 and
there is a way in stage one of the tc39
there's a there is a syntax for native
partial application that will let you to
partially apply partially apply
arguments to a function by using the
question mark for what you don't want to
apply so down minor seventh can be a
partially pi transpose by leaving the
note as a question mark so here we can
map these partially applied functions to
an array of notes and here the
transposition
or up a major third or transpose down a
minor seventh
[Music]
so partial application is a great tool
for writing reusable functions that are
partially applied but the real bread and
butter of functional programming is
composition and composition is a
technique to combine multiple functions
into a new function so here we're going
to take a note name and we'll return the
frequency but detuned slightly so we
have a function converting a note name
to a frequency for example an input of a
four would return a number of four three
which is the frequency in Hertz of in a
four D tune-up will be defined a
function to multiply by a small
percentage so an input of 440 Hertz
would return 444 0.4 and then compose
here takes two functions and calls them
from right to left so get detuned
frequency is a function that is created
by composing D tune-up and frequency so
an input of a four would return 444
point four and there's a pipeline
operator that's currently in stage one
of the tc39 spec as well that would
allow for easier function composition
through piping so here you can write a
get detuned frequency by piping the note
first to frequency and then to D tune-up
so here we can hear our a4
and the same note but detuned so now
putting it all together we are going to
be writing a function that converts a
human-readable shorthand into a computer
readable array so everyone able to read
that all right just kidding
so first we'll be importing some
dependencies from RAM des and ton Jas
and this might seem like a lot of
functions that we're my computer just
fell asleep sorry all right
because that means I'm about halfway
through cuz I thinks it's a 15 minute so
first we're importing some dependencies
from Ramdan tone j s and we're going to
define some functions by function
composition so here parse name takes a
shorthand note and it returns a name for
example c4 at a 10 would return a string
of see it pipes the input through two
other functions match which is which
will return an array of matching strings
that that match the regex and head which
will return just the first string so
when passing c4 at a 10 the match
function would return in the array of
the string of C and then the head
function will take that array of the
string of C and returns just the string
of C so C for add a 10 to a ray of C for
to just to just see parse octave here
works similarly it takes a short hand
note and it returns an octave so C for
at a 10 will return an array of the
string of 4 which will return the number
4 and parse duration takes a short hand
note and returns the duration it splits
at the a symbol and it takes the last
string so in this case in 8m first time
here is a little bit more complicated it
takes a note and it's context within the
array and it will return a scheduled
time so it takes the array of notes it
slices the array to only those notes
preceding that your current element it
maps each of those notes to their length
and it reduces them to a single value in
this case of bird so as all together
this lets the Web Audio API know when to
play each note by adding the the time of
all of the previous notes
and now purse shorthand is a composition
of compositions it accepts a string and
it returns an array of objects that is
able to be consumed by the Web Audio API
through the the tone j/s first we split
a tall white space this turns the the
string into an array of strings and then
array map we use two we pass it the
array of strings and that converts it to
of an object so apply spec takes an
object shape with functions and each
string in the array will be passed into
each of these functions and it will be
mapped to an object of a matching shape
so what will be returned is an object
with a name with with the value that
were takes each of those strings and
passes it to parse name for the name
parse octave for the octave purse
duration for the length and parse time
for the time so with our purse shorthand
function this input string becomes this
array of strings past our PI spec
becomes this array of note objects where
we have the name the octave the length
and the time so this string can become
this music or this string can become
this music
[Music]
so why should you care about functional
programming I like functional
programming because it forces you to
break down problems into their smallest
parts when converting a string into
music we had to think of how to parse
this shorthand in a way that a human
that the computer could read it and
there's a lot of parts there but we
didn't write very many functions we
there was only one function that was
actually written most of the functions
were defined by just piping together
very small functions like like regex
match or a or a first or a conversion
from a string to a number or an ad so it
forces you to break down problems into
their smallest parts so if you found
this interesting and would like to go
further in functional programming
there's some really great resources
professor frisbees mostly advocate guide
to functional programming it is
definitely a more than adequate guide is
available for free online Professor
frisbee is known as doctor boolean on
Twitter he's got some great great video
resources as well also I would highly
suggest looking into a functional
utility library in in this case I used
random lodash FP is a branch of lodash
that has a lot of really great utility
functions that have a functional
programming perspective and lambda cast
is a really great podcast for going over
the fundamentals of functional
programming much more in depth thank you
so much for your time if you have any
questions feel free to catch me
afterwards or message me at Adam DC dev
on Twitter thank you
take mine
[Applause]