npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

recursive-methods

v1.1.1

Published

A good way to learn functional programming in javascript, Inside the functions, there are no any keywords such as "function", "let", "var", "for","while", etc.

Downloads

38

Readme

Getting started

This package includes several recursive methods. The functions of them are as same as the JS native methods such as Array.sort, Array.map. The difference between the native methods and these methods is that these methods are recursive methods and with trail optimization, never worry about memory out, after using async and await, JS implements the trail optimization very well. e.g.

export const extreme = async <T>(func: (a: T, b: T) => boolean, list: List<T>): Promise<T> => {
  if (list.length <= 1) {
    return list[0];
  }
  const extremeI = async (fun: (a: T, b: T) => boolean, li: List<T>, max: T): Promise<T> => {
    await null;
    const temMax = fun(max, li[0]) ? max : li[0];
    if (li.length <= 1) {
      return temMax;
    }
    return extremeI(fun, li.slice(1), temMax);
  };
  return extremeI(func, list.slice(1), list[0]);
};

const arr = new Array(1000000).fill(0).map(() => Math.random());
(async () => {   
    console.time('s')
    console.log(await methods.extreme((a, b) => a > b,arr))
    console.timeEnd('s')
})()
// Even though the array length is over 1,000,000, it still works, it just takes time, it doesn't take memory

Installation

npm install recursive-methods --save

Usage

javascript

const recursiveMethods = require('recursive-methods');
(async () => {
  const greatest = await recursiveMethods.extreme((a, b) => a > b,[1, 2, 3, 4, 5, 6, 7, 8]);
  console.log(greatest);
})();
// 8

typescript

import { map } from 'recursive-methods';
const list = ['one', 'two', 'three', 'four', 'five'];
(async () => {
  const newList = await map((v, i) => v + String(i), lists);
  console.log(newList);
})();
// ['one0', 'two1', 'three2', 'four3', 'five4']

Methods

.fibonacci(n)

Return a fibonacci number

.fibonacci(0); .fibonacci(1); .fibonacci(2);
// Individually return 1; 1; 2;

.fibonacciArr(n)

Return a fibonacci list

.fibonacciArr(0); .fibonacciArr(1); .fibonacciArr(2);
// Individually return [1]; [1, 1]; [1, 1, 2];

.filter(func, list)

Return a filtered list

.filter(v => v > 5, [1, 2, 3, 4, 5, 6, 7, 8]);
// return [6, 7, 8]

.sort(func, list)

Return a sorted list(it's faster a bit)

.sort((a, b) => a > b, [1,4,5,2,8,2,4,6]);
// return [1,2,2,4,4,5,6,8]

.sorter(func, list)

Return a sorted list, but it has trail optimization

.sorter((a, b) => a > b,[1,4,5,2,8,2,4,6]);
// return [1,2,2,4,4,5,6,8]

.extreme(func, list)

Return an extreme value

.extreme((a, b) => a > b, [1,4,5,2,8,2,4,6]);
// return 8

.whileis(func, list)

Return the slice when the values matched

.whileis(v => v > 3,[5,6,7,8,9,3,2,1,2,8,9]);
// return [5,6,7,8,9]

.drop(func, list)

Return the rest list which dropping one specific value

.drop(v => v === 9,[5,6,7,8,9,3,2,1,2,8,9]);
// return [5,6,7,8,3,2,1,2,8,9]

.map(func, list)

Return a list that created by the returned value of the input function

.map((v, i) => `Value: ${v}, Index: ${i}`, ['value1', 'value2', 'value3']);
// return [Value: value1, Index: 0,Value: value2, Index: 1,Value: value3, Index: 2]

.isCongruence(param, param)

Compare the two values, return false or true

.isCongruence({a: [{b:'one', c: [222,'dd']}]},{a: [{b:'one', c: [222,'dd']}]});
// return true
.isCongruence({a: [{b:'one', c: [223,'dd']}]},{a: [{b:'one', c: [222,'dd']}]});
// return false
.isCongruence((a, b) => a + b,(a, b) => a + b);
// return true

.reverse(list)

Return a reversed list

.reverse([1, 2, 3, 4, 5, 6]);
// return [6, 5, 4, 3, 2, 1]

.fragment(gap, func, list)

Return a list which created by the returned value of the parameter func. The difference between function map and this function is that this function do not loop every element of the list, it depends on the specific parameter gap

.fragment(
  3, 
  (v, i) => (v[0] || 0) + (v[1] || 0) + (v[2] || 0),
  [1, 2, 3, 4, 5, 6, 7, 8]
);
// return [6, 15, 15]

.fragment(3, (v, i) => v, [22, 33, 44, 55, 66, 77, 88, 99, 12, 12, 124]);
// return [[22, 33, 44], [55, 66, 77], [88, 99, 12], [12, 124]]

.deduplicate(list)

Return a list which is deleted duplicated values

.deduplicate([1,2,3,4,4,4,2,2,2,5,5,1,2,3,6]);
// return [1,2,3,4,5,6]
.deduplicate(['aa','bb','cc','cc','cc','dd']);
// return ['aa','bb','cc','dd']
.deduplicate([{a: 2,b: 2}, {a: 3,b: 3}, {a: 2,b: 2}, {a: 4,b: 4}]);
// return [{a: 2,b: 2}, {a: 3,b: 3}, {a: 4,b: 4}]

.regenerateMatrix(matrix)

matrix is an array like

[[1,2,3],[4,5,6],[7,8,9]]

pick the column values as the new matrix's row values

.regenerateMatrix([['1','2',3],['4','5',6],['7','8',9]])
// return [['1','4','7'], ['2','5','8'],[3,6,9]]

.rotateMatrix(matrix)

rotate a matrix 90 degrees as anti-clockwise

.rotateMatrix([['1','2',3],['4','5',6],['7','8',9]])
// return [[3,6,9],['2','5','8'],['1','4','7']]