Coding reduce from scratch in vanilla JavaScript

แชร์
ฝัง
  • เผยแพร่เมื่อ 6 ม.ค. 2025

ความคิดเห็น •

  • @devcoffee
    @devcoffee 5 ปีที่แล้ว +2

    I love your drive man. That’s something that I admire about your content. Keep it up!

  • @TheNerdyDev
    @TheNerdyDev 5 ปีที่แล้ว +6

    Awesome video. Keep it up, mpj. Really liked the implementation that you did :)

    • @funfunfunction
      @funfunfunction  5 ปีที่แล้ว +3

      Glad you liked it!

    • @Sharpie360
      @Sharpie360 5 ปีที่แล้ว +1

      Hi prateek! (from rabbitwerks)

  • @hasratsabit2976
    @hasratsabit2976 5 ปีที่แล้ว +4

    I missed your vanilla JS tutorials. Keep them coming!

  • @jamman55
    @jamman55 5 ปีที่แล้ว +6

    Love your channel. Love the content!
    I miss your snazzy intros.

  • @Saiphes
    @Saiphes 5 ปีที่แล้ว +14

    Love this format. Miss the old coffee intro.

    • @funfunfunction
      @funfunfunction  5 ปีที่แล้ว +6

      oh, I should really make a new intro

    • @Dr3amDisturb3r
      @Dr3amDisturb3r 5 ปีที่แล้ว +2

      ​@@funfunfunction Please involve struggling with keyboard typing lul

    • @funfunfunction
      @funfunfunction  5 ปีที่แล้ว +2

      Hahahaha yes

  • @Djzaamir
    @Djzaamir 5 ปีที่แล้ว

    NIce and short, really helpful to understand what goes on under the stood. Thanks MPJ.

  • @RobinMacPhersonFilms
    @RobinMacPhersonFilms 5 ปีที่แล้ว

    Thanks for continuing this series on, MPJ! Sending many positive vibes from your newest Patreon supporter in San Francisco after watching for years ✨So pleased to realize the live-stream is actually Monday morning HERE haha 🙏🏼
    Keep up all the great work, man

    • @funfunfunction
      @funfunfunction  5 ปีที่แล้ว

      Good morning to you, Robin! Thanks for the vibes and your long time support, really means the world to me 💛🥰Morning is a trick concept on a global level, when is it, really? SF mornings is a convenient time for me to shoot on, so I ended up picking that for my time zone haha

  • @KrotUA
    @KrotUA 4 ปีที่แล้ว +2

    I missed your vanilla JS tutorials too. But I should say that you missed some part - initialAccumulatorValue (initialValue in the future) is not required parameter if there is no initialValue - first element of the array should be taken as accumulatorValue
    From MDN: InitialValue - A value to use as the first argument to the first call of the callback. If no initialValue is supplied, the first element in the array will be used and skipped. Calling reduce() on an empty array without an initialValue will throw a TypeError.

  • @DavoMkrtchyan
    @DavoMkrtchyan 5 ปีที่แล้ว

    I missed this format! Thanks for bringing it back!

  • @kanz831021
    @kanz831021 4 ปีที่แล้ว

    Thank you so much! It helped me understand Redux Reducers !!!

  • @geechai3475
    @geechai3475 ปีที่แล้ว

    Much Thanks. Helped a lot

  • @hcmlopes
    @hcmlopes 5 ปีที่แล้ว +1

    MPJ good job on these videos

  • @bachelorofcomputersciencee3804
    @bachelorofcomputersciencee3804 5 ปีที่แล้ว +1

    Relaxing content...💻💻💻👍👍👍

  • @priyankamalviya3613
    @priyankamalviya3613 5 ปีที่แล้ว

    hands down the best implementation of reduce function I saw! Now I really, thoroughly understand reduce :). Thanks a ton @mpj!!

  • @kirillpavlovskii8342
    @kirillpavlovskii8342 4 ปีที่แล้ว

    how about if the accumulator parameter is an object? or an array? that's not gonna work

  • @gaptekdev
    @gaptekdev 5 ปีที่แล้ว

    Hi, awesome video, may i know what extension you have use to get directly result (output) on editor..?

  • @gaetan5331
    @gaetan5331 5 ปีที่แล้ว

    Love this content ! keep doing cool stuff like that

  • @jocampo92
    @jocampo92 5 ปีที่แล้ว

    Hey mpj, do you have a video/resource about how you handle your overlays?

  • @davejs
    @davejs 5 ปีที่แล้ว +1

    Did you consider implementing reduce with recursion?

    • @Marcosmapf
      @Marcosmapf 5 ปีที่แล้ว

      completely doable, similar implementation difficulty, but much less performatic than the sollution he implemented since the stack would grow much faster and consume more memory

    • @davejs
      @davejs 5 ปีที่แล้ว

      @@Marcosmapf possibly, however reduce can be written using tail recursion to avoid growing the call stack and save on memory. The for-loop approach works just as well though, I'm just biased towards functional programming patterns :)

  • @AtulSingh-rq7wh
    @AtulSingh-rq7wh 5 ปีที่แล้ว

    Hey! can I get the vs code configuration? Like which extensions you prefer . I'll be really thankful.

    • @funfunfunction
      @funfunfunction  5 ปีที่แล้ว

      No extensions except Quokka. I get caught up in tool tweaking and never end up doing any real work.

  • @alanmatkorski1126
    @alanmatkorski1126 5 ปีที่แล้ว

    Before watching the video I gave it a try and I came up with this recursive curried version:
    const reduce = fn => seed => xs => xs.length == 0 ? seed : reduce(fn)(fn(seed, xs[0]))(xs.slice(1));

  • @gidmanone
    @gidmanone 5 ปีที่แล้ว

    @Fun Fun Function
    For as long as I shall dwell I shall henceforth not doubt your power.
    I humbly bow at your feet MPJ

  • @sweLogan
    @sweLogan 5 ปีที่แล้ว

    Like this basic videos format!

    • @funfunfunction
      @funfunfunction  5 ปีที่แล้ว +1

      I have an extremely exhaustive video series on generator functions, not sure if I can add much there.

    • @TheNerdyDev
      @TheNerdyDev 5 ปีที่แล้ว

      I do have a series on that. You can check my channel as well. I have also create my own async await using generators and pify. You can find that on my channel. Maybe it helps.

  • @alexandrradaev8438
    @alexandrradaev8438 5 ปีที่แล้ว +1

    Good mood to you

  • @TofuBug24
    @TofuBug24 5 ปีที่แล้ว

    Personally I prefer how the LINQ extension method for this is named: Aggregate to me that fits better the broad nature of what it does, reduce to me just has this underlying implication you are taking AWAY from the object if your doctors say i'm going to reduce your meds you'd intuitively think they mean mean you will take LESS meds but in javascript reasoning he actually means group all your meds together in one pill and take that, it just a little jarring, Calling it Aggregate while few people actually know that word once you do you understand that it doesn't matter if you are adding numbers, multiplying, concatenating strings, flattening multi dimensional arrays, etc what you are doing at a high level is aggregate potentially disparate things into a simpler value not always necessarily a single value, e.g. you are putting different fruit into fruit salad that to me just seems more honest about what it is doing

  • @Sharpie360
    @Sharpie360 5 ปีที่แล้ว

    This is a strangest techno track I've heard..

  • @Abmallim
    @Abmallim 5 ปีที่แล้ว

    next make redux ?

  • @nathancornwell1455
    @nathancornwell1455 4 ปีที่แล้ว

    I could figure out how to do a simple reducer function just like adding numbers but I am having trouble implementing a more advanced reduce function where you can reduce an array down to an object. Now, I'm never getting to sleep lol

    • @nathancornwell1455
      @nathancornwell1455 4 ปีที่แล้ว

      Probably not the greatest and most articulate but here is my implementation of reduce( ) :
      function reduce(arr,cb, initial ) {
      let result;
      let i;
      if(initial || initial === 0) {
      result = initial;
      } else {
      result = arr[0];
      }
      for(!!initial || initial === 0 ? i = 0 : i = 1; i < arr.length; i++) {
      result = cb(result, arr[i])
      }

      return result;
      }

  • @micoberss5579
    @micoberss5579 5 ปีที่แล้ว

    I did it this way:
    function reduceArray(reducerFn, array, initVal) {
    let initialValue = initVal ? initVal : 0;
    for (let i = 0; i < array.length; i++) {
    initialValue = reducerFn(initialValue, array[i]);
    }
    return initialValue;
    }
    so you dont have to pass initial value

    • @Bayo106
      @Bayo106 4 ปีที่แล้ว

      thanks. this helps

  • @vorname1485
    @vorname1485 5 ปีที่แล้ว

    Beginner: 3 hours later, **damn it, initialAccumulatorValue it is!**
    Experienced: ~5mins, either **finds good fitting name** or **I see this trickery! You'r not going to trick me into wasting hours! a straightforward initialAccumulatorValue it is!**

  • @AjitKumar-wb9bp
    @AjitKumar-wb9bp 5 ปีที่แล้ว

    @mpjme your recent video is not funny then it used to be in functional programming series

  • @adriel611
    @adriel611 4 ปีที่แล้ว

    man are you drunk?