Giter VIP home page Giter VIP logo

purescript-sequences's Introduction

purescript-sequences

Build Status

Various efficient-ish sequence types for PureScript.

The implementations are based on 2-3 finger trees, as described in the paper Finger Trees: A Simple General-Purpose Data Structure, Ralf Hinze and Ross Paterson, Journal of Functional Programming 16:2 (2006) pp 197-217.

Big props also go to taku0 who did most of the initial work on this.

Documentation

You probably want one of:

This package also provides Data.FingerTree, which is what these are based on, and may be useful for implementing other data structures.

Why not just use Arrays all the time?

JavaScript's Array type is designed for use in an imperative programming environment, where anything can be mutated at any time. This means that reusing them is usually not possible. For example:

var as = [1,2,3]
var bs = as.concat([4,5,6])
bs[0] = 10

as[0] should still be 1 after executing these statements. Therefore, concat has to copy the whole array, and is therefore O(this.length + n), where n is the length of the argument.

However in PureScript, values are immutable (ignoring the FFI). So we may take advantage of this by writing functions that reuse parts of data structures where possible. Sequences are one such structure - in this case, the amortized complexity of concat is reduced to O(log(min(n1, n2))), where n1 and n2 are the lengths of the arguments.

Amortized complexities of other operations:

Native array Sequence
cons/uncons O(n) O(1)
getAt i O(1) O(log(min(i, n-i)))
setAt i O(n) O(log(min(i, n-i)))
splitAt i O(n) O(log(min(i, n-i)))

Is it faster?

Not always. For example:

insert-lots append map filter fold apply concatMap traverse sort

When is this approach not suitable?

If you are using JavaScript libraries via the FFI, and passing Arrays back and forth between PureScript and JavaScript, you might find that it's easier and more efficient to just use Arrays. Generally, JavaScript libraries will not be able to use the Seq type in this library, and so you would have to convert between Seqs and Arrays at the PS/JS boundaries. The conversion in either direction is O(n).

purescript-sequences's People

Contributors

davidharrison avatar hdgarrood avatar sardonicpresence avatar taku0 avatar telser avatar zudov avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.