Placeholder Image

字幕列表 影片播放

  • good afternoon.

  • Everybody hope everyone's having a great time at J s.

  • Confident a pest that my name is Adam GC.

  • I'm Adam GC 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 toe learn a functional programming, I found that the same itch was scratched regarding the analytical thinking.

  • So when I started Thio try to get back into music as a hobby, I decided to use functional pro 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 only into 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?

  • The non functional programming definition might be something like a sequence of commands attracted into a reusable unit.

  • But a more functional definition is a relationship between a set of inputs and outputs, and this is sometimes refer 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 a 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 referential.

  • Be transparent.

  • This means that AH, function call with the same parameters can be replaced by its return value.

  • So here we're defining middle C with a name and an active an increment octave.

  • Here is a pure function.

  • It takes a note and it returns a new note, with the active in corrected by one in high sea.

  • Here is the result of that pure function.

  • Play here is a function that is an impure function because it has a side effect.

  • It sends music to the browser.

  • You can pass on either high C or in current active middle C, and I'll have the same effect because of the referential transparency of the incurring octave function.

  • So we decided to learn functional programming, remove all side effects and now your program is useless is important to note that side effects are not bad.

  • Http requests.

  • Don 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 a mutability, and that's the idea that a very bold state cannot be modified after creation.

  • You might be thinking, Why?

  • Why would you like to tie one arm behind your back?

  • But there is a reason for this.

  • There's some advantages of the mutability, such as it's less for your brain to track.

  • Humans have a working memory.

  • It's the biological equivalent to ran.

  • And if your brain has to keep track of what state A current variables in its Maur time that your brain is working on that and not spending on the rest of your program, it also mutability prevents unintentional side effects.

  • You accidentally changing a variable in the 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.

  • Ah, a function as an argument or returns of function.

  • You might be thinking this is something along the lines off, but he's an example that might help.

  • Here.

  • Tone Js is a framework that's built on top of the Web audio, a p I, and playing out here is a higher order function.

  • It accepts a note and it returns a function play middle.

  • See, here is the result of that that function being called with C four at event listener here except a function as an argument.

  • So at event, listener and play note are both higher order play note because the returns of function and at event listener because it accepts a function now manipulating raises an important part of all paradigms and functional programming has a lot of really great tools for dealing with a raise.

  • And there's two concepts that we already covered that are important.

  • The higher order functions because each of these methods, except a function and immune ability, because each of these methods that I'm mentioning ah will return a new value rather than manipulating the current one.

  • So a raid at Filter it accepts a function that's run against every element.

  • That function receives the element the index in the array, and it returns a subset of the original rain.

  • So we define a melody and we did find 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 e value.

  • So here we have our unfiltered reality, and when we filter to just the high notes, you read a map except the function that's run against every element as well.

  • It also accepts the element index and array within that that function.

  • But rather than returning a subset of the original ray, it returns in 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 include the active, and now we have the a copy of the array.

  • But with each note implemented by one active with a high multi and the last one I'll cover today is a raid at reduce and all right I reduced.

  • Also, accept the function that's run against every element.

  • But in addition to the accumulator, I think in the addition to the element index and array, it also accept 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 and job script or turn a single value s so I like to think of it as a generic utility function that can return any type.

  • Well, there would be another array.

  • Um, in fact, you can write, filter or map using a reduce, or it can return a single string or a single interred herb.

  • So I find the best way to learn reduce is really by ah, by example.

  • So here we're defining this melody with no 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 the reduced except of function and an initial value.

  • And then the rich value is the end accumulation.

  • So starting with the initial value of zero we go through and each notes length, it's added to the total zero pa's 00.1 to 5 0.25 plus 0.1 to 5 this 50.25 point +375 until we get to the total length of 1.6 to 5 seconds.

  • So here, with our melody way, get 1.6 to 5 seconds.

  • All right, now that we're done with a raise, partial application is an incredibly useful tool for writing reusable and generic functions, and it is sickening to preset or fix some of the arguments of a function.

  • So here we have a transposed function.

  • It takes a direction either up or down an interval such as a major third or a minor seventh and you know which will be transposed.

  • A major third is a function that is a partially applied transposed we set up, and we set major third, but we wait to receive the note.

  • So here, when passing in C four up major third will return an E four or a B flat, too will return a D three and there is a way in Stage one of the TC 39.

  • There's a, uh, there is a syntax for native partial application that will let you two personally apply.

  • Um, 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 pie transposed by leaving the note as a question mark.

  • So here we can map these partially applied functions to an array of notes and hear the transposition or up a major third, or transposed down a minor seventh.

  • So partial application is a great tool for writing reusable functions that are partially applied.

  • But the rial bread and butter ah, functional programming is composition, and composition is a technique to combine multiple functions into a new function.

  • So here we're gonna take a note name and we will return the frequency.

  • But d tuned slightly.

  • So we have a function converting a note name to a frequency, for example, in the input of a four would return a number of fourth pretty, which is the frequency and hurts of in a four de tune up will be defined a function to multiply by a small percentage, so input of 440 hurts would return 444.4 and then compose here, takes two functions and calls them from right to left so get detained.

  • Frequency is a function that is created by composing D Turn up and frequency.

  • So in input of a four would return 444.4.

  • And there's a pipeline operator that's currently in stage one of the TC 39 suspect as well.

  • That would allow for easier function composition through piping.

  • So here.

  • All right, get detained frequency by piping the note first to frequency and then to de tune up.

  • So here we can hear are a four and the same note, but d tuned.

  • So now putting it all together we're going to be writing a function that converts a human readable shorthand into a computer readable array.

  • So I've been able to read that.

  • Just kidding.

  • Um, So first we'll be importing some dependencies from Ramda and Tone Js, and this might seem like AA lot of functions that were my My computer just fell asleep.

  • Sorry.

  • All right, because that means I'm about halfway through, I think such a 15 minutes, Um So first we're importing some dependencies from ambient tone Js, 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, C four at a 10 would return a string of C.

  • It pipes that input through to other functions match, which is which will return an array of matching strings that that match the Rejects and head, which will return just the first string.

  • So when passing C four at a den, the match function would return in array of the string of C and then the head function.

  • We'll take that array of the string of seeing our turns just the string of seed.

  • So C four added in to array of C four to just to just see person active here works similarly.

  • It takes a shorthand note, and it returns an octave.

  • So C four at it n will return an array of the string of four, which will return the number four and parsed oration, takes a shorthand note and returns the duration.

  • It splits at the assemble, and it takes the last string.

  • So in this case, in eight n first time here is a little bit more complicated.

  • It takes a note and its context within the array, and it will return a scheduled time so it takes the array of notes it slices.

  • Theory a toe on Lee.

  • Those notes proceeding that your current element.

  • It maps each of those notes to their length, and it reduces them to a single value in this case.

  • And so as altogether, this lets the Web audio AP I know went to play each note by adding the time of all the previous notes.

  • And now purse shorthand is a composition of compositions.

  • It accepts a string, and it returns in array of objects that is able to be consumed by the Web body away.

  • P.

  • I threw the tone Js First we split at all white space.

  • This turns the the string into an array of strings and then a ray map.

  • We used to we pass it the array of strings and that converts it to off in 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 takes each of those strings and pass it to parse name for the name parts octave for the active part duration for the length and part time for the time.

  • So with our purse shorthand function, this input string becomes this array of strings past.

  • Apply 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.

  • 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 human that the computer could read it.

  • And there's a lot of parts there, but we didn't write.

  • Very many functions were there was only one function.

  • I was actually written.

  • Most of the functions were defined by just piping together very small functions like like rejects match or a or a first or a ah 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 and functional programming.

  • There's some really great resource.

  • Is Professor Frisbees mostly advocate guide to functional programming.

  • It is, Ah, definitely a more than adequate guide.

  • Eyes available for free online Professor Frisbee eyes known as Dr Boolean on Twitter.

  • He's got some great great video resource is as well.

  • Also, I would highly suggest looking into a functional utility library.

  • Um, in this case, I used random low dash F p is a branch of load ash 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 D C Dev on Twitter.

  • Thank you.

  • Good night.

good afternoon.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

B1 中級

創作音樂與組成函數,由亞當-吉塞|布達佩斯2019年JSConf會議。 (Composing music with composed functions by Adam Giese | JSConf Budapest 2019)

  • 1 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字