All right.
So look the next challenge and we're going to be building on the factorial kind of example that we looked
at before and we already had one factorial challenge up here where we said Hey use Go-Betweens and channels
to calculate factorial and that solution just as a reminder look like this right.
Factorial for an vectorial returns a chance.
And so he said make a channel and then launch this Gauvin and and then you know here is four and that's
what we want to do factorial for.
And so he gets off doing it and then takes the total and puts it onto the channel and closes the channel
it's off and running.
So this has been returned.
And this is waiting to receive all the channels is putting on the town that receives from the channel.
So we have that matching seat or cool you put on it put off.
We don't have any deadlocks and this is blocking.
Right.
Until it's all done so that we're not exiting main.
So the solution for the previous factorial challenge which was to use go routines and channels to calculate
factorial.
So we're going to kind of elaborate on that.
And and here's the code I want you to start with and it's right here.
The challenge description solution of this one 06 challenge description and so start with this code
and this code here is already using channels and it's already using routines to do factorial and it's
doing Factorial on 100 numbers.
And so these numbers are going from you know three to 12.
And we're doing that 10 times and we're putting those onto channels who are already calculating factorial
using channels and go routines and concurrency.
But what I want you to do is I want you to also use the fan now fan and Poutre.
Right.
And I want you to change that.
Currently we're doing 100 calculations right from 3 to 12.
That's 10.
And we're doing that 10 times the ten times ten is 100.
I want you to do it for a thousand or maybe 10000.
OK.
We'll start with a thousand a thousand and then and then run that and and use the fan fan pattern.
So the main idea of the challenge is to use the fan out fan and pattern and then also the second challenge
it be interesting just to look at how much of your computer's resources are being used when you run
this and see if that might crank it up and see you know like you run for 10 miles per 100000 times and
then take a look at that.
And then if you can post the percentage of your resources being used to this discussion I'm going to
show you that.
So here's the solution.
Pause video go work on it allow yourself to struggle.
That's really part of the learning process allow yourself to get those deadlocks and to sort of bang
your head against the wall.
And then also know that the solutions here so don't struggle too much of them take a look at it.
So just talking through the solution.
So you should have already paused the video and tried to work this out.
And I'm going to pause talking why this noise goes by.
All right.
So I should have already worked this out.
And and if I'm going just talking the solution through if I'm going to you know do a thousand iterations
right.
I need to do a thousand factorial computations.
Well here's 10 right now I'm doing 100 10 times 10 years 10 times.
So there's a thousand computations like that's one of the changes I need to make and something and come
over to my solution see if I made that change and sure enough I have a hundred there.
And what I did from my solution it was I pretty much mirrored what was done in this glaring out class.
So I had a gin and gin is going to loop you know a thousand times and put values onto a channel and
then close that channel and then it's all running and go on.
And so that you know gets launches out there running and then the chairman gets returned and returns
of the channel and I take that resealing channel and I'm passing it to 10 workers and here all those
workers factorial function.
Right.
And so this is fanned out.
I have multiple functions or multiple functions pulling from the same channel.
And you can very much see that.
And then here's Fanon.
I have a whole bunch of channels and they're all going to be merged into one channel and we're going
to range over that to get the values off to that one one China channel.
So we have found out and we have found and we're doing it for a thousand a hundred times 10.
There's that 10 100 times 10 right we're doing it for a thousand factorial computation.
All right.
So factorial is a you know Jen is pretty straightforward that first part and then factorials just like
we were looking at before.
And so as fact and then merge is like like well we saw a square and so you know weight group make a
channel and then I use the fun expression out right here and then add to the weight group delay of my
slice.
From my variadic parameter and then I'm going to range over all of the parameters that came in.
And this is the range for ranging over slides so I have a key and a value or an index and a value.
However you want to think about it and then I say go out and I launch this as a function.
Launch it is a go routine and I pass then whatever the current Chan event is the receive only.
Sorry I don't get that to be so.
And Channa and that gets passed in here.
So this is a function that goes to there and it takes every C only chance and and then arrange over
that and I get the value out and I put that onto the one channel out which emerging everything onto
and then I called done when this is done.
And yeah.
And so that's all getting put on to out and this is off and running and waiting so that I could finally
close out when you know I have put all the values onto it.
I need to get onto it.
And so these have all been left off and running.
And this is off and running and these you know so when I go through the flow of execution of this function
come in I'm just making sure I don't have any deadlocks come in that runs that runs this function gets
assigned to that variable and it's a funky expression that's running this runs this loops launches all
of those covered scenes and then the flow just continues down here that gets launched in the Go Team
boom our return.
And then that comes up here receiving Chan int and that gets put right there.
And now a range is ready to pull off of that channel and so that channel is out channel and says I'm
putting things on the channel I'm all supplying them off.
So I don't have a deadlock.
And then it's going to close eventually.
So that when this range will know we're done.
It will be a close Channel off the rest of lives and then I'll finish.
I'm not exiting main early because that's blocking.
So just checking all of that that I don't have a deadlock and then I'm not you know is flying right
straight out of Maine before all the processing is done.
All right.
So there you go.
Is there anything else Sara that we got found out.
We have found it in.
I guess all we can do is watch it rather than look at the resources and this is 7:7 get do that and
change directory and go Windows 7 and clear all this out and then go run and we'll add race there just
for fun the first time through I forgot to that tab or go and BAM.
So did all of that work.
And you can see it's totally jumbled.
That's kind of nuts but it is jumbled to the most beautiful way.
Right because all those things are all running racing doing what they do.
And and new race conditions are reported but it got some lightning fast.
And so we did that for a thousand factorial computations.
And then what got printed was when we merge values off of them it didn't seem like a thousand things.
Got it right there.
So we to add a little trouble shooting in there and I might be recording this video.
So the troubleshooting that I would do would be far too high.
And so that means I have the variable i know why that's why.
Why.
Which is now zero and and then argue y plus plus Y plus plus and Trent.
Why am I going to have my answer Stromness again
you go Main.
What do you know.
Like a thousand values.
I don't know why that looks so thin before.
I'm not quite sure what happened so let's run with the race condition again and see what comes out.
And it's we're having a little bit of like mixed up stuff right there.
But before that was all looking pretty like you know pretty evenly sequentially it's get go around race
mean go to.
So it is a thousand again.
It definitely looks like a lot more.
I don't know maybe I was just scrolling by them way too fast.
So I'm going to change this now to 7000.
I'm going to make it come down in 2000.
And this is going to allow me to bring up my activity monitor and I can see how much of my CV is being
used by different processes.
I have no idea.
CLV quarter.
Oh that's my camp recorder that's taking up 1.6 to see use.
My computer is
Intel Core I-17 which has four cores but it's like two each core has like a virtual coursers actually
like two cores for every core.
So 800 percent potential that I could be right here right here.
So see I get this one hour on this and we're going to be doing it for 100000 right.
And yeah let's run it and clear this all out and let's just run it with mean race.
So that's running.
Oh my gosh so so fast.
I didn't even able to get over here.
All right let's make it even more.
And instead of
instead of you know we'll make it 30 in the calculations to 40 and 30.
Thirty one two three four five six seven eight nine ten guys and we'll make it ten times a million by
itself calculates and it's a lot more complication for each one.
I just want to run for a bit so I can go look at my resources.
It's running and looking at resources.
All right.
So that was an interesting experiment.
And my answer for that is it was hard to see how much of my resources are being used.
So when you take challenge to out it's gone