question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Integration into the default types

See original GitHub issue

Hi

I like the functional, lazy style of thinking very much, so I was quite excited when I first heard of Lazy.js!

There is one drawback though which is not really a fault of Lazy.js but rather that JS is not designed as lazy. Frequently there is a rather simple task, like I want to uppercase a string:

(Sorry for the Coffescript; I can convert it to JS if you wish)

  upp = (s) ->
    Lazy(s).toUpperCase().toString()

My code becomes more type conversion than anything else.

This would be all fine if I could just ignore the normal functions and always expect lazy data types as arguments/return value. I can not however because most libraries (and bits of existing code in my projects) expect that the native non-lazy types are used.

Here is another example; let’s say I want to process an array, cast to a string and process that string and return it:

  specialString = (v) ->
    s__ = Lazy v
      .map (x) -> x+1
      .toString()

    Lazy s__
      .substring 1, 8
      .toString()

My solution in these simple cases is normally to fall back to the native functions or underscore; I don’t think this is a very good solution and I think it would be much nicer if I just could write something like this and have support for both – code that is not written explicitly for lazy.js and code that is.

  upp = (s) -> s.toUpperCase()

  specialString = (v) ->
    v.map (x) -> x+1
     .toString()
     .substring 1,8

I realize that there are much better ways to implement the snippets above, but I hope they they serve to illustrate my observations.

I thought about this problem but I could not really find a solution, so I thought I’d put this problem out here and ask if we maybe can come up with a way to better integrate those two worlds.

Despite of this I’ve now used lazy.js for a while productively and it is really cool! Thanks for the hard work guys!

Issue Analytics

  • State:open
  • Created 9 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
koraacommented, Feb 13, 2017

Hi!

So I am again evaluating lazy.js. The good news is I can now basically say “my function takes and returns es6 iterators” and that is working pretty well. Lazy JS is an es6 iterator, so returning that works fine. The problem is: I still need the cast to lazy JS.

I wrote my own lib based on es6 iterators – just to get a feel for the problem:

  var {isPlainObject} = lodash;

  function iter(seq){
    if (isPlainObject(seq)) {
      function* pairs() {
        for (var k in seq) {
          yield [k, seq[k]];
        }
      };
      return pairs();
    } else {
      return seq[Symbol.iterator]();
    }
  };

  function* map(seq, f) {
    for (var k of iter(seq)) {
      yield f(k);
    }
  }

  function each(seq, f) {
    for (var nul of map(seq, f));
  }

I usually import that with something like: var {iter, map, each} = itertools; I also used my own version of the Lazy() function – I called it iter() (it’s a no-op except for objects).

But I call that implicitly map() and each(), so when I use those functions I can avoid the extra call to iter(). Would it be possible to do something similar in lazy.js?

I mean, provide functions like Lazy.map(iter, function)?

0reactions
koraacommented, Feb 13, 2017
Read more comments on GitHub >

github_iconTop Results From Across the Web

Choose an API Gateway API integration type
You choose an API integration type according to the types of integration endpoint you work with and how you want data to pass...
Read more >
Integration Services Data Types - Microsoft Learn
Integration Services includes a complete set of numeric data types, so that you can match the data type closely to the size of...
Read more >
Configuration types for integration servers - IBM
Integration servers require two types of resources to run and provide an integration that you develop: one or more BAR files that contain...
Read more >
TypeScript: JavaScript With Syntax For Types.
TypeScript is JavaScript with syntax for types. TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at...
Read more >
Datatype Default Values - Informatica Documentation
If the Integration Service cannot determine the start value of a variable by any other means, it uses a default value for the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found