Functional Programming with
     Streams in node.js
Who Am I?

  HP webOS Framework Team

  @CrabDude (Twitter, Github, etc…)

  Organizer BayNode & Dallas.node

  Author:

    trycatch – async try/catch in node.js

    stepup – step node.js control flow + trycatch

    tiki – in-browser package manager
The Problem
     VFS         VFS           VFS

hermesClient hermesClient hermesClient

              hermesBuild        3rd Party API


    hermes      hermesDeploy



   Ares
  (browser)
The Goal

  Don’t buffer any data

  Don’t write to disk

  Minimize latency

  Allow reuse of functionality (e.g., readdir)

  Functionality may be local or remote (e.g., getFolder)
Why node.js?

“Node's goal is to provide an easy way to build
  scalable network programs.”
                                    – nodejs.org
node.js === JavaScript

JavaScript is the future of the web,
  and code reuse is its unfair advantage.
Functional Programming with Streams in node.js
Basics

  EventEmitter

  Stream

  Traditional streams (aka non-node.js lazy streams)
EventEmitter: Example
EventEmitter: Extend
Streams

“Streams are to time as arrays are to space.”
              – Jed Schmidt @ JSConf.eu/2010
Readable Streams

  Events: data, end, error, close

  Methods: pause, resume, end, destroy
Writable Streams

  Events: drain, error, close, pause, resume

  Methods: write, end, destroy
readable.pipe(writeable)

Readable streams can be piped to writable streams
  (and vice-versa!)

  on(‘data’) => write()

  on(‘end’) => end()

  on(‘drain’) => resume()

  on(‘close’) => destroy()

  on(‘error’) => on(‘error’)

  on(‘pause’) => pause()
readable.pipe(writeable): Example
Lazy Streams

Streams in functional programming…
Lazy Streams: Higher Order Functions

A higher-order function is a function that does at least
  one of the following:

  Take one or more functions as input
  output a function
Lazy Streams: Lists
Got All That?
Hermes: Revisited
     FTP        Dropbox        Box.net   Filesystem

hermesFtp hermesDropbox hermesBox hermesFilesystem

               hermesBuild                 3rd Party API

                          hermesDeploy

     Ares
   (browser)
Still Got All That?
We need…

We need to recursively zip a directory…

  Let’s start with abstract implementation non-specific
   functional versions of…

    Filter paths from readdir

    Filter directories from paths

    Filter files from paths

    Recursively return all file paths in a directory
Functional Programming with Streams in node.js
We need…

  More “abstract implementation non-specific functional
   versions” (errr…) of…

    ls (aka readdir)

    getFile (aka readFile)

    getFolder (stream of gets)
Functional Programming with Streams in node.js
We need…

  Yet More AINSFV of...

    Zip a stream of files

    Allow node.js streams to be piped to lazy streams

       pipe(): lazy stream => node.js stream

       pump(): node.js stream => lazy stream
Functional Programming with Streams in node.js
Almost there!
Now we can…

Zip a folder up using an arbitrary…

  getFile / readFile

  ls / readdir



… And stream the response! Yeeeeehaw!
Functional Programming with Streams in node.js
Sweeeeeet.
Now What?

Since all the implementation specific functions
  are passed in, we can…
Port this to the browser!
Say whaaat?

node-browserify by @Substack does a lot of this already

    Stream, Buffer, Path, etc…

We need…

    Complete buffer implementation (browser has
     TypedArrays & ArrayBuffer)

    fs (browser has FileSystem API)

    zlib…

       inflate(), deflate(), gzip(), gunzip(), unzip()
Stay Tuned.

I’m currently porting the fs and buffer modules to
  the browser, and eventually zlib.

http://github.com/crabdude/fs-browserify
http://github.com/crabdude/buffer-browserify
http://github.com/crabdude/zlib-browserify
We’re HIRING!
            webOS Framework Team
If you think this is pretty cool…
   Or you think I’M pretty cool…

Stop by the Enyo booth or talk to me afterwards.
Thank you.

        adam.crabtree@palm.com
https://github.com/Gozala/streamer/blob/master/readme.js
http://en.wikipedia.org/wiki/Higher-order_function
https://github.com/mikeal/request
http://felixge.s3.amazonaws.com/11/nodejs-streams.pdf
https://github.com/substack/node-browserify

Functional Programming in JavaScript:
http://igstan.ro/posts/2011-05-02-understanding-monads-with-
  javascript.html
http://ndc2011.macsimum.no/mp4/Day2%20Thursday/
  Track6%201500-1600.mp4
http://drboolean.tumblr.com/
http://osteele.com/sources/javascript/functional/

More Related Content

PPTX
Functional node.js
PPT
Node.js: CAMTA Presentation
PDF
Introduction to performance tuning perl web applications
PDF
Os Fitzpatrick Sussman Swp
PDF
Os Alrubaie
KEY
Introduction to NodeJS with LOLCats
PPTX
Background Processing - PyCon MY 2015
PDF
Node.js for Rubists
Functional node.js
Node.js: CAMTA Presentation
Introduction to performance tuning perl web applications
Os Fitzpatrick Sussman Swp
Os Alrubaie
Introduction to NodeJS with LOLCats
Background Processing - PyCon MY 2015
Node.js for Rubists

What's hot (19)

PPT
Applied Shell Scripting - stills to time-lapse
PDF
Алексей Петров "PHP at Scale: Knowing enough to be dangerous!"
PDF
Александр Махомет "Beyond the code или как мониторить ваш PHP сайт"
KEY
Server side scripting smack down - Node.js vs PHP
PPTX
Webdevcon Keynote hh-2012-09-18
ODP
Basic testing with selenium
PPTX
HTTP::Parser::XS - writing a fast & secure XS module
PPTX
Chenli linux-kerne-community
KEY
A language for the Internet: Why JavaScript and Node.js is right for Internet...
PDF
Searching CPAN Offline
PDF
Nginx وب سروری برای تمام فصول
PPTX
A slightly advanced introduction to node.js
PDF
Web Development with Python and Django
PDF
Great Tools Heavily Used In Japan, You Don't Know.
PDF
Developer-friendly taskqueues: What you should ask yourself before choosing one
PPT
5 things MySql
PDF
Jruby a Pi and a database
KEY
A rough guide to JavaScript Performance
PDF
OSDC 2016 - DNS for Developers by Jan-Piet Mens
Applied Shell Scripting - stills to time-lapse
Алексей Петров "PHP at Scale: Knowing enough to be dangerous!"
Александр Махомет "Beyond the code или как мониторить ваш PHP сайт"
Server side scripting smack down - Node.js vs PHP
Webdevcon Keynote hh-2012-09-18
Basic testing with selenium
HTTP::Parser::XS - writing a fast & secure XS module
Chenli linux-kerne-community
A language for the Internet: Why JavaScript and Node.js is right for Internet...
Searching CPAN Offline
Nginx وب سروری برای تمام فصول
A slightly advanced introduction to node.js
Web Development with Python and Django
Great Tools Heavily Used In Japan, You Don't Know.
Developer-friendly taskqueues: What you should ask yourself before choosing one
5 things MySql
Jruby a Pi and a database
A rough guide to JavaScript Performance
OSDC 2016 - DNS for Developers by Jan-Piet Mens
Ad

Viewers also liked (20)

PDF
Writing native bindings to node.js in C++
PDF
Use of 3D Immersive Technology for the Support of Gifted Learners
PPSX
♥♥♥
PPT
Vision2music
PDF
优丽奇中国 公司手册
PDF
Goodrich Global Corporate Brochure
PPT
Digiaika - Mikä Muuttuu Markkinoinnissa
PDF
Accelerating innovation and diffusion of renewable energy technologies: techn...
PPT
Voita peli verkossa: Parempia asiakkaita verkkopalvelun optimoinnilla
PDF
CambridgeIP Webinar: Developing a fact Based IP Strategy
PDF
CambridgeIP: Case Studies Of Recent Client Engagements
PDF
Goodrich Global 产品常见问题 (地板)
PPS
Blowin In The Wind
PDF
Renewable energy and water treatment: emerging opportunities
PPT
Smart camera monitoring system
PDF
E11 Physics Evaluation Sheet
PDF
The HFA pMDI Patent Landscape: Minefield or Goldmine
PPT
Burns supper (3ºb 2012 13)
PPT
TodiCastle: villa rentals & historic hotel in Umbria
PPT
Ota sosiaalinen media tehokäyttöön
Writing native bindings to node.js in C++
Use of 3D Immersive Technology for the Support of Gifted Learners
♥♥♥
Vision2music
优丽奇中国 公司手册
Goodrich Global Corporate Brochure
Digiaika - Mikä Muuttuu Markkinoinnissa
Accelerating innovation and diffusion of renewable energy technologies: techn...
Voita peli verkossa: Parempia asiakkaita verkkopalvelun optimoinnilla
CambridgeIP Webinar: Developing a fact Based IP Strategy
CambridgeIP: Case Studies Of Recent Client Engagements
Goodrich Global 产品常见问题 (地板)
Blowin In The Wind
Renewable energy and water treatment: emerging opportunities
Smart camera monitoring system
E11 Physics Evaluation Sheet
The HFA pMDI Patent Landscape: Minefield or Goldmine
Burns supper (3ºb 2012 13)
TodiCastle: villa rentals & historic hotel in Umbria
Ota sosiaalinen media tehokäyttöön
Ad

Similar to Functional Programming with Streams in node.js (20)

PPTX
Node.js: A Guided Tour
KEY
node.js: Javascript's in your backend
ODP
Asynchronous I/O in NodeJS - new standard or challenges?
PDF
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
PPTX
TypeScript - Silver Bullet for the Full-stack Developers
KEY
SD, a P2P bug tracking system
PPTX
Introduction to node.js GDD
PPTX
Clustered PHP - DC PHP 2009
PDF
Node.js - async for the rest of us.
PDF
RuG Guest Lecture
PDF
Nodejs a-practical-introduction-oredev
PDF
PuppetDB: Sneaking Clojure into Operations
PPT
Node js beginner
PDF
Docker Tips And Tricks at the Docker Beijing Meetup
PDF
Developing IT infrastructures with Puppet
PDF
Demo 0.9.4
KEY
Node.js - A practical introduction (v2)
PPTX
Java Hates Linux. Deal With It.
PDF
Erik Skytthe - Monitoring Mesos, Docker, Containers with Zabbix | ZabConf2016
PPTX
Everything you wanted to know about writing async, concurrent http apps in java
Node.js: A Guided Tour
node.js: Javascript's in your backend
Asynchronous I/O in NodeJS - new standard or challenges?
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
TypeScript - Silver Bullet for the Full-stack Developers
SD, a P2P bug tracking system
Introduction to node.js GDD
Clustered PHP - DC PHP 2009
Node.js - async for the rest of us.
RuG Guest Lecture
Nodejs a-practical-introduction-oredev
PuppetDB: Sneaking Clojure into Operations
Node js beginner
Docker Tips And Tricks at the Docker Beijing Meetup
Developing IT infrastructures with Puppet
Demo 0.9.4
Node.js - A practical introduction (v2)
Java Hates Linux. Deal With It.
Erik Skytthe - Monitoring Mesos, Docker, Containers with Zabbix | ZabConf2016
Everything you wanted to know about writing async, concurrent http apps in java

Recently uploaded (20)

PDF
CloudStack 4.21: First Look Webinar slides
PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PDF
Getting started with AI Agents and Multi-Agent Systems
PDF
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
PPTX
observCloud-Native Containerability and monitoring.pptx
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
A novel scalable deep ensemble learning framework for big data classification...
PPTX
Tartificialntelligence_presentation.pptx
PDF
Developing a website for English-speaking practice to English as a foreign la...
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PPTX
Modernising the Digital Integration Hub
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PPT
Module 1.ppt Iot fundamentals and Architecture
PDF
A review of recent deep learning applications in wood surface defect identifi...
PDF
Getting Started with Data Integration: FME Form 101
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
CloudStack 4.21: First Look Webinar slides
WOOl fibre morphology and structure.pdf for textiles
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
Getting started with AI Agents and Multi-Agent Systems
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
observCloud-Native Containerability and monitoring.pptx
Assigned Numbers - 2025 - Bluetooth® Document
A contest of sentiment analysis: k-nearest neighbor versus neural network
A novel scalable deep ensemble learning framework for big data classification...
Tartificialntelligence_presentation.pptx
Developing a website for English-speaking practice to English as a foreign la...
Univ-Connecticut-ChatGPT-Presentaion.pdf
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
Modernising the Digital Integration Hub
Final SEM Unit 1 for mit wpu at pune .pptx
Module 1.ppt Iot fundamentals and Architecture
A review of recent deep learning applications in wood surface defect identifi...
Getting Started with Data Integration: FME Form 101
sustainability-14-14877-v2.pddhzftheheeeee
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf

Functional Programming with Streams in node.js

  • 1. Functional Programming with Streams in node.js
  • 2. Who Am I?   HP webOS Framework Team   @CrabDude (Twitter, Github, etc…)   Organizer BayNode & Dallas.node   Author:   trycatch – async try/catch in node.js   stepup – step node.js control flow + trycatch   tiki – in-browser package manager
  • 3. The Problem VFS VFS VFS hermesClient hermesClient hermesClient hermesBuild 3rd Party API hermes hermesDeploy Ares (browser)
  • 4. The Goal   Don’t buffer any data   Don’t write to disk   Minimize latency   Allow reuse of functionality (e.g., readdir)   Functionality may be local or remote (e.g., getFolder)
  • 5. Why node.js? “Node's goal is to provide an easy way to build scalable network programs.” – nodejs.org
  • 6. node.js === JavaScript JavaScript is the future of the web, and code reuse is its unfair advantage.
  • 8. Basics   EventEmitter   Stream   Traditional streams (aka non-node.js lazy streams)
  • 11. Streams “Streams are to time as arrays are to space.” – Jed Schmidt @ JSConf.eu/2010
  • 12. Readable Streams   Events: data, end, error, close   Methods: pause, resume, end, destroy
  • 13. Writable Streams   Events: drain, error, close, pause, resume   Methods: write, end, destroy
  • 14. readable.pipe(writeable) Readable streams can be piped to writable streams (and vice-versa!)   on(‘data’) => write()   on(‘end’) => end()   on(‘drain’) => resume()   on(‘close’) => destroy()   on(‘error’) => on(‘error’)   on(‘pause’) => pause()
  • 16. Lazy Streams Streams in functional programming…
  • 17. Lazy Streams: Higher Order Functions A higher-order function is a function that does at least one of the following:   Take one or more functions as input   output a function
  • 20. Hermes: Revisited FTP Dropbox Box.net Filesystem hermesFtp hermesDropbox hermesBox hermesFilesystem hermesBuild 3rd Party API hermesDeploy Ares (browser)
  • 21. Still Got All That?
  • 22. We need… We need to recursively zip a directory…   Let’s start with abstract implementation non-specific functional versions of…   Filter paths from readdir   Filter directories from paths   Filter files from paths   Recursively return all file paths in a directory
  • 24. We need…   More “abstract implementation non-specific functional versions” (errr…) of…   ls (aka readdir)   getFile (aka readFile)   getFolder (stream of gets)
  • 26. We need…   Yet More AINSFV of...   Zip a stream of files   Allow node.js streams to be piped to lazy streams   pipe(): lazy stream => node.js stream   pump(): node.js stream => lazy stream
  • 29. Now we can… Zip a folder up using an arbitrary…   getFile / readFile   ls / readdir … And stream the response! Yeeeeehaw!
  • 32. Now What? Since all the implementation specific functions are passed in, we can…
  • 33. Port this to the browser!
  • 34. Say whaaat? node-browserify by @Substack does a lot of this already   Stream, Buffer, Path, etc… We need…   Complete buffer implementation (browser has TypedArrays & ArrayBuffer)   fs (browser has FileSystem API)   zlib…   inflate(), deflate(), gzip(), gunzip(), unzip()
  • 35. Stay Tuned. I’m currently porting the fs and buffer modules to the browser, and eventually zlib. http://github.com/crabdude/fs-browserify http://github.com/crabdude/buffer-browserify http://github.com/crabdude/zlib-browserify
  • 36. We’re HIRING! webOS Framework Team If you think this is pretty cool… Or you think I’M pretty cool… Stop by the Enyo booth or talk to me afterwards.
  • 37. Thank you. [email protected] https://github.com/Gozala/streamer/blob/master/readme.js http://en.wikipedia.org/wiki/Higher-order_function https://github.com/mikeal/request http://felixge.s3.amazonaws.com/11/nodejs-streams.pdf https://github.com/substack/node-browserify Functional Programming in JavaScript: http://igstan.ro/posts/2011-05-02-understanding-monads-with- javascript.html http://ndc2011.macsimum.no/mp4/Day2%20Thursday/ Track6%201500-1600.mp4 http://drboolean.tumblr.com/ http://osteele.com/sources/javascript/functional/