Today's assignment we are going to look at p n junctions. This is assignment 5. In assignment 4 we looked at metal semiconductor junctions. In assignment 5 we are going to look at junctions form between p and n type. So usually the p and n are of the same material which case it is a homo junction. We have also seen hetero junctions where the junction is formed between 2 different materials. In this assignment we will focus fully on the homo junctions. We will do some calculations on the built-in potential when a junction is formed, the depletion widths and the total that is the total depletion width and also the depletion width on the p and the n side. A p n junction is essentially a diode. It is a rectifier so that it conducts current in the forward bias and does not conduct in the reverse bias. So we will also do some calculations of the forward bias current and the reverse saturation current. So some of this will be similar to what we did in assignment 4 where we looked at a short key junction which is also a rectifier. So later we can compare the properties of a p n junction and that of a short key junction. So let me go to problem number 1. We have a silicon p n junction which has an n region with 10 to the 17 donors per centimeter cube, n region so n d is 10 to the 17 per centimeter cube and there is a p region with acceptor concentration of 2 times 10 to the 17 per centimeter cube, n a. The material here is silicon and it is at room temperature. So temperature T is 300 Kelvin. The intrinsic carrier concentration we have seen this so many times in the past is just 10 to the 10 per centimeter cube. So in part a we want to calculate the built in potential of this junction. So V naught is the built in potential. So it is the potential when the junction is an equilibrium and this forms because we have electrons from the n side moving into the p side. This is a diffusion current. We have holes from the p side moving to the n side. These essentially meet each other and annihilate so that you have a depletion region. So on one side of a depletion region you have a net positive charge. This is the n side. On the other side you have a net negative charge that is your p side and there is a junction potential that develops. So this built in potential is nothing but k t over e ln of n a n d over n i square. So this is just a direct substitution of the numbers n a and n d are given. n i square is also given. If n i is not known can always calculate n i from the band gap and the effective density of states or the effective mass of the electrons and holes. So you can just plug in the numbers and the answer is 0.852 volts. In part 2 we want to calculate the total depletion width. So let me call the W that is the total depletion width. So the depletion width again forms because you have electrons and holes moving across the junction and are recombining. So we have seen this concept of a depletion width earlier when we looked at a short key diode or a short key junction. In that case the depletion width is almost entirely on the semiconductor side. So here the depletion width will be in both the p and the n side. The total depletion width is again just given by a direct formula substitution. So 2 epsilon naught epsilon r n a plus n d times V naught where V naught is your built in potential by e n a and n d and the hole to the one half. So epsilon naught is the permittivity of free space. Epsilon r is the permittivity of silicon. The relative permittivity and epsilon r is 11 point time. So that is a known value for silicon. So once again everything here is known. We just calculated the built in potential V naught. So we can do the substitution and this works out to be 1.3 times 10 to the minus 7 meters or if you want to write in nanometers 130 nanometers. In part C we want to calculate the depletion width on the p and the n side. So the ratio of the depletion widths W p over W n is inversely proportional to the concentration of your dopants. So this is equal to n d over n a. Another way of writing this of course is that W p n a is W d n d and this comes from the charge neutrality. So that the total positive charge due to your positively charged donors on the n side must be equal to the total negative charge due to the negatively charged accepted ions on the p side and those to essentially balance. We also know the total width W is just W p plus W n and total width W has been calculated to be 130 nanometers. So we have all the numbers. It is again a case of doing the substitution and the math. So I will just write down the final values. So W p is 43 nanometers and W n is 87 nanometers. So the total width comes out to be 130 nanometers. The acceptor concentration on the p side is higher. So 2 times 10 to the 17. So the depletion width on the p side is smaller. So let us now go to problem 2. Problem 2, we have a p n junction diode with a concentration of 10 to the 16 acceptor atoms on the p side and 10 to the 17 donor atoms on the n side. So once again you have p side and the n side. So n a is 10 to the 16 and n d is 10 to the 17 per centimeter cube. So we need to know the built-in potential if the material of the semiconductor is different. So in this particular case, we want the built-in potential for the semiconductor materials, germanium, silicon and gallium arsenide. So if you go back to the formula for the built-in potential, skt over e ln of n a n d over ni square. So if you change the material and if you keep the dopant concentrations the same, the temperature is also same. It is typically room temperature. The only thing we are changing is ni. We have seen earlier that ni depends upon the band gap square root of n c n v exponential minus eg over 2 kt. So as the value of the band gap increases, ni essentially goes down because it has an exponential with a negative term. If the value of ni goes down, then the built-in potential will essentially increase. So in this case, we have 3 materials. So I will write down the table that is given in the problem. So we have germanium, silicon, gallium, arsenide. The band gap values are given in eg and these are mainly used just for comparison. We do not need the band gap values. As far as this problem is concerned, germanium is 0.7, silicon is 1.1, gallium arsenide is 1.4. What we do need is the values of ni and ni is again given per centimeter cube. So germanium is 2.4 times 10 to the 13, silicon is 1 times 10 to the 10 and gallium arsenide is 2.1 times 10 to the 6. So ni essentially decreases as the value of the band gap increases. So we now need to calculate the built-in potential for these 3 materials. We can make use of this formula, just substitute na and nd and then the values of ni for the different materials. So we can go through and work out the math. I will just write down the final answer.  The units is volts for germanium. It is higher for silicon 0.775 and it is even higher for gallium arsenide 1.213. So as the value of ni goes down because you have a higher band gap, the built-in potential at the junction essentially increases. So this information is especially useful when you are trying to build devices with materials apart from silicon because once you know the built-in potential, you will also know what kind of current that needs to be applied through the circuit for a particular kind of application. Let us now go to problem 3. In problem 3, we have a silicon abrupt junction which is in thermal equilibrium at room temperature. So temperature T is 300 Kelvin. It is doped in such a way such that EC-EF is 0.21 electron volts in the n region. So you have an n region and you have a p region. So in this question, the doping concentrations are not given but the position of the Fermi level is. So here EC-EF is 0.21 electron volts and EF-EV is 0.18 electron volts. The material is silicon. So some of the parameters of silicon ni which is 10 to the 10 for centimeter cube, the band gap of silicon EG is 1.10 electron volts. The position of the intrinsic Fermi level EFI, it is not exactly at the center but it is very close to the center. So we can take this as 0.55 electron volts. So these are some of the parameters of intrinsic silicon that we can use. So the first part of the question says draw the energy band diagram for the p-n junction. So before we do that, we have to first draw the energy band diagram for the 2 regions separately and then we can put them together to draw the energy band diagram for the junction. So on the n side, this is my conduction band. This is my valence band. This gap is nothing but EG which is 1.1. EFI is at the center of the gap. So that is 0.55 and EG is 1.1. So this question says EC-EF is 0.21. So the Fermi level on the n side EF-N is 0.21 electron volts. So this height which is nothing but 0.55-0.21 is 0.34. So all the energies are in electron volts. I am just not writing the units where everything is in EV. We can now do the same for the p region. So the material is the same. So the band gap is the same. We just draw this slightly better. So it is the same silicon. So EC and EV are located in the same place. EFI will also be located in the center. EFI. In this particular problem, EF-EV is given to be 0.18 electron volts. So EFP. So this is 0.18. So EFI to EV is 0.55 so that this height is nothing but 0.37. So this is 0.55. This is 0.18. This is 0.37. So we have the energy band diagrams of the n and the p region separately. We can put them together and draw the energy band diagram of the p-n junction. But before we do that, I would like to calculate the concentration of electrons in holes on the n and the p side. So that we can do. We are basically using the formula EFn-EFI is kT ln n over ni and EFP-EFI is equal to minus kT ln of p over ni. So the position of the Fermi level is related to the concentration of the majority carriers. On the n side, it is your donors. On the p side, it is the acceptors. So here this term is known and this is the unknown. Same way here this is known and this is the unknown. So EFn-EFI is 0.34 and EFP-EFI is minus 0.37. So we can substitute in the values so that we get n equal to nd which is equal to 5.1 times 10 to the 15 centimeter cube. p is nothing but nA is slightly higher 1.62 times 10 to the 16 centimeter cube. So even without doing the numbers, we could have predicted that nA will be higher than nd simply because the Fermi level on the p side is located much is closer to the valence band. It is only 0.18 compared to the Fermi level on the n side which is 0.21 electron volts below the conduction band. So we have drawn the energy band diagram separately. We also have the concentration of the electrons and holes. So let me draw the energy band diagram when the junction is formed. To do that we know that the Fermi levels must essentially line up at equilibrium. So this is EFn, this is EFP. Far away from the junction you still have an n-type semiconductor and you still have a p-type semiconductor. Let me just arbitrarily mark an interface between these two and we can show the bands bending so that these two join. So this is EV, this is EC, this is EC, this is EV. So you have the Fermi levels lining up and there is a built-in potential, this is a straight line and there is a built-in potential V0 formed at the junction. So part B we need to determine the concentration of the impurities. So we actually just did that. So this is essentially part B. Just by looking at the shift in the Fermi levels we can calculate the concentration of the impurities. Part C we want to calculate the built-in potential. So V0 is nothing but kT over E ln of Na Nd over Ni square. We can do all the substitution and the numbers. So this works out to be 0.71 volts. We can also calculate the built-in potential by looking at the energy band diagram. So in this particular case the distance between EFN and EFP, so this distance is essentially 0.34 plus 0.37. So this distance delta is 0.34 plus 0.37 which is 0.71 electron volts. So when the junction is formed we know that the Fermi levels have to line up. So we can think of as either the n side shifting completely by 0.71 or the p side shifting up by the same 0.71 so that they line up. So the built-in potential or the built-in voltage is nothing but the difference between the Fermi level positions. So this is 0.71 electron volts. If you divide by E it is 0.71 volts. So instead of using the formula we can also calculate the built-in potential by just looking at the energy band diagram. So let us now go to problem 4. So in problems 1 to 3 we looked at the PN junction in equilibrium so that there was no external potential that was applied and no current that was flowing through the junction. In problem 4 which is slightly a long problem we are going to look at a PN junction that is essentially biased and we are going to calculate some values for the current in the forward and the reverse bias. So problem 4 you have an abrupt PN plus junction. So when we say a PN plus or a NP plus N the plus essentially denotes that this is heavily doped. So when one of the carriers or when one of the sides of a PN junction is heavily doped then the depletion region lies almost entirely on the other side. So one way to see that is to go back to this equation. So NA WP is equal to ND WN. So when ND is much greater than NA this implies WN is much smaller than WP. So that the depletion width is almost entirely on the P side. I will also just write the reverse when NA is much greater than ND then you have WP much smaller than WN and the depletion is almost entirely on the N side. So we have an abrupt PN junction. The cross sectional area A is 1 millimeter square. We will use the cross sectional area to calculate the current. The acceptor concentration of 5 times 10 to the 18 boron atoms on the P side. So NA is 5 times 10 to the 18 per centimeter cube and there is a donor concentration. So this is boron. There is a donor concentration ND is 10 to the 16 per centimeter cube and this is arsenic. So in this problem NA is much higher than ND. So this should actually read P plus N not PN plus. It is my mistake because NA is much greater than ND. So we have 5 times 10 to the 18 boron and 10 to the 16 arsenic atoms on the N side. The whole lifetime values are also given. So the lifetime of the whole tau h in the N region. So these are your minority carriers. This is equal to 417 nanoseconds. Similarly the lifetime of the electrons in the P region is only 5 nanoseconds and this difference is because of the difference in concentration of the dopants. The thermal generation lifetime is also given. So tau g is 1 microsecond. Some other values are also given for this problem. So mu e which is the mobility of the electrons. So 120 centimeter square per volt per second. Mu h is 440 centimeter square per volts per second and e g is 1.1 e v. The length of the P and the N regions are also given. So you have a P region is 5 micrometers and the N region with this 100 micrometers. So these are a whole set of data that is given about the silicon P in junction. So the first thing we need to calculate is the minority diffusion length and to determine what type of diode this is. So we want to calculate the minority diffusion lengths. So in the case of a P N junction under equilibrium you have a dynamic equilibrium so that electrons and holes are moving across the junction and constantly get annihilated. When we apply a forward bias the P side is connected to the positive. The N side is connected to the negative. The Fermi levels no longer line up but essentially gets shifted and when this happens the barrier comes down. So V naught is the built in potential or the barrier during equilibrium. When you apply an external potential the barrier is V naught minus V external. When the barrier goes down we basically have minority carriers moving across the junction. So we have electrons from the N side moving to the P side where they are minority carriers. We also have holes from the P side moving to the N side and there they are the minority carriers. So it is this minority carrier diffusion that essentially causes current to flow in a P N junction. So the first thing you want to calculate is the diffusion length. To know the diffusion length we need to know the diffusion coefficients. So D e which is the diffusion coefficient of electrons is nothing but k t mu e over e. So it depends upon the mobility and D h is k t mu h over h. So the values of mu e are given. Mu h is given. Everything else is a constant. So we can plug it in. So the D e is 3.10 centimeter square per second. D h is 11.39 centimeter square per second. So D h is higher than D e because mu h is higher than mu e and this is because the holes are diffusing on the N side and the concentration on the N side is two orders of magnitude less than the P side. So because you have less concentration of your dopants, the diffusion coefficients are higher. From the diffusion coefficients we can calculate the length. So L is nothing but square root of D times tau. So L e is D e tau e. L h is D h and tau h. So once again D e and D h we have calculated. Tau e and tau h are given. So we can substitute the numbers. So L e works out to be 1.2 micrometers. I am not doing the math. So all your units are in centimeters. So the answer will also be in centimeters. You can just convert that into micrometers. So L e is 1.2 and L h is larger. It is 20.8 micrometers. So if you looked at the length of the diodes on the P and the N side, on the N side the diode length is 100. On the P side, the diode length is 5 micrometers. So L e is smaller than the 5 micrometers. So this is the length on the P side. L h is smaller than 100 micrometers which is the length on the N side. So that this is essentially a long diode. So a long diode is 1 in which the diffusion lengths are smaller than the physical lengths of the P and the N region. So this is part A. Let us go to part B. So in part B, we want to calculate the built-in potential across the junction. So this is the potential when the junction is in equilibrium. So this is fairly straight forward. So just kT over E ln of Na Nd over Ni square. So we have all the numbers. We just need to substitute that. This works out to be 0.877 volts. So this particular problem does not ask you to calculate the depletion widths. But you can go ahead and do the calculation and you will find that the depletion width is almost entirely on the N side and that there is a very small depletion width on the P side. Part C, what is the current when there is a forward bias of 0.6 volts across the diode? So now you have the diode to be forward bias. The external potential V is 0.6 volts. So when you apply an external potential, the barrier height is lowered so that there is an increase in current due to the minority carriers diffusing across the junction. In this particular case, the current density is given by a constant J s0 times exponential EV over kT minus 1. Usually the exponential term is much higher than 1 so that this can be written as J s0 exponential EV over kT. J s0 is your reverse saturation current and this is given by ni square E dh over lh and d plus de over le na. So we saw the derivation for this during the course notes but this is your reverse saturation current and this is something that plugs in here. So if you remember the assignment from the short key junction, so the metal semiconductor junctions, we had a similar expression to this except that the constant out front had a different value which depended upon the thermionic emission but now here we have a P n junction so the constant here is your reverse saturation current. In this particular problem, na is much higher than nd and since they are in the denominator, this term will essentially dominate over the other term. So this is the reverse saturation current density. To calculate the current, we just need to multiply this by the area. So all the numbers are here, we calculated dh and lh in part a, na and nd are known, ni is also known so that J s0, so instead of J s0, I will directly write i s0 which is J s0 times the area. So this is 8.36 times 10 to the minus 14 amperes. So the reverse saturation current is essentially a really small value. Once you know i s0 or J s0, you can calculate the current during forward bias.  So the current works out to be 0.96 times 10 to the minus 3 amperes or 0.96 milli amperes. So the current in the forward bias is 0.96 milli amperes. So that is nearly 10 to the 11 orders of magnitude or 10 to the 10 orders of magnitude higher than the reverse saturation current. This is why we essentially call a p-n junction to be a rectifier because it conducts very well during forward bias and the reverse bias current is very small. So let us now go to part d. Part d, we want to estimate the forward current at 100 degrees. So the temperature is now increased. You can write this in Kelvin so that is 373 Kelvin. So voltage is the same. So v is 0.6 volts. The question also says that assume the temperature dependence of Ni dominates over everything else. So over the diffusion lens, the diffusion coefficients itself also the mobilities. So if you only take Ni into consideration, so we can see that the current or if you write this down, J is J s0 exponential Ev over kT. So ratio of J at 373 Kelvin to that at 273 or 293 Kelvin, this should be 373. So 373 by 293 Kelvin or 297 Kelvin which is room temperature. So let me just draw this, write this as 297 is nothing but J s0 prime or J s0 at 373 Kelvin divided by J s0 297 Kelvin. So the potential is the same. So the ratio of the currents or the ratio of the current densities is nothing but the ratio of the reverse saturation currents. This is directly proportional to Ni square. So that this is Ni square at 373 Kelvin divided by Ni square 297 Kelvin. Ni square is nothing or Ni square root of NcNv exponential minus EG over 2 kT. So for this problem we can take Nc and Nv to be independent of temperature. So the ratio of Ni is just given by the exponential term. So if you do this, this ratio works out to be approximately 100. So that the reverse saturation current is increased by 100 when we go from room temperature to 100 degrees C. So the new values of I s0 will be 100 square. So the new values of I s0 at 373 Kelvin, just me write down the final answer is 8.36 times 10 to the minus 10 and current I at 373 Kelvin is 0.10 amps. So that the current essentially increases by 4 orders of magnitude. In part E we want to calculate the reverse current when you have a voltage of 5 volts. So we want to know the reverse current when Vr is 5 volts. To calculate the reverse current we first need to calculate the new width when you apply a reverse bias. So the width W is 2 epsilon 0 epsilon r V0 plus Vr and we said that the depletion region lies almost entirely on the N side. So I only have E and B whole to the half. So this formula is something we used before to calculate the width of the depletion region when you have a p-n junction under equilibrium. So you only modified it to add the reverse bias voltage and we also removed the contribution due to N A because N D is much smaller than N A. So we can plug in the numbers. The new depletion width comes out to be 0.88 micrometers and most of this is in the N side. So when you have a reverse bias you have thermal generation of carriers within this depletion region and this thermal generation of carriers is responsible for your reverse current. So I gen which is the current due to thermal generation of carriers is E times the cross sectional area times depletion width times N I divided by tau g where tau g is the thermal lifetime of the carriers and that value is also given. So everything here is known we can substitute the numbers and I gen works out to be 1.41 times 10 to the minus 9 amperes. So this number is again much smaller than your forward bias current which is of the order of milliamps. So once again even if you take thermal generation of carriers into account we essentially have a rectifying action in a P N junction. So let us now look the last question. So problem 5 you have a germanium P N junction diode. So it is germanium P plus N the values are given. So N A is 10 to the 18 per centimeter cube N D is 10 to the 16 D H. So D H I will write on this side. So D H is 49 centimeter square per second D E because we are looking at minority carriers is 100 centimeter square per second tau E is equal to tau H is equal to 5 microseconds in the cross sectional area is 10 to the minus 4 per centimeter square. So we want to calculate the diode current. So we want to calculate I when we have a forward bias V of 0.2 volts N I is 2.4 times 10 to the 13 per centimeter cube. So this is very similar to the previous question. So J is J is not exponential E V over K T and then J is not this N I square over E D H over L H N D plus D E over L E N A. So all the numbers are given. So J is not I will just substitute and write the final answer but you can directly do the substitution. J is not is 2.94 times 10 to the minus 5 ampere per centimeter square. So the current I is not is 2.94 times 10 to the minus 9 amperes. Once you know I is not you can substitute here and you can get the current. The current I is nothing but 6.687 times 10 to the minus 6. So in this particular case the difference between the current and I is not is not as high as in the case of silicon. One particular reason is because your applied voltage is very small. It is only 0.2 volts. Another difference is that the material is germanium. So that the band gap is smaller. So the built in potential is also smaller. At the same time N I is larger. So that J is not is also larger. So these are some of the factors you have to take into account when choosing materials for forming p-n junctions.


Thanks. I hope everybody is nice and awake. My name is Ryan Muller. I work at Novartis basically across the street here. I'm a software engineer there and I write software that helps the scientists there in their research. My talk today is about BaconJS, Functional Reactive Programming and how you can leverage it in your applications along with Backbone. So how many of you are familiar with Functional Programming? How many of you like it? That's pretty good. How many of you don't like it? Oh, good. I picked out Ion's then. So basically the official definition from Wikipedia is that it's a paradigm that treats computation, blah, blah, blah, blah. The easier definition is that Functional Programming is a style of programming where functions take input and produce output without changing anything outside those functions. So here's a quick code sample of showing the difference between functional and imperative styles. This is sort of the Java version, if you like, where you sort of keep track of state yourself and you're keeping track of state in the sum variable and the i variable and we're sort of looping over it. Whereas on the right we have, we're using reduce instead, where it's much more declarative, we're just sort of specifying what we want to do and just sort of letting our reduce function take care of how we actually want to do it. If you really want to see some differences between these two styles, you can find some code that's been written in Java and in Scallet, for instance, because both of those programs or both of those languages encourage a very different style of programming. So my example has the main difference between imperative and functional programming. In functional programming, which is declarative, the focus is on what you want to do, whereas in imperative, the focus is on how you want to do it. So in 2010, we got underscore 1.0, which brought this very wonderful vocabulary for operating over collections to JavaScript and we could suddenly operate over our collections using just this vocabulary instead of having to keep track of it and do all the legwork ourselves. We got map, filter, reduce, so on and so forth. It almost becomes its own language where you have to learn the vocabulary and the concepts. And coming from this direction, functional reactive is a very natural next step to me. The BaconJS documentation looks a lot like the underscore documentation. Just in that, it presents you with this vocabulary for building programs, which you can then combine and use in very interesting ways, much like underscore. I actually think of Bacon as sort of a real-time underscore and I'll get into that with a few examples later. All right, now I'm going to show those examples. All right, so the central primitive in functional reactive is usually called an observable or a signal. A lot of the libraries out there have this really incredibly dense documentation where they use all this funky terminology, but functional reactive is really about one thing, immutable streams. If you remember two words from this talk, it's those two words, immutable streams, because it more accurately describes what you're working with. Now a stream is pretty straightforward. A stream is just a series of related events and errors that may terminate. Here's an example of a stream. This is a stream of key up events, which is very easy to generate using Bacon. I hope this diagram is clear because it's a timeline. Right now it's all the way at the right and you can see about 30 seconds worth scrolling by. I just thought this was a nice way to demonstrate the way a stream works. These are Bacon events and a Bacon event can wrap any kind of JavaScript value. It can wrap a Boolean, it can wrap a number, a string. Here I've got them wrapping a jQuery key up events. They can wrap objects and they can even wrap other Bacon streams, which is important. I'll get into that later. Streams can also have errors in them, but suppose for a second that this stream here represents a particularly unreliable weather API, we're getting temperature information from some source and the server periodically goes down. Each of those X's represents the server somehow going down or we can't contact it. Clearly we didn't write offline first app. If you look at it this way, you can think of a stream as basically a promise that can resolve or reject more than once. If you think about a promise, you have your function where you're doing something asynchronous and then you have your success callback and you have your error callback. The only difference between any promises library and Bacon.js is that Bacon might call those callbacks more than once and also Bacon gives you these really useful abstractions for converting between different streams. Oops, I skipped one. Finally, here we got a stream that ends. I mentioned that streams can end. When they end, it's pretty simple. They just don't emit any more events and you can handle this differently in your application depending on what you're doing. So we've got another weather report stream. I've gotten rid of the errors. Maybe we refactored our server or something. Really, this is all just random numbers, but suppose this is temperature. This is going to look familiar to anybody who has used underscore, which is hopefully everybody. We just take our stream and we filter it, except instead of getting a collection out of it, we get another stream. Our original stream hasn't been altered at all, but we've just got a new stream with only the events that we're interested in. I'm miserable when it's cold out, so I just decide to ignore values that are, if it's colder than 41. So that's how I wrote my filter. I'm also using the ES6 arrow functions. I hope that's okay with everybody. It's much nicer to read Bacon when you use arrow functions. So now I take it. I use another familiar function. I use map and I add the little degrees Fahrenheit symbol on the end. Now we've got a stream of strings and we can take this and we can update a div on our page. So in three quick lines, we've gone from some kind of weather event stream to get rid of the events that we're not interested in. We formatted it and now we're spitting it out onto a page. What a sign does is it'll just call a method on a div. This is just straight up jQuery. Bacon doesn't know anything about jQuery for this particular call. It's just calling the HTML method on the jQuery object that it's selecting. I've used map. There's also reduce. Reduce also has a parallel in Bacon.js. It's got two parallels, actually. One of them is called scan. We've got a stream of numbers. If you scan over them, oops, that's not a number. If you scan over them, then you get a second stream where each number is just the sum of all the numbers leading up to it. Oh, I forgot to give it a starting value. There's also another function called reduce, which is much more familiar to underscore users where it'll just give you a single value when your first stream closes. These are both useful in different instances. It just depends on what you're trying to do here. One point that I'd like to make about scan is that it creates a special kind of stream that Bacon calls a property. A property is basically the same thing as a stream. Bacon handles it very similarly. The only difference is that a stream is, like I said before, it's a series of discrete events where each event is its own distinct thing. A property is more of a continually changing value. Our first stream up there is a stream where each number is its own discrete event. The second one is a property. Each event that you're seeing there represents an updated change in a value. This is a small distinction, but it's important for certain parts of the Bacon API to understand it. We've got also a couple of ways that you can combine multiple streams. There's a merge function, which does exactly what you would expect it would. It just takes two streams and just sort of slaps them together. This is useful, for instance, if you have a search field. If you want to capture key presses, if you're looking for the enter key or the search button click, then you can take both of those sets of events and put them on one stream and then just handle that one stream and run your search function there. Very simple. There's also combined, which allows you to run a function on the latest event from each stream to create your new events. Here it's just grabbing the color from the first one and the number from the second one. Any time there's an event on either stream creates a new event on our output stream. Whoops. All right. I'm not going to bore you all with the entire API here, but there is one higher level method that you have to understand if you're going to use Bacon effectively and that's Flatmap. What Flatmap does is it takes a callback that returns a stream and it will take all the events from all the streams that that callback returns and put them on to one stream. It's a little bit of a cryptic definition, but maybe an illustration will help. Here I've got a series of URLs. Let's say we're generating them somehow. We've got user input or some kind of timer. If we take these, now I couldn't do the CSS to make a timeline coming off of a timeline, so I just cheated and added a picture. So the yellow bubble on the top there is, pretend that that's our HTTP URL here. If you map it with our just normal map function to an Ajax stream, because remember a stream is basically a fancy promise, then we'll get a stream of streams and we'll have all these sort of dangling events off of it and it'll be kind of tough to deal with. But instead, if we use Flatmap, then we get our, let's pretend that these are JSON responses, then instead we'll get our nice responses on one stream. And this is the sort of pattern that I would use to create that weather stream earlier if I wasn't using random numbers for it. So all this vocabulary, you get map, filter, merge, Flatmap, all that. It almost makes up a whole new language in its own right, with its own control flow. The fact that you have this new control flow means that you have to really attack your problems in a different way. You sort of have to get into the FRP mindset. You're not really so much thinking about past, present, and future, what just happened and what's about to happen. You're more thinking of what's going on over here, what's going on over there, because you're sort of always stuck in the current moment with FRP. Naturally, this is honestly one reason why you might want to consider not using FRP. It's because it does have a bit of a steep learning curve. It's really awesome once you've mastered it, but it's a little bit like starting off with Vim or Emacs. You're going to have maybe a couple of weeks of just really crummy performance, and then you're going to suddenly just get it, and then you're just going to take off and smooth sailing, almost. Another side effect is that you also have to learn the vocabulary of bacon. If you sit down and read through the documentation a few times, that should suffice. You sort of get a feel for the kinds of things that bacon can and can't do. Pure functional programming is good if you're an academic, if you're studying computer science, but ultimately you really do want to have side effects on the real world. Ultimately, the joke about functional programming is the reason that functional programming doesn't change global state is because nobody actually writes pure functional programs. You eventually do have to have output of some sort. As a result, you need to have some place in your application where you stop using functional programming, and you start having some kind of global state. If you're writing a background application, a really nice place to have that divide is between the internals of your models and collections and the externals. If you have bacon.js handling the sort of plumbing, then you can have your models just sort of automatically take care of themselves. You can have your views automatically take care of themselves and everything will just work. And you can just pass around those models just as you would any other and they'll just still continue to work. So now I'm going to go through a couple of recipes for combining bacon.js with backbone. Some of them are good and some of them are bad. I'm starting off with the bad one. So this is just a stream of models. This is sort of the, I don't know, maybe a first attempt at it. This is sort of why would you use backbone if you're doing this? This is you lose all the event handling stuff on the models, everything, all the plumbing that we learned about this morning at Jeremy's keynote. This is not a great, you know, you might want to just abandon backbone if you can do something as simple as this, but odds are you can't. If you could then you might not be using bacon either. A better way would be to use one model and just capture the change event as a stream. There's a wonderful little library called backbone.eventstreams. It's a file it's about this long, but you include it in your project and it'll just add this as event stream. It'll add a couple of other bacon.js goodies to your backbone items and you can get these wonderful streams just from a single call. This model is very simple, or this method of updating your views is very simple and if you use something like this to update your views, that is probably very familiar. So this would be the vanilla updating or vanilla backbone way of doing it. The bacon version would be more something like this, where you capture the event stream and then every time you see an event you would render. This is useful for updating your views, but it's also useful if you have multiple people potentially editing a resource and you might want to prevent conflicts. So instead of saving, and actually just thought it honestly just thought of this during Greg's talk, the offline first, if you have an offline first app and you save all your events, all your change events to local storage, when the app comes back on it can just push that list of events up to the server and the server can decide how to handle any conflicts, kind of like a get merge type thing. Or to go back to the view example, if you want to avoid updating your entire view, you can just do something like this and have only listen to events on a single field and create a stream from that. Now what I'd like to say about these samples is that the way that they are right now is you would probably never write them, because they don't actually do anything. I mean if you look at the model.onChange line and then the model.asEventStream line, it's longer, it's a little more unwieldy, you have all this bacon stuff that you have to deal with, so why would you do that? And the answer is that in case you want to refactor it later, in case you want to change things. Let's say you have a counter on your page, let's say you have you're counting some kind of event and you're testing and this event only occurs maybe every two seconds or so. So you write your code, you add streams in there just for the heck of it and you push the production. Well it hits production and your users start using it and suddenly you find out that this counter isn't updating every other second, it's updating dozens of times per second. So your browser starts to sweat, your CPU fare goes up and what do you do? Well if you weren't using bacon you'd probably introduce a variable at the top, you'd probably keep track of when the last update was. On every update you would check how long it's been, see if it's been beyond the certain interval and that'd be probably maybe four or five lines of code with a lot of room for error. If you're using bacon.js though, bacon actually has a very nice throttle function. So you basically just throttle based on how many milliseconds you want minimum between events. And so that way you can say okay well I just want to throttle it to every 250 milliseconds. So suddenly your UI gets much snappier. You're only updating four times a second but maybe you don't need all those updates that are making your browser sweat. So what you lose in terms of time spent learning bacon.js you gain in maintainability and flexibility of your code. One of the coolest things, oh responsive design folks. That's better. One of the coolest things you can do is there are a handful of ways that you can combine bacon.js with web sockets to sort of transparently have your streams available on either side of server versus client. This is one pattern that you can use to communicate changes in the model directly. Bacon also has a handful of other functions for converting from a callback or converting from a node style callback. There are quite a few ways that you can actually handle streams across that server client boundary. So when you have a stream on your server and you have it handled in this way you can actually take that same stream that you have on your server and do all your mapping filtering on it on the client or perhaps you want to throttle it on the server before you send it to the client. If you spend maybe 20 minutes playing around with it it's a really cool technique and it's a lot of fun that you can just sort of freely flow between server and client side if you're using node. All right I'm going to demo making a simple chat app. Suppose you've got an API like this one that's already been written. In reality we probably use web sockets for this but I just want to use this for an example. Also the chat app is just going to be one big chat room just for the sake of simplicity. So here's the the chat thread collection that's how I represent the collection. It's a little complex there's a lot of baking going on in there but I'm going to step through it step by step. This creates a property that always contains the time stamp of the last update with the server. You see we're just capturing the request event as a stream and then we're using scan to convert it into a new date so that way it'll always contain the current time stamp. If you notice before on the filter example that I had all the streams that you create from a particular stream it always occurs simultaneous unless you're doing some sort of delay or throttle it'll always be simultaneous so that time stamp is always going to represent time stamp on our request. This is very simple it just creates a stream of empty events and it's just basically a heartbeat every two seconds it sends out an empty event. Now here's where we get into the cool stuff. We take our last sync property and remember property is a continuous value so we can sample it between events because it always holds on to its value until it changes. So we take that value and we sample it on that heartbeat and we so now we have a stream of time stamps where each time stamp is the time stamp of the previous synchronization with the server. You take that and you map the time stamp to URL as the API documentation had a couple sides back and now we're sort of in familiar territory this is the the side that I cheated and used the picture on. You take that URL and you flat map it manually trigger the request because we're sort of bypassing the synchronization only because this isn't really a create read update delete app. This is more sort of sending discrete items and then we return our Ajax request as a stream and so that that Ajax stream is going to emit one event which is going to be an array of JSON items representing the messages that occurred since the last update and then the stream is going to end. So now we have an array or no a stream of arrays of JSON objects. We take that we just map it to our message model which I want to demonstrate in a moment or just show the code for it a moment. I messed up the indentation there a little bit but the this is fairly fairly straightforward and then you take that result and you call the push function on your collection. So the this context up there refers back to the collection and so now in just a handful of lines we've gone from taking our heartbeat mapping it to a URL with the last timestamp. Take that create an Ajax request create a stream of Ajax responses map them to our message model and then assign it or rather push it into our collection. Here's the message model it's very simple that's why oh I forgot about that. So here's the demo you don't need that functionality for for this part of the demo. I've got a few bots that are on there they're going to join in a second but the essential functionality for receiving messages is in place. So now I'm just going to add them out the sending functionality. I'm not using streams here you probably want to listen for an error or something here but this is just a simple post request. So then the view actually uses that merge functionality that I talked about to combine the enter key presses with the send button clicks. So we take the key up events and we filter based on 13 which is enter your key code and you merge it with the click stream. So now I can use the enter key or and the delay is because of the polling. So now because I've merged them I've combined these events into a single stream that I can handle in a particular way. Oh yeah coffee script goes very very nicely with Bacon.js because coffee script is sort of designed for functional it's ideal for functional programming and that's exactly what Bacon.js is. So any program that you write in JavaScript is going to be absolutely gorgeous if you write it in coffee script. Maybe just so maybe some of you have heard of it I don't know. So if you are looking for more information about this I've got a few resources for those of you who liked my talk those of you who didn't like my talk. This is the best explanation of functional reactive program that I've been able to find. A lot of the other explanations are really dense they sort of cram a lot of terminology in there but this is actually a gist on GitHub. I love that people are posting these sort of blog posty things as gists I know it just makes me happy on some level but I'll post these links on Twitter afterwards and this is actually this has a lot of diagrams it uses the RxJS library for its examples but it's very easy to follow no matter what library you're using because it's got diagrams it's actually where my static image diagram came from. The Bacon.js documentation is of course a great place to look it's got plenty of examples. RxJS is the implementation that I mentioned that the intro uses. If you're not a huge fan of Bacon.js but you're still interested in playing around with reactive then I would advise checking this one out. These two are really interesting. Elm is a little language that compiles to JavaScript and HTML in your browser and it's designed for functional reactive programming so you don't so much have variables you just sort of have streams as the primitives and it's a very interesting way of looking at it. The programs are a little dense to read. You have to practice a little bit. They read a little bit like Haskell which is not very well but they are beautiful little programs and there are a couple of really cool demos with it. There's also a few days ago Matt's creator of Ruby published I think it's just a specification. I'm not sure if it's the full language but information on a language that he's working on called stream and stream basically uses bash pipes. It's sort of a metaphor for functional reactive programming so you take your streams and you can use the pipe to sort of go from one item to the next. It looks a little bit ruby-ish but it's also got sort of bash mixed in there. It's a very interesting way of thinking about it. Finally for those of you who just aren't interested in functional reactive at all there is communicating sequential processes. I can never remember that acronym and JSCSP is a particular implementation of it. If you play around with ES6 generator functions then this library might be fun for you because it sort of brings a little bit of the flavor of go routines to JavaScript and let's you use sort of that style of programming. So that is basically the whole presentation. I think I'm out of time. Yeah okay. If you have any questions feel free to come find me and thanks for your patience.


This is where my neighbors lived for the last two and a half years. About a month ago, they disappeared. Some new owner came in, started cleaning up the place. This is after a month of cleanup. Seriously again, this is after weeks of cleaning up. I've lived next door to these people for two and a half years. I never had any idea. They tell me that this garage looks okay right now, but they tell me that it was five feet deep in garbage. Five feet deep. It's almost as tall as me. And then it had a terrible rat infestation. As a matter of fact. Oh, this is after the rat specialist has been here a number of times. There's still rats. There's water here. It looks like it was on fire. It's all melted. This is hair. Oh, there's another one. Yeah, dead rat.  There's a garage toilet. This place looks like it hasn't been lived in for years. It looks like an abandoned building. What on earth are these? There's a little girl lived here. Saw her catch the bus and go to school every day. She wasn't covered with filth. She looked like a normal human being. She must have been working hard to keep herself clean. Okay, this is only the beginning. You haven't seen anything yet. Look at this. Look at the bathroom. What is this all over the sink? What is this? And then we've got the tub. Remember there were people living here. Okay. There's just filth along every wall. Every corner. Here's the main bedroom. Holes in the walls. More holes in the walls. Again, the floors. The edge of the floor is just covered with filth. Look at all this. And again, I never saw this when it was actually dirty. What? What must have been the little girls room? Almost as bad. Not as bad as the main bedroom, though, but almost. So you never know. You never know. Good luck, kid, wherever you are.


All right, so we've progressed to the point where we understand equilibrium as being summed up by this expression, where if we're at equilibrium, if a chemical reaction is at equilibrium, this must be true. Some product of the amounts of each reactant and product raised to some powers must be equal to the product of some partition functions raised to some powers. This right-hand side, the partition functions raised to these stoichiometric coefficients, those partition functions are constants at a particular temperature. Stoichiometric coefficients are constant. This whole collection of things is a constant, and that's what we call the equilibrium constant. On the other hand, on this side, these numbers of molecules, those are going to depend on the extent of reaction. If we have the reaction hasn't proceeded forwards or backwards, then we have certain amount of reactants and products. If it's proceeded a little bit forwards, then the number of atoms or moles of products has changed. These n values depend on the extent of reaction or the reaction coordinate that we're calling C or squiggle. Depend on squiggle. What I mean by that, let's do an example so we can see in detail what I mean by that. Let's go back to our favorite reaction at the moment, the equilibrium reaction between hydrogen and bromine gas, forming hydrogen and bromide gas. Let's suppose as initial amounts of each of these substances, we have my initial, I'll write n naught, the initial amount of H2, let me make the initial amount of H2 and Br2 and HBr all equal to one another just for simplicity. Let's just say I have a mole and a half of each of these.  Those are my initial amounts. After that reaction proceeds, maybe it's, and in fact, let me tell you one more thing about this reaction. Let's say we're doing this reaction at a particular temperature where the equilibrium constant is 1800. So put these molecules in a container, the reaction is going to proceed maybe forward, maybe backward. We want to find out how far forward, how far backward that reaction will proceed until it reaches equilibrium under which conditions this set of terms is equal to that equilibrium constant. So after the reaction has proceeded some amount, forwards, backwards, the number of molecules or number of moles of H2 remaining will be however many I started with. Since H2 is a reactant, I will have lost as many as the reaction coordinate says. I've lost as many as this extent of reaction. Likewise, number of moles of Br2 is its initial amount minus the extent of reaction HBr, on the other hand, I'm going to have the initial amount of HBr. And then since that's a product, I'm going to gain HBr if the reaction goes forward and in fact I'm going to gain two HBr, so I add twice Xe. And what I do with those amounts of each species is I say at equilibrium, this condition must be true. Amounts raised to the stoichiometric coefficients must equal the equilibrium constant. So setting this term equal to this term, reactants remember have negative stoichiometric coefficients when I raise them to the negative power they're going to appear in the denominator. So this looks like moles of the product HBr raised to its stoichiometric coefficient over moles of reactants H2 and Br2 raised to their stoichiometric coefficients, those negative coefficients put them in the denominator. That has to be equal to the equilibrium constant. So this is why we wrote down these equations for how much of each reactant and product we have at some particular extent of reaction just inserting these values into this equation. Moles of HBr is, let me go ahead and start using initial amount 1.5 moles of HBr plus twice the extent of reaction that gets squared in the numerator. In the denominator I've got initially one and a half moles of H2 minus some quantity and one and a half moles of Br2 minus squiggle. That all must equal the equilibrium constant K. So now I just have K remember something I know, I know the value of K. I'm solving for the value of squiggle. I just have an algebraic expression to solve for squiggle. What we do from this point is going to depend on what the particular reaction looks like. Every different set of initial conditions, every different set of stoichiometric coefficients will lead to a different algebra problem. In this case, since I have something squared on the top and conveniently this quantity times the same quantity is something squared on the bottom, so if I take the square root of both sides, I've got this ratio of one and a half moles plus two squiggles in the numerator, one and a half moles minus squiggle in the denominator has to equal the square root of K. Rearranging that, moving this denominator over to the right hand side. I've got one and a half moles plus two squiggle must be root K times one and a half minus squiggle. To solve for the extent of reaction I'll pull that over both of those squiggle terms over to the left side of the reaction. So I've got negative root K squiggle that I'm going to pull over here and make positive root K and I've also got a two squiggle on this side. On the right, I'm going to take this one and a half moles and put it over here. So I'll end up with one and a half moles from here and also one and a half moles times root K. So all together I've got one and a half moles times root K plus one. So then I can write down solving for the extent of reaction. It's going to be one and a half moles times root K plus one over root K and it looks like I have a sign error. Yes, I do have a sign error. This one and a half moles when it went over to this side was negative. Positive one and a half moles over here is negative one and a half moles over here. So the numerator is root K minus one, the denominator is root K plus two. So now I've delayed as long as I can sticking in this value of 1,800. If I take one and a half moles times square root of 1,800 minus one divided by square root of 1,800 plus two, that square root, that ratio of these square roots plus and minus some quantities, that works out to about 0.93. So I take 93% of one and a half moles and I end up at about 1.4 moles. All right, so algebra work is done. I've solved for squiggle, I've solved for the extent of reaction. All that tells me, however, is how far the reaction went forwards or backwards. I'm not quite done yet. I wanted to know how much of each reactant and product we had when we reached equilibrium. So if the reaction proceeded to this extent, 1.4 moles, then the amount of H2, Br2 and HBr, the amount of H2 and Br2 are both, in this case, one and a half moles minus the extent of reaction. So one and a half moles minus 1.4 gives me 0.1 moles. So I've only got 0.1 moles left of each of these reactants. The reaction went almost all the way forwards to completion, but not all the way. And then the amount of product I've got, the amount of HBr, that's the initial one and a half moles that I started with plus twice squiggle. So I'll go ahead and write that one out, one and a half moles plus twice the 1.40 moles that the reaction proceeded, 1.5 plus 2.8 gives me 4.3. So I've got 4.3 moles of HBr when the reaction reaches equilibrium. So in that sense, we're now done. As you can probably guess, when you solve equilibrium problems like this, the algebra problem is detailed enough that there's enough likelihood of an algebraic mistake if I hadn't caught my own algebraic mistake here. It's nice to be able to do a double check. We know that if we've successfully reached equilibrium, this must be true. The moles of HBr squared over the moles of H2 and the moles of Br2, that should come out equal to the equilibrium constant. So if I haven't made any mistakes, then if I take HBr squared, 4.3 squared, divide by H2 divided by Br2, so divide by that number twice, 0.1 mole for H2, another 0.1 mole for Br2. If we do this calculation, 4.3 squared divided by 0.1 divided by 0.1, plug that in the calculator, that'll give us a double check. In fact, that does when we round it off to two sig figs. That does give us 1,800 as it should. If we hadn't made any mistakes, this will always work out. But it's nice to be able to do this double check and make sure that you confirm your assumption that you had reached this value of the equilibrium constant. If I pay attention to units here, notice mole squared in the numerator cancels a mole and a mole in the denominator. That points out the fact that this value of K is unitless. If we didn't have a reaction where the molecularity was the same on the reactant and product side, if I didn't have two molecules of reactants turning into two molecules of products, then it wouldn't have been the case that the units of moles canceled in the numerator and the denominator. In that sense, even though the algebra might have felt a little bit tedious, this is a relatively easy problem because of the convenience of that molecularity being the same on the reactant and product side. These get a little more complicated and there's an extra source of complication when we have reactants where we have a different number of products than reactants. We'll see examples of that coming up soon.


Hey everybody, I'm Jen and I'm the host of the open source comm weekly top five This is where I bring you the best articles from the past week There's an article in the site every Friday, and there's this video on YouTube on our YouTube channel every Friday I encourage you to subscribe and Get an alert when we upload a new video So I told you guys last week that I had had a surprise for you and the surprise is a new setup so have this great board here have my little kitty cats and dancing flower and penguin for Linux and I'm gonna be using some notes here And I'm gonna try to look up at you guys as much as I can and reference my notes as little as possible But you know, it's a learning process. So stick with me. Hopefully it'll be fun I won't have too many ums and ramblings So I have some updates For you first and the first update is all things open. I have my all things open t-shirt on here from last year The little space dude is the mascot of all things open And then I have all things open up on the board The conference is going to be from October 18th through the 22nd in Raleigh, North Carolina Where it is every year for the past few years since it began This is an open-source conference that talks about open source in technology open source beyond technology communities design software Content management systems all kinds of stuff It's a super fun conference and I am actually Looking out at the Raleigh Convention Center right now where the conference is going to be hosted So it's a great time. Raleigh is a great city Most of the open source comm staff is in Raleigh, so we go every year We'd love to see you Please drop us a line if you're gonna be there and we might be able to meet up with you I got this great watch from the spark fun folks a couple years ago at all things open and Really neat connections like this happen. It's just you meet some neat people and all kinds of different folks are there So check it out The other update that I have for you is that open source hardware Content theme begins next week. And so what that means is that every day next week? Open source comm is going to be publishing an article about open hardware So we run these themes every week about a different topic and during that week or that two weeks depending on the theme and What we have planned we run an article every day So I would suggest that if you want to stay up to date on the newest articles Be alerted that we have a new article. I would follow us on one of your preferred social media platforms We're on Twitter. We're on Facebook. We're on g-plus or on LinkedIn You can also sign up for our weekly newsletter that's going to give you the best or all of the articles that we've published for the past week I think are most of them. So anyway sign up for one of those great ways to Basically just stay in the loop and and find out about the content that we're publishing All right, those are my updates. Let's move on to the top five So I'm going to reference my notes just a little bit here, which is which is a new process and I think it's going to work out good Our first article at number five is open source has to be more than Linux Jonah Bacon writes for his column six degrees this time about the meaning and implications of openness One of the many quotes that grabbed my attention from this article. It's a great thought-piece article is this is this quote here Whether it was Linus Torvald's first kernel the first incarnation of Wikipedia The first Raspberry Pi board or anything else the world's greatest innovations all started with that one person just making something What transforms a hack into something with potential is that it is one shared freely to formulate it with free tools that anybody can access and Three invites participation from other people who can make the hack better Great quote great article. Check it out At number four three big lessons. I learned from running an open-source company So this article is written by Grant Ingersoll and he is the CTO and co-founder of lucid works a venture-backed open-source company and Grant writes about his experience Running and building lucid works from the ground up in this article. He gives tips on what to sell When support is necessary and how to manage the community at number three Why tools like Docker vagrant and Ansible are hotter than ever Greg DeKonisberg is the vice president of the automation tool Ansible and in this article He writes that tools that allow people to speak the same language Are seeing the most success right now? Read more about the success of Docker vagrant and Ansible in this article At number two practical Python programming for non-engineers Al Stugert is a software developer and tech book author who writes that Just because we're surrounded by computers doesn't mean the average person needs to be able to reprogram their smart fridge He says though that we are in the age of the internet and the Internet of things is upon us So if we want to be able to customize that stuff as this age approach Approaches it might be good to know a little bit of programming even if you're not a programmer Even if you're not a non-engineer like me So he says that people like us can turn to Python. We can get just a little bit educated in this article Finally at number one this week RTFM how to write a manual worth reading? So let's start with what our TFM stands for it is read the fine manual And it's a phrase the author of this article rich Bowen says is uttered at people who have asked a question that we the Enlightened feel is beneath our dignity to answer But it's not beneath our dignity to use as an opportunity to squish the newbies ego ouch Our TFM applies for one to the documentation needed for software projects So many see it as a necessary evil because maybe it's not a fun job, but it's true that somebody does have to do it Read more in this article from rich About how there are people who want to do this job and there are ways to make it fun and engaging All right, everybody that wraps up the top five this week Thanks so much for joining me check out all things open check out our open hardware theme next week, and I will see you next time