Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Lenses.alongside working so we don't traverse the depth of the tree of paths for every attribute #3

Open
bmaddy opened this issue Mar 19, 2014 · 0 comments

Comments

@bmaddy
Copy link
Contributor

bmaddy commented Mar 19, 2014

Depends on #2.

Say we have some attribute paths that look like this:

attribute :first_paragraph, [Lenses.get_predicate('.../relation'), Lenses.single, Lenses.load_model, Lenses.get_predicate('.../paragraphs'), Lenses.single]
attribute :first_footer, [Lenses.get_predicate('.../relation'), Lenses.single, Lenses.load_model, Lenses.get_predicate('.../footers'), Lenses.single]

Currently, we would make an http request to load that model once for each attribute but we would like to load that model only once. To do that we need to convert our paths into a tree structure something like this:

Lenses.get_predicate('.../relation'), Lenses.single, Lenses.load_model, Lenses.get_predicate('.../paragraphs'), Lenses.single
[
 [Lenses.get_predicate('.../relation'),
  [Lenses.single,
   [Lenses.load_model,
    [Lenses.get_predicate('.../paragraphs'),
     [Lenses.single, :first_paragraph]
    ],
    [Lenses.get_predicate('.../footers'),
     [Lenses.single, :first_footer]
    ]
   ]
  ]
 ]
]

To do this we'll need to implement == in our lens classes. Something like this:

class GetPredicate
  ...
  def ==(other_lens)
    this.class == other_lens.class && @predicate == other_lens.predicate
  end
end

Once we have taken our paths and merged their roots to form a tree, we would like to compose the lenses so we get a new lens that converts n lenses to n values. On the view side we would see something like this:

Lenses.alongside(Lenses.get_predicate_with_name(:paragraph, '.../paragraph'),
                 Lenses.get_predicate_with_name(:footer, '.../footer'))
# where get_predicate_with_name returns something like this: [:paragraph, 'some paragraph']
# alongside should give a lens that has a view that looks like this:
[[:paragraph, 'some paragraph'], [:footer, 'some footer']]

See the relevant stackoverflow question here: https://stackoverflow.com/questions/22865170/what-kind-of-lens-combinator-is-this

The type signature we want can be found here: http://hackage.haskell.org/package/lens-4.1.2/docs/Control-Lens-Lens.html#v%3aalongside

Here's a similar lens (that provides the values without the attribute names) in coffescript:
https://github.com/flazz/lens.js/blob/master/src/lens.coffee#L35-45
There's example usage at the bottom of the readme.

@bmaddy bmaddy changed the title Get Lenses.zip working so we don't traverse the depth of the tree of paths for every attribute Get Lenses.alongside working so we don't traverse the depth of the tree of paths for every attribute Apr 15, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant