16.10: Particle System with JS Array Functions - Topics of JavaScript/ES6

Поделиться
HTML-код
  • Опубликовано: 1 окт 2024
  • In this video, I demonstrate higher order array functions in the context of my simple particle system coding challenge.
    🎥 Simple Particle System: • Coding Challenge #78: ...
    🎥 for...of video: • 16.4: for...of loop - ...
    🚂 The Coding Train website: thecodingtrain....
    💖 Support this channel on Patreon: / codingtrain
    🛒 To buy Coding Train merchandise: www.designbyhu...
    📚 Book recommendations: www.amazon.com...
    💻 github.com/Cod...
    🎥 For an Introduction to Programming: • Start learning here!
    🎥 For More Coding Challenges: • Coding Challenges
    🔗 p5js.org/
    🔗 processing.org
    📄 Code of Conduct: github.com/Cod...

Комментарии • 102

  • @adribier
    @adribier 6 лет назад +31

    I love your videos, please never stop doing them :)

  • @peterosudar6653
    @peterosudar6653 5 лет назад +6

    "Do I qualify as a functional programmer?" So... if you don't I don't think anyone does.
    Your humility goes beyond compare.
    Thank you for your inspired work.
    We all have a lot to learn!

  • @organizedmarketing
    @organizedmarketing 6 лет назад +3

    You are very humble and Its very helpful watching you code , and don't hesitate to leave in all the debugging that is enjoyable and very helpful. I will be using this lesson while I attempt to refactor the Asteroids game. i have been learning with while i build it. i was inspired by your videos to use it as my learning project. I am diving into es6 or maybe stumbling might be a better association. Here is a link if you're interested ( customstairpro.com/asteroid/index.html ) . I will add it to git hub and all that when I'm done. still working on initiating all the levels and the variety of game play. as well as mobile button so i can play it on my tablet. Thank you

  • @Sworn973
    @Sworn973 6 лет назад +16

    That was a good Idea, the way you make each individual little video about one function, than group all them in one nice and clean example. Very well done!

  • @koopa9815
    @koopa9815 2 года назад +2

    This series has been so helpful so far!! Thank you. Somehow I got through 10 videos in one sitting lol

  • @thedotisblack
    @thedotisblack 6 лет назад +6

    Simple and great example with lots of potential!!

  • @DanielPradoBurgos
    @DanielPradoBurgos 6 лет назад +6

    I'm glad ur embracing ES6 and Higher order functions! Only got a small advice you might wanna consider, perhaps chaining your class calls, so instead of update and show being 2 different lines, you could return the class instance so you could do p.update().show() :)

    • @avatar098
      @avatar098 6 лет назад +3

      Good advice, but only in moderation. Some patterns work really well with this (i.e. Builder), but generally you don't want to have
      myInstance.method1().method2().method3().method4()...methodN(); as it convolutes the meaning of the code segment. Watch out for bad code smell :)

    • @DanielPradoBurgos
      @DanielPradoBurgos 6 лет назад +3

      avatar098 completely agree :) should be used only in moderation and just when it makes sense :)

    • @kamoroso94
      @kamoroso94 6 лет назад +4

      avatar098 if you're doing a lot of method chaining, just put each method call indented on it's own line like:
      const nums = new Array(100)
      .fill()
      .map((_, i) => i)
      .filter(x => x > 10 && x % 3 == 0)
      .join('
      ');

    • @victornpb
      @victornpb 6 лет назад

      jquerize all the things. ye idk how i feel about this

    • @DanielPradoBurgos
      @DanielPradoBurgos 6 лет назад +1

      victornpb it's a common pattern in JS :)

  • @eugen189763987689379
    @eugen189763987689379 6 лет назад +10

    you are so awesome!

    • @eugen189763987689379
      @eugen189763987689379 6 лет назад

      I used to hate java. At college studying IT there was java everywhere. And I could not understand. I am a Linux guy, I love kernel code, I love programming at sys level, programming in C. Also had a lot to do with C++. And java was a academical approach - at least for me. And back then (2011/12) also JS was more like to be used for alert boxes and simple mathematical operations;?back then PHP was the bitch to rule the shit. I had quite an extensive journey to sys and net administration for the last 6 yrs. 4 months ago I wanted to get an updated overview of what's going on with web dev etc. blew my mind!
      I had assumed that JS was some Hippe language to be disappeared within 2 or 3 years. I thought ruby might be the way to go.
      Well, obviously the wrong horse to make a bet on. Now you come into play. All I missed is being filled by you. Even more efficiently than autodidactically consuming JS by myself. Thank you so much for thate

  • @volfegan
    @volfegan 6 лет назад +2

    Shiffman, you are a very functional programmer of higher order! Rejoice by the Emperor.

  • @dileepkumar-xl3lv
    @dileepkumar-xl3lv 6 лет назад +1

    Why splicing the objects??
    Instead we can make the particle object position to zero and alpha to 100.
    Hope u get it.

  • @ankitasinha9912
    @ankitasinha9912 4 года назад

    Hi
    I executed the following snippet without {} for arrow function and I get the result as 15. However when I put the braces I get undefined
    let ar1=[1,2,3,4,5];
    let reducer1=(acc,currentvalue)=>{acc+currentvalue};//I get undefined with {} .However without {} i works perfectly
    console.log(ar1.reduce(reducer1));
    Why?

  • @WillMiddlewick
    @WillMiddlewick 6 лет назад +4

    These videos deserve more views.

  • @Kammo777
    @Kammo777 6 лет назад

    Awesoem things just cool
    sir can you make for me....or solve it please..
    document.onclick = initall;
    function initall(){
    document.onclick = init;
    }
    for (var i=1; i

  • @AmanBhimani
    @AmanBhimani 6 лет назад +3

    Isn't this really inefficient? You're looping through the same array 5-6 times, whereas before it was only 1. These higher order functions also have lower efficiency than using a simple for-i loop.

    • @TheCodingTrain
      @TheCodingTrain  6 лет назад +2

      Yes, this is an excellent point!

    • @AmanBhimani
      @AmanBhimani 6 лет назад

      Thanks for the video though! Always love watching these to learn more than I know.

    • @quillaja
      @quillaja 6 лет назад

      I'd guess the larger concern is that each call to these 'functional style' functions produces a new array, with a cost of memory use and garbage collection. As for looping, it's probably not much of a difference between doing "5X" once or "1X" 5 times. But I could be wrong.

  • @samuelmarialva120
    @samuelmarialva120 4 года назад

    I'am trying to mimic your code, so i can learn practicing. Everything looks the same but my particles amount are only 51 instead of 255. How i can change the amount of particles that i have? Anyone?

  • @SubjektDelta
    @SubjektDelta 6 лет назад +1

    18:54 - basically everyone who started to program :D

  • @bassam.2023
    @bassam.2023 4 года назад

    For the sort function, what if you get 0? What is a.col - b.col at 17:40 is equal to 0? Is that a positive or negative or true or false?

  • @jithunniks
    @jithunniks 6 лет назад +1

    That's exactly how you should teach coding!!!

  • @ridespirals
    @ridespirals 6 лет назад +1

    you know what, a lot people rag on you for writing old school for loops, and there are many cases where it indeed helps readability or understandability, but a plain for loop with a cached length variable is the fastest way to loop in javascript, by a wide margin. there are many benchmarks out there about it, so whenever someone complains about the plain for loop just say "it's technically the fastest way to loop in javascript." because it's true!

  • @RichardHumulock
    @RichardHumulock 6 лет назад +2

    YOURE THE COOLEST!

  • @GoDXaviA
    @GoDXaviA 6 лет назад +4

    Such a great series

  • @rahulbansode1537
    @rahulbansode1537 5 лет назад

    I didn't not understand the reduce part why did you add that 0 and createVector(0,0) in the return part of the arrow function

  • @algc19
    @algc19 6 лет назад +1

    I'm learning Haskell at college and suddenly it is just like the good ol' JS thanks to your videos!

  • @StarlightHeaven
    @StarlightHeaven 6 лет назад +1

    Nice series dan. It helps me a lot to understand about high order function that before i cant really understand by watching another videos. Your simplify and fun explaination makes me feel comfortable and excited to learn about this series and at last makes me understand about higher order function. You are my fav dan. Cheers!

  • @CryptoRootz
    @CryptoRootz 4 года назад

    you lost me bro....

  • @manuelavalos4578
    @manuelavalos4578 5 лет назад

    Hey!! you are awesome!! Thanks for share your incredible knowedge of javascript and another stuffs!! You inspire me everyday! Thanks!! Saludos desde Argentina!

  • @nicktube65
    @nicktube65 6 лет назад

    I noticed the issues you were having with fill(). So, to keep with your ES6 higher order function theme, I wrote a method to fill an array using ES6 Generators.
    Here's the gist: gist.github.com/n-j-m/fb726239a62b48eee0720ddc60200cee
    Hope it helps (and maybe you'll do a tutorial on generators??).
    You make learning programming fun! Keep up the good work!

  • @raelindan
    @raelindan 6 лет назад

    Since this video is roughly mainly about es6 syntax for array things, my question is this :
    What is the current es6 browser support and could it be used in big, say like asp. Net mvc, projects? Or is backwards compatibility a hindering factor in that regard?

  • @clbgrmn
    @clbgrmn 6 лет назад

    I'm very much like you in that inline/higher order functions make me uncomfortable. I don't feel that they add a ton of value for how much readability they detract. But, that's the way the industry is now, so I think it'd be awesome if you keep using them consistently so we can all get more comfortable with them :)

  • @lauriejmusic
    @lauriejmusic 6 лет назад

    for those who also have performance concerns when using these functions for animation, there's fast.js :) github.com/codemix/fast.js

  • @jNRoss123
    @jNRoss123 2 года назад

    I love the way you teach code! Where have you been my coding lifetime! Can you do a short video on sorting an array by the price of an item, please?

  • @novictim
    @novictim 4 года назад

    I love how you are passionate about programming :)

  • @tomercohen3968
    @tomercohen3968 6 лет назад

    how did you download javascript becuse im trying and i cant do it for some ressone it will be relly helpful

  • @kingharvest00
    @kingharvest00 5 лет назад

    Thanks for the videos. My brain hurts slightly less now!

  • @gummansgubbe6225
    @gummansgubbe6225 6 лет назад

    Are there any hidden/included parallelization behind these functions?

  • @ukhando
    @ukhando 5 лет назад

    In order to remove a loop, you can put the update and show inside of the filter : particules.filter( p => {p.update();p.show();return !p.finished();});

    • @ukhando
      @ukhando 5 лет назад

      And the sums can be particules.reduce((a,p) => {a.x+=p.x;a.y+=p.y;return a;}, {x:0,y:0});

  • @curt8806
    @curt8806 6 лет назад

    You’re the best glad we got back to graphical stuff. It’s much more fun to watch.

  • @jaaaaaaaaaaaaaaaaaaa
    @jaaaaaaaaaaaaaaaaaaa 6 лет назад

    Would love to see you code the game Ballz! It would be interesting to see how you'd go about it

  • @uzroot
    @uzroot 5 лет назад

    Watched all series in one start ) Thank you for such a greate tutorial )

  • @DebanjanKarmakar
    @DebanjanKarmakar 6 лет назад

    can you please make a tutorial on kepler's law

  • @mussatawerghi9269
    @mussatawerghi9269 5 лет назад

    I am the number 500 liked .. Thank you ..

  • @okoiful
    @okoiful 6 лет назад +9

    That habit of using short var names is really a bad one... you should try to not do it :)

  • @colinwilliams3459
    @colinwilliams3459 5 лет назад

    When I make my particle system, the particles don't move, the update function just changes the position of the next particle that is created. Any ideas?
    Edit: Nevermind I got it, I forgot to make sure it was redrawing the paricle, rookie mistake (forgive me I'm only 12)

  • @andrewkiminhwan
    @andrewkiminhwan 6 лет назад

    such enthusiasm, bravo!

  • @ridespirals
    @ridespirals 6 лет назад

    one liners don't literally need to be on one line, put some line breaks in there :)

  • @jakubkurdziel2407
    @jakubkurdziel2407 4 года назад

    You are my hero, i looked for this for weeks

  • @rolerasi1
    @rolerasi1 6 лет назад

    what text editor you use?

  • @meeshoot3850
    @meeshoot3850 6 лет назад

    Super vidéo, merci :)

  • @RafaKoodziejczyk
    @RafaKoodziejczyk 6 лет назад

    You could also update and show in filter function all in one go?

  • @AngelGonzalezM
    @AngelGonzalezM 6 лет назад

    Perfect!!!

  • @WhoCarrot
    @WhoCarrot 6 лет назад

    I've learned so much from your videos! Do you reply to all incoming emails?

    • @TheCodingTrain
      @TheCodingTrain  6 лет назад +1

      I try to but I admit I miss quite a few and/or reply very late!

    • @WhoCarrot
      @WhoCarrot 6 лет назад

      Quite understandable, thanks for trying though & keep up the brilliant work!

  • @EfroimRosenberg
    @EfroimRosenberg 6 лет назад +5

    You could have made the forEach a oneliner: particles.forEach(p => p.update() || p.show()) ;

    • @TheCodingTrain
      @TheCodingTrain  6 лет назад +1

      Thanks for the tip!

    • @Sworn973
      @Sworn973 6 лет назад +3

      If for some reason the update returns TRUE the show wont run

    • @EfroimRosenberg
      @EfroimRosenberg 6 лет назад

      That's actually expected behavior for a logical OR operator, as it's evaluated left to right, if the left side is true it'll short-circuit the right side and won't test it, the same is for an AND operator if the left side is evaluated to false.

    • @Sworn973
      @Sworn973 6 лет назад +1

      Exactly, so using OR condition just to try run 2 line of code instead of using {} isn't a good idea, at leas I would not use it.

    • @EfroimRosenberg
      @EfroimRosenberg 6 лет назад +1

      That's correct, I was merely pointing out that there is a way to condense the code if you really wanted to and knew the result of the expressions...

  • @ARQAlejandroM
    @ARQAlejandroM 6 лет назад +1

    Ok now I'm so obsessed with this but don't even know why... Thanks!!!

  • @charbelsarkis3567
    @charbelsarkis3567 6 лет назад

    Was there no live stream last friday

  • @AleksanderFimreite
    @AleksanderFimreite 6 лет назад

    Thanks. It's nice to know about these array functions, so that I know what is going on. But I never write them myself when I write code! I try to write code in a way that could be written simularly in ANY other language. And this way is not always available.
    I also find I want to use the integer from for loop often. If I have multiple arrays of the same length, keeping the data separate. And I want to access data[i] and loadedFileData[i] in the same loop. So I often use the default for-loops all the time to stay consistant.

    • @AleksanderFimreite
      @AleksanderFimreite 6 лет назад

      It makes them a lot more flexible at the very least. It's nice to know about these parameters. I haven't seen any parameters show up for functions like these in Visual Studio. So as far as I know, you can't easiliy check what the parameter is actually referencing if someone named it poorly.

    • @TheCodingTrain
      @TheCodingTrain  6 лет назад

      Thank you for this helpful feedback!

  • @iakanoe
    @iakanoe 6 лет назад

    You could have done particles.fill(() => new Particle());
    in the line where you did
    particles.fill().map(......);
    Would this work? Was fill() a higher order function?

    • @iakanoe
      @iakanoe 6 лет назад

      I'm asking because you didn't explicitly say if fill is a higher order function in its explanation video, and I cba to Google it lol

    • @TheCodingTrain
      @TheCodingTrain  6 лет назад +2

      I believe fill() is not a higher order function.

    • @iakanoe
      @iakanoe 6 лет назад

      ok then, didn't really check heh. could have been better that way however

  • @eansengchang6840
    @eansengchang6840 6 лет назад

    This is probably not the greatest place to submit a coding challenge idea, but I just thought about it. Try making reversi?

    • @kenhaley4
      @kenhaley4 6 лет назад

      Hmm. That would be a great machine learning project.

  • @mkaufmandev
    @mkaufmandev 6 лет назад

    Another good challenge too would be doing the same thing using pure Canvas. I didn’t try, and maybe I’m missing something about P5, but I’m guessing this approach would be sluggish on older mobile devices.

  • @Nitinsingh-uj1gw
    @Nitinsingh-uj1gw 6 лет назад

    first