All Projects → sjsyrek → lazy-linked-lists

sjsyrek / lazy-linked-lists

Licence: other
Lazy and infinite linked lists for JavaScript.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to lazy-linked-lists

Libft
42 library of basic C functions - queues, lists, memory operations and more 😄
Stars: ✭ 21 (+50%)
Mutual labels:  linked-list
AlgoDaily
just for fun
Stars: ✭ 118 (+742.86%)
Mutual labels:  linked-list
algorithm coding
推荐算法、相似度算法、布隆过滤器、均值算法、一致性Hash、数据结构、leetcode练习
Stars: ✭ 30 (+114.29%)
Mutual labels:  linked-list
Competitive Programming
Contains solutions and codes to various online competitive programming challenges and some good problems. The links to the problem sets are specified at the beginning of each code.
Stars: ✭ 65 (+364.29%)
Mutual labels:  linked-list
infinite view pager
📜Infinite View Pager widget for Flutter
Stars: ✭ 26 (+85.71%)
Mutual labels:  infinite-lists
react-infinite-scroll-list
Manage infinite list with the IntersectionObserver API
Stars: ✭ 20 (+42.86%)
Mutual labels:  infinite-lists
needle
📌📚 An extensive standalone data structure library for JavaScript.
Stars: ✭ 25 (+78.57%)
Mutual labels:  linked-list
Data-Structures-and-Algorithms
Data Structures and Algorithms implementation in Python
Stars: ✭ 31 (+121.43%)
Mutual labels:  linked-list
InterviewBit
Collection of solution for problems on InterviewBit
Stars: ✭ 77 (+450%)
Mutual labels:  linked-list
flist
Modern Fortran Linked List
Stars: ✭ 27 (+92.86%)
Mutual labels:  linked-list
Data-Structures-Algorithms-Handbook
A series of important questions with solutions to crack the coding interview and ace it!
Stars: ✭ 30 (+114.29%)
Mutual labels:  linked-list
Java
"Always choose a lazy person to get the job done. A lazy person will always find the easy way out."
Stars: ✭ 16 (+14.29%)
Mutual labels:  linked-list
Data-Structures-and-Algorithms
Implementation of various Data Structures and algorithms - Linked List, Stacks, Queues, Binary Search Tree, AVL tree,Red Black Trees, Trie, Graph Algorithms, Sorting Algorithms, Greedy Algorithms, Dynamic Programming, Segment Trees etc.
Stars: ✭ 144 (+928.57%)
Mutual labels:  linked-list
deque
JavaScript implementation of a double-ended queue
Stars: ✭ 17 (+21.43%)
Mutual labels:  linked-list
react-bidirectional-infinite-scroll
Bidirectional infinite scroll written using react
Stars: ✭ 31 (+121.43%)
Mutual labels:  infinite-lists
LinkedList
An implementation of the Doubly Linked List in C++
Stars: ✭ 16 (+14.29%)
Mutual labels:  linked-list
geeks-for-geeks-solutions
✅ My own Amazon, Microsoft and Google SDE Coding challenge Solutions (offered by GeeksForGeeks).
Stars: ✭ 246 (+1657.14%)
Mutual labels:  linked-list
Data-structures
Data Structures in Java
Stars: ✭ 13 (-7.14%)
Mutual labels:  linked-list
adif
用标准c语言开发的常用数据结构和算法基础库,作为应用程序开发接口基础库,为编写高性能程序提供便利,可极大地缩短软件项目的开发周期,提升工程开发效率,并确保软件系统运行的可靠性、稳定性。
Stars: ✭ 33 (+135.71%)
Mutual labels:  linked-list
Algorithms
✨ a bunch of algorithms in a bunch of languages ✨
Stars: ✭ 55 (+292.86%)
Mutual labels:  linked-list

Lazy and infinite linked lists for JavaScript

license build status test coverage bitHound score dependencies status devDependencies status dependency status style semantic-release CII Best Practices

NPM

A spectre is haunting ECMAScript—the spectre of tail call optimization. — Karl Marxdown

About

This library is adapted from maryamyriameliamurphies.js with several modifications. It includes functions for creating both eagerly and lazily-evaluated linked list data structures, including infinite lists, and a core subset of functions for working with them. Unlike maryamyriameliamurphies.js, however, lazy-linked-lists does not implement function currying, partial application, or type checking. It does, however, implement most of the standard Haskell type classes as instance methods, and it also implements the ES2015 iteration protocols, so you can use them in for...of loops or otherwise as regular iterators.

The lazy lists provided by this library are implemented using the new Proxy API in ES2015. Briefly, the lists returned from most of the list constructors are actually hidden behind proxy objects that trap references to their "tail" property, so that list elements, produced by an ES2015 generator function, are only evaluated on demand. Obviously, if you request the entire tail (by, for example, calling the length() function on a list), then the entire tail will be evaluated. You will want to avoid doing that with infinite lists.

Note that as of this writing, most implementations of the ES2015 standard do not yet support proper tail calls. But support is on its way! The newest versions of node and Safari have already rolled it out, and other vendors are surely not far behind. See the top line of this compatibility chart to track the progress of the feature that will make recursively defined, fully-functional, high octane linked lists in JavaScript a reality. Until that fateful day, however, you may be limited to lists of only 10,000 elements or so.

For further details, see the documentation for maryamyriameliamurphies.js.

Try it now with Tonic

Examples

Linked list, eagerly evaluated:

const lst = list(1,2,3,4,5,6,7,8,9,10);
lst.valueOf(); // => '[1:2:3:4:5:6:7:8:9:10:[]]'

Linked list, lazily evaluated:

const lst = listRange(1, 10);
lst.valueOf(); // => '[1:2:3:4:5:6:7:8:9:10:[]]'

Linked list, lazily evaluated with a user-defined step function:

const lst1 = listRangeBy(0, 100, x => x + 10);
const lst2 = listRangeBy(10, 0, x => x - 1);
lst1.valueOf(); // => '[0:10:20:30:40:50:60:70:80:90:100:[]]'
lst2.valueOf(); // => '[10:9:8:7:6:5:4:3:2:1:[]]'

Iterating over a linked list:

const lst = listRange(1, 5);
for (let value of lst) { console.log(value); }
// 1
// 2
// 3
// 4
// 5

Infinite list:

const lst = listInf(1);
take(10, lst).valueOf(); // => '[1:2:3:4:5:6:7:8:9:10:[]]'
lst.valueOf(); // => RangeError: Maximum call stack size exceeded

Infinite list with a user-defined step function:

const lst = listInfBy(0, x => x + 10);
take(11, lst).valueOf(); // => '[0:10:20:30:40:50:60:70:80:90:100:[]]'

Haskell-style type class action:

const lst1 = list(1,2,3);
const lst2 = list(3,2,1);

// Eq
lst1.isEq(list(1,2,3)); // => true
lst1.isEq(lst2); // => false

// Ord
lst1.compare(lst2); // => Symbol()
lst1.compare(lst2) === LT; // => true
lst1.isLessThan(lst2); // => true
lst1.isGreaterThan(lst2); // => false

// Monoid
lst1.mappend(lst1.mempty()); // => '[1:2:3:[]]'
lst1.mappend(lst2); // => '[1:2:3:4:5:6:[]]'

// Foldable
lst1.foldr((x,y) => x * y, 1); // => 6

// Traversable
lst1.traverse(x => list(x * 10)); // => '[[10:20:30:[]]:[]]'

// Functor
lst1.fmap(x => x * 10); // => '[10:20:30:[]]'

// Applicative
const f = x => x * 10;
const fs1 = list(f);
const fs2 = list(f,f,f);
fs1.ap(lst1); // => '[10:20:30:[]]'
fs2.ap(lst1); // => '[10:20:30:10:20:30:10:20:30:[]]'

// Monad
lst1.flatMap(x => list(x * 10)); // => '[10:20:30:[]]'
lst1.then(lst2); // => '[4:5:6:4:5:6:4:5:6:[]]'
const stringify = x => list(`${x}`);
lst1.flatMap(x => list(x, x * 10, x * x)).flatMap(stringify); // => '[110122043309]'

Other fun stuff:

const lst1 = listInfBy(0, x => x + 2);
const lst2 = take(10, lst1);
const lst3 = reverse(lst2);
const lst4 = sort(lst3);
lst3.valueOf(); // => '[18:16:14:12:10:8:6:4:2:0:[]]'
lst4.valueOf(); // => '[0:2:4:6:8:10:12:14:16:18:[]]'

const lst5 = iterate(x => x * 2, 1);
const lst6 = take(10, lst5);
lst6.valueOf(); // => '[1:2:4:8:16:32:64:128:256:512:[]]'
index(lst6, 10); // => Error: *** Exception: index: range error
index(lst5, 10); // => 1024

const lst7 = repeat(3);
const lst8 = take(10, lst7);
lst8.valueOf(); // => '[3:3:3:3:3:3:3:3:3:3:[]]'
index(lst7, 100); // => 3

const lst9 = replicate(10, 3);
lst9.valueOf(); // => [3:3:3:3:3:3:3:3:3:3:[]]

const lst10 = list(1,2,3);
const lst11 = cycle(lst10);
const lst12 = take(9, lst11);
lst12.valueOf(); // => [1:2:3:1:2:3:1:2:3:[]]
index(lst11, 99); // => 1
index(lst11, 100); // => 2
index(lst11, 101); // => 3

See also the files in the example directory.

How to install and use

  • Install with npm npm install --save-dev lazy-linked-lists. Do not install this package globally.
  • If you're transpiling >=ES2015 code with Babel, put import * as lazy from 'lazy-linked-lists'; at the top of your script files.
  • Or, to pollute your namespace, import functions individually: import {listRange, listRangeBy} from 'lazy-linked-lists';.
  • Or, if you aren't transpiling (or you're old school), use node's require syntax: const lazy = require('lazy-linked-lists');.

How to develop

  • Fork this repo and clone it locally.
  • npm install to download the dependencies.
  • npm run compile to run Babel on ES2015 code in ./source and output transpiled ES5 code to ./distribution.
  • npm run lint to run ESlint to check the source code for errors.
  • npm test to run Mocha on the test code in ./test.
  • npm run cover to run nyc on the source code and generate testing coverage reports.
  • npm run clean to delete all files in ./distribution.

See also

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].