Menu

gitpiper

unfold javascript Code Snippet in 2024

functionarrayintermediate

Last Updated: 18 July 2024

Builds an array, using an iterator function and an initial seed value.

  • Use a while loop and Array.prototype.push() to call the function repeatedly until it returns false.
  • The iterator function accepts one argument (seed) and must always return an array with two elements ([value, nextSeed]) or false to terminate.
const unfold = (fn, seed) => { let result = [], val = [null, seed]; while ((val = fn(val[1]))) result.push(val[0]); return result; };
var f = n => (n > 50 ? false : [-n, n + 10]); unfold(f, 10); // [-10, -20, -30, -40, -50]

javascript snippet similar to unfold For You in July 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️