GithubHelp home page GithubHelp logo

muddydixon / pp.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from voqn/pp.js

0.0 1.0 0.0 227 KB

Pseudo-Parallel, Passing-Procedure, Pretty-Promise. Asynchronous Collection & Procedure Control Flow

Home Page: voqn.github.com/pp.js

License: MIT License

pp.js's Introduction

pp.js - pianissimo -

Build Status

pp.js is called pianissimo.js, which means Pseudo-Parallel, Passing-Procedure, or Pretty-Promise. pp.js is a javascript library for Asynchronous Collection & Procedure Control Flow.

this library is inspired by async.js, JsDeferred, $.Deferred, and Promise/A. And aiming provide compatible API.

to read this library specification see Guide, Reference

Faster, Fewer Cost, Parallel multi process

Benchmark pp.js vs async.js

  • pp.js faster than async.js (more than 1.25x)
  • pp.js use fewer memory async.js (lower than 1/3)
  • pp.js work looks like parallel (see Guide/Trampolining)
  • while running pp.js process, It does not block user control as possible as.

License

Copyright (c) 2012 VoQn

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

## API Reference # Guide ## CPS (Continuation Passing Style) __pp.js__ is designed by [CPS](http://en.wikipedia.org/wiki/Continuation-passing_style), _Continuation Passing Style_, for effective Asynchronous processing.
# sync procedure
sq = (x) ->
  x * x

console.log sq 10 # return 10 * 10 -> 100 -> console.log(100) => IO output

# CPS procedure
cpsSq = (next, x) ->
  next x * x

cpsSq console.log, 10 # console.log(10 * 10) -> console.log(100) => IO output

# Async procedure
heavyProcessing = (callback, parameters) ->
  # do something (use long time, or network communication)
  # ...
  # ...
  callback error, result # when process done, result apply asynchronouse

heavyProcessing (e, r) -> # callback
  if e # receive error
    # do something
  else # process has been succeeded
    # do something
, [### parameters ###]
## Trampolining, "Pseudo-Parallel" __pp.js__ doesn't provide _true_ parallel processing. Parallel processing is a strictly pseudo. This pseudo-parallel processing on [Trampoling](http://en.wikipedia.org/wiki/Trampoline\_\(computing\)).

pp.js API are curried function.

For example, CPS sum of number array is this.

printSum = pp.foldl (next, memo, value) ->
  if typeof value isnt 'number'
    next new TypeError "\"folding\" require number, but #{typeof value}"
  else
    next null, memo + value
  return
, (error, result) ->
  console.log result
, 0 # See it! subject array has not apply!

printSum [10, 11, 12] #=> 33
printSum [1, 2, 3, 4, 5] #=> 15

Invocation type of callback, "fill" and "order"

In designing Asynchronous operetion, maybe occur a problem that dependency with each procedures.

Because solve it, pp.js provide two iteration. fill and order.

fill

fill process is ASAP (As Soon As Possible)

fireStack = []

pp.fill [
  (next) ->
    setTimeout ->
      fireStack.push '1st'
      next null, '1st'
    , 100
  , (next) ->
    setTimeout ->
      fireStack.push '2nd'
      next null, '2nd'
    , 200
  , (next) ->
    setTimeout ->
      fireStack.push '3rd'
      next null, '3rd'
    , 50
], (error, result) ->
  # result     --- ['1st', '2nd', '3rd']
  # fire_stack --- ['3rd', '1st', '2nd']

order

order process is keep invocation order.

fireStack = []

pp.order [
  (next) ->
    setTimeout ->
      fireStack.push '1st'
      next null, '1st'
    , 100
  , (next) ->
    setTimeout ->
      fireStack.push '2nd'
      next null, '2nd'
    , 200
  , (next) ->
    setTimeout ->
      fireStack.push '3rd'
      next null, '3rd'
    , 50
], (error, result) ->
  # result     --- ['1st', '2nd', '3rd']
  # fire_stack --- ['1st', '2nd', '3rd']

Difference?

pp.fill F, G, H, CALLBACK
# eval F -> eval G -> eval H -> (wait callback...) -> eval CALLBACK

pp.order F, G, H, CALLBACK
# eval F -> (wait F callback...) -> eval G -> (wait G callback...) -> ...

Why pp.fill's name is parallel but fill? Because it run all procedures and wait until all callback is filling.

pp.order is keeping its ordering. When it began run procedure, wait that callback, run next procedure. Until last.

Type

One of difference between pp.js with async.js is consisted argument format.

TimeSlice: number (integer milli-second [0 < t])

pp.TIME_SLICE provide consts for frame rate.

  • FPS_240 - 4ms
  • FPS_120 - 8ms
  • FPS_60 - 16ms
  • FPS_30 - 33ms
  • FPS_15 - 66ms
  • FPS_1 - 1s (1000ms)

Callback: function(?Error, [somethings...])

pp.js defined Callback type that is function(Error, [somethings...]).

first argument, received Error, is accepted as nullable.


Iterable: (!Array | !Object as {string: any})

pp.js defined Iterable type that is not null Array or Object.

primitive values ... undefined, null, string, boolean and number aren't accepted.


Iterator: function(callback, [somethings...])

pp.js defined Iterator type that is function(callback, [somethings...])

For example, available iterator for Array

  • function(function:next, any:value, number:index, array:iterable)
  • function(function:next, any:value, number:index)
  • function(function:next, any:value)
  • function(function:next)

for Object,

  • function(function:next, any:value, string:key, object:iterable)
  • function(function:next, any:value, string:key)
  • function(function:next, any:value)
  • function(function:next)

iterator type need continuation function for 1st argument.


Predicator: function(callback, value, [key, iterable])

pp.js defined Predicator type that is function(callback, value, [key, iterable])

Specially, predicator passing boolean result to callback.

Example

cpsIsEven = (next, value) ->
  next null, value % 2 is 0

Accumulator: function(callback, memo, value, [key, iterable])

pp.js defined Folding type that is function(callback, memo, value, [key, iterable]).

for accumulate array list.

Example

cpsAdd = (next, memo, value) ->
  next null, memo + value

pp.js's People

Contributors

ujihisa avatar

Watchers

muddydixon 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.