SlideShare ist ein Scribd-Unternehmen logo
1 von 16
To Infinity & Beyond!
   Protocols & Lazy Sequences in Node
      Part Deux – Sh*t Just Got Real

Bahul Neel Upadhyaya (@bahulneel)
BraveNewTalent
http://github.com/bahulneel
http://www.bravenewtalent.com
cosy.lang
    Library                           Protocols
●   Protocols                     ●   ISeq
●   Sequences Lazy & Async        ●   ISync
●   Argument length dispatch      ●   IStream
●   Tail recursion                ●   IPromise
●   Object Identity
●   Object Metadata
                npm install cosy-lang
An Example (TF-IDF)



“Tf–idf, term frequency–inverse document frequency,
is a numerical statistic which reflects how important a word is to a
document in a collection or corpus. It is often used as a weighting
factor in information retrieval and text mining.”
                                                         - Wikipedia
Words
function stripWord(word) {
    return word.replace(/[^-a-zA-Z_0-9]+/, '').toLowerCase();
}
function isWord(word) {
    return /^[-a-zA-Z_0-9]+$/.exec(word)
}
function words(string) {
    return vec(filter(isWord, map(stripWord, string.split(/ /))));
}
Term Frequencies
function tf(words) {
    var max = 0, counts, word;
    function countFeq(counts, word) {
        var newCounts = clone(counts);
        if ('undefined' === typeof newCounts[word]) newCounts[word] = 0;
        newCounts[word] += 1;
        if (newCounts[word] > max) max = newCounts[word];
        return newCounts;
    }
    counts = reduce(countFeq, {}, words);
    if (max) {
        for (word in counts) {
            if (counts.hasOwnProperty(word)) counts[word] /= max;
        }
    }
    return counts;
}
Inverse Document Frequency
idf = fn$({
      1: function (terms) {
           return idf({}, 1, terms);
      },
      3: function (freq, docCount, terms) {
           if (null === first(terms)) return null;
           function calcIdf(terms) {
               var docFreq, invDocFreq = {}, word;
               docFreq = merge(freq, first(terms));
               for (word in docFreq) {
                   if (docFreq.hasOwnProperty(word)) invDocFreq[word] = docCount/(1+docFreq[word]);
               }
               return cons(invDocFreq, idf(docFreq, docCount + 1, rest(terms)));
           }
           return lazy(terms, calcIdf);
      }
});
TF-IDF
function tfIdf(documents) {
    var theWords, terms, freq;
    terms = map(tf, map(words, documents));
    freq = idf(terms);
    function calcTfIdf(tf, idf) {
        var word, tfIdf = {};
        for (word in tf) {
            if (tf.hasOwnProperty(word)) tfIdf[word] = tf[word] * idf[word];
        }
        return tfIdf;
    }
    return map(calcTfIdf, terms, freq);
}
Making Sequences Asyncronous
    Source                          Sink
●   Takes an ISeq & ISync as    ●   Takes an IStream as it's
    it's argument                   argument
●   Extends IStream             ●   Extends ISeq & ISync
●   Registers a tick callback   ●   First returns stream.skip
    using the ISync interface       until stream emits
●   Emits first element when
                                ●   Calls tick callback when
    callback is called              stream emits
Socket IO - Server
lang.protocol.extend(lang.stream.IStream, socketServer.Socket,
       ["tap", function (socket, fn) {
             socket.on("message", function (data) {
                   fn(JSON.parse(data));
             });
       }],
       ["emit", function (socket, val) {
             socket.send(JSON.stringify(val));
       }]
  );
  function server(port, callback) {
       var io = socketServer.listen(port);
       io.sockets.on('connection', callback);
  }
Socker IO - Server
(function (lang, tfIdf, server) {
  server(1234, function (socket) {
        lang.stream.pipe(tfIdf(socket), socket);
  });
})(require('cosy-lang'),
  require('./lib/tf-idf'),
  require('./lib/socket-server').server);
SocketIO - Client
lang.protocol.extend(lang.stream.IStream, socketClient.SocketNamespace,
       ["tap", function (socket, fn) {
             socket.on("message", function (data) {
                   fn(JSON.parse(data));
             });
       }],
       ["emit", function (socket, val) {
             socket.send(JSON.stringify(val));
       }]
  );
  function client(addr, callback) {
       var io = socketClient.connect(addr);
       io.on('connect', function () {
             callback(io);
       });
  }
Socket IO - Client
client("http://localhost:1234", function (socket) {
      lang.stream.tap(socket, function (val) {
            console.log('td-idf', val);
      });
      lang.stream.pipe(documents, socket);
});
Demo



 #!
Future work
●   Queues
●   Persistent Data Structures
●   Performance
●   Graphs
●   Persistence
Links
●   Cosy
     getcosy.org
     github.com/organizations/getcosy
●   Demo
     github.com/bahulneel/cosy-lang-demo
●   Me
     @bahulneel
     github.com/bahulneel
Fin



Questions

Weitere ähnliche Inhalte

Was ist angesagt?

Wprowadzenie do technologi Big Data i Apache Hadoop
Wprowadzenie do technologi Big Data i Apache HadoopWprowadzenie do technologi Big Data i Apache Hadoop
Wprowadzenie do technologi Big Data i Apache Hadoop
Sages
 
Chapter21 separate-header-and-implementation-files
Chapter21 separate-header-and-implementation-filesChapter21 separate-header-and-implementation-files
Chapter21 separate-header-and-implementation-files
Deepak Singh
 
Ejercicios
EjerciciosEjercicios
Ejercicios
leonharo
 
Building High-Performance Language Implementations With Low Effort
Building High-Performance Language Implementations With Low EffortBuilding High-Performance Language Implementations With Low Effort
Building High-Performance Language Implementations With Low Effort
Stefan Marr
 

Was ist angesagt? (20)

Wprowadzenie do technologi Big Data i Apache Hadoop
Wprowadzenie do technologi Big Data i Apache HadoopWprowadzenie do technologi Big Data i Apache Hadoop
Wprowadzenie do technologi Big Data i Apache Hadoop
 
tokyotalk
tokyotalktokyotalk
tokyotalk
 
LCDS - State Presentation
LCDS - State PresentationLCDS - State Presentation
LCDS - State Presentation
 
Chapter21 separate-header-and-implementation-files
Chapter21 separate-header-and-implementation-filesChapter21 separate-header-and-implementation-files
Chapter21 separate-header-and-implementation-files
 
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash course
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash courseCodepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash course
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash course
 
GoでKVSを書けるのか
GoでKVSを書けるのかGoでKVSを書けるのか
GoでKVSを書けるのか
 
Herding types with Scala macros
Herding types with Scala macrosHerding types with Scala macros
Herding types with Scala macros
 
Ejercicios
EjerciciosEjercicios
Ejercicios
 
Typelevel summit
Typelevel summitTypelevel summit
Typelevel summit
 
Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...
Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...
Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...
 
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
 
Oleksandr Kutsan "Using katai struct to describe the process of working with ...
Oleksandr Kutsan "Using katai struct to describe the process of working with ...Oleksandr Kutsan "Using katai struct to describe the process of working with ...
Oleksandr Kutsan "Using katai struct to describe the process of working with ...
 
Building High-Performance Language Implementations With Low Effort
Building High-Performance Language Implementations With Low EffortBuilding High-Performance Language Implementations With Low Effort
Building High-Performance Language Implementations With Low Effort
 
Compiler basics: lisp to assembly
Compiler basics: lisp to assemblyCompiler basics: lisp to assembly
Compiler basics: lisp to assembly
 
Flux and InfluxDB 2.0
Flux and InfluxDB 2.0Flux and InfluxDB 2.0
Flux and InfluxDB 2.0
 
Mysql5.1 character set testing
Mysql5.1 character set testingMysql5.1 character set testing
Mysql5.1 character set testing
 
Yevhen Tatarynov "From POC to High-Performance .NET applications"
Yevhen Tatarynov "From POC to High-Performance .NET applications"Yevhen Tatarynov "From POC to High-Performance .NET applications"
Yevhen Tatarynov "From POC to High-Performance .NET applications"
 
C coroutine
C coroutineC coroutine
C coroutine
 
Thrfit从入门到精通
Thrfit从入门到精通Thrfit从入门到精通
Thrfit从入门到精通
 
Ns tutorial
Ns tutorialNs tutorial
Ns tutorial
 

Ähnlich wie To Infinity & Beyond: Protocols & sequences in Node - Part 2

The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)
jeffz
 
The Evolution of Async-Programming on .NET Platform (TUP, Full)
The Evolution of Async-Programming on .NET Platform (TUP, Full)The Evolution of Async-Programming on .NET Platform (TUP, Full)
The Evolution of Async-Programming on .NET Platform (TUP, Full)
jeffz
 
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
julien.ponge
 

Ähnlich wie To Infinity & Beyond: Protocols & sequences in Node - Part 2 (20)

Socket.io
Socket.ioSocket.io
Socket.io
 
Introduction to Scalding and Monoids
Introduction to Scalding and MonoidsIntroduction to Scalding and Monoids
Introduction to Scalding and Monoids
 
Meet Up - Spark Stream Processing + Kafka
Meet Up - Spark Stream Processing + KafkaMeet Up - Spark Stream Processing + Kafka
Meet Up - Spark Stream Processing + Kafka
 
Think Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJSThink Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJS
 
ITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function ProgrammingITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function Programming
 
The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)
 
Introduction to Spark with Scala
Introduction to Spark with ScalaIntroduction to Spark with Scala
Introduction to Spark with Scala
 
The Evolution of Async-Programming on .NET Platform (TUP, Full)
The Evolution of Async-Programming on .NET Platform (TUP, Full)The Evolution of Async-Programming on .NET Platform (TUP, Full)
The Evolution of Async-Programming on .NET Platform (TUP, Full)
 
Reactive Programming Patterns with RxSwift
Reactive Programming Patterns with RxSwiftReactive Programming Patterns with RxSwift
Reactive Programming Patterns with RxSwift
 
Hadoop + Clojure
Hadoop + ClojureHadoop + Clojure
Hadoop + Clojure
 
Hw09 Hadoop + Clojure
Hw09   Hadoop + ClojureHw09   Hadoop + Clojure
Hw09 Hadoop + Clojure
 
Server side JavaScript: going all the way
Server side JavaScript: going all the wayServer side JavaScript: going all the way
Server side JavaScript: going all the way
 
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
 
Interpreter Case Study - Design Patterns
Interpreter Case Study - Design PatternsInterpreter Case Study - Design Patterns
Interpreter Case Study - Design Patterns
 
Improving Correctness with Types
Improving Correctness with TypesImproving Correctness with Types
Improving Correctness with Types
 
Time for Functions
Time for FunctionsTime for Functions
Time for Functions
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
 
Solr @ Etsy - Apache Lucene Eurocon
Solr @ Etsy - Apache Lucene EuroconSolr @ Etsy - Apache Lucene Eurocon
Solr @ Etsy - Apache Lucene Eurocon
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)
 
JS everywhere 2011
JS everywhere 2011JS everywhere 2011
JS everywhere 2011
 

To Infinity & Beyond: Protocols & sequences in Node - Part 2

  • 1. To Infinity & Beyond! Protocols & Lazy Sequences in Node Part Deux – Sh*t Just Got Real Bahul Neel Upadhyaya (@bahulneel) BraveNewTalent http://github.com/bahulneel http://www.bravenewtalent.com
  • 2. cosy.lang Library Protocols ● Protocols ● ISeq ● Sequences Lazy & Async ● ISync ● Argument length dispatch ● IStream ● Tail recursion ● IPromise ● Object Identity ● Object Metadata npm install cosy-lang
  • 3. An Example (TF-IDF) “Tf–idf, term frequency–inverse document frequency, is a numerical statistic which reflects how important a word is to a document in a collection or corpus. It is often used as a weighting factor in information retrieval and text mining.” - Wikipedia
  • 4. Words function stripWord(word) { return word.replace(/[^-a-zA-Z_0-9]+/, '').toLowerCase(); } function isWord(word) { return /^[-a-zA-Z_0-9]+$/.exec(word) } function words(string) { return vec(filter(isWord, map(stripWord, string.split(/ /)))); }
  • 5. Term Frequencies function tf(words) { var max = 0, counts, word; function countFeq(counts, word) { var newCounts = clone(counts); if ('undefined' === typeof newCounts[word]) newCounts[word] = 0; newCounts[word] += 1; if (newCounts[word] > max) max = newCounts[word]; return newCounts; } counts = reduce(countFeq, {}, words); if (max) { for (word in counts) { if (counts.hasOwnProperty(word)) counts[word] /= max; } } return counts; }
  • 6. Inverse Document Frequency idf = fn$({ 1: function (terms) { return idf({}, 1, terms); }, 3: function (freq, docCount, terms) { if (null === first(terms)) return null; function calcIdf(terms) { var docFreq, invDocFreq = {}, word; docFreq = merge(freq, first(terms)); for (word in docFreq) { if (docFreq.hasOwnProperty(word)) invDocFreq[word] = docCount/(1+docFreq[word]); } return cons(invDocFreq, idf(docFreq, docCount + 1, rest(terms))); } return lazy(terms, calcIdf); } });
  • 7. TF-IDF function tfIdf(documents) { var theWords, terms, freq; terms = map(tf, map(words, documents)); freq = idf(terms); function calcTfIdf(tf, idf) { var word, tfIdf = {}; for (word in tf) { if (tf.hasOwnProperty(word)) tfIdf[word] = tf[word] * idf[word]; } return tfIdf; } return map(calcTfIdf, terms, freq); }
  • 8. Making Sequences Asyncronous Source Sink ● Takes an ISeq & ISync as ● Takes an IStream as it's it's argument argument ● Extends IStream ● Extends ISeq & ISync ● Registers a tick callback ● First returns stream.skip using the ISync interface until stream emits ● Emits first element when ● Calls tick callback when callback is called stream emits
  • 9. Socket IO - Server lang.protocol.extend(lang.stream.IStream, socketServer.Socket, ["tap", function (socket, fn) { socket.on("message", function (data) { fn(JSON.parse(data)); }); }], ["emit", function (socket, val) { socket.send(JSON.stringify(val)); }] ); function server(port, callback) { var io = socketServer.listen(port); io.sockets.on('connection', callback); }
  • 10. Socker IO - Server (function (lang, tfIdf, server) { server(1234, function (socket) { lang.stream.pipe(tfIdf(socket), socket); }); })(require('cosy-lang'), require('./lib/tf-idf'), require('./lib/socket-server').server);
  • 11. SocketIO - Client lang.protocol.extend(lang.stream.IStream, socketClient.SocketNamespace, ["tap", function (socket, fn) { socket.on("message", function (data) { fn(JSON.parse(data)); }); }], ["emit", function (socket, val) { socket.send(JSON.stringify(val)); }] ); function client(addr, callback) { var io = socketClient.connect(addr); io.on('connect', function () { callback(io); }); }
  • 12. Socket IO - Client client("http://localhost:1234", function (socket) { lang.stream.tap(socket, function (val) { console.log('td-idf', val); }); lang.stream.pipe(documents, socket); });
  • 14. Future work ● Queues ● Persistent Data Structures ● Performance ● Graphs ● Persistence
  • 15. Links ● Cosy getcosy.org github.com/organizations/getcosy ● Demo github.com/bahulneel/cosy-lang-demo ● Me @bahulneel github.com/bahulneel