Live data from Hacker News

Generalizing 'jq' and Traversal Systems using optics and standard monads

chrispenner.ca

1–10 of 102 posts

Re: Generalizing 'jq' and Traversal Systems using optics and standard monads

#2
Optics, as the OP calls it, is definitely an underestimated problem of modern computing.

So much work goes in most languages into processing nested data structures, especially in this day and age of heterogeneous APIs, and a lot of it is actually not that difficult to generalize conceptually into most languages. A few weeks ago I wrote a PoC in Python and it was remarkably easy to make a lens for traversing data structures.

Declarative optics and updates would make writing business logic a lot easier on so many domains.

Re: Generalizing 'jq' and Traversal Systems using optics and standard monads

#4

Optics, as the OP calls it, is definitely an underestimated problem of modern computing. So much work goes in most languages into processing nested data structures, especially in this day and age of heterogeneous APIs, and a lot of it is actually not that difficult to generalize conceptually into most languages. A few weeks ago I wrote a PoC in Python and it was remarkably easy to make a lens for traversing data stru…

I wonder how these "optics" compare to .NET's Language Integrated Queries (LINQ) (see https://en.wikipedia.org/wiki/Language_Integrated_Query )

Re: Generalizing 'jq' and Traversal Systems using optics and standard monads

#5

"Optics" and "lenses" seem like such bad names just to be able to make a "view data through a lens" pun.

It's not a pun, it's an analogy. When we want to see things in a different way in the real world, we literally use lenses (or more generally, optics).

Re: Generalizing 'jq' and Traversal Systems using optics and standard monads

#6

"Optics" and "lenses" seem like such bad names just to be able to make a "view data through a lens" pun.

It definitely can feel a bit strained at times, but the basic metaphor of:

- lenses “focus” on elements of a product type

- prisms “split” a sum type so that optics can work over selected branches

...feels nice when you’ve been working with it for awhile.

Re: Generalizing 'jq' and Traversal Systems using optics and standard monads

#7
This is a really exciting area. See also the Cambria project [1] and the HN discussion from yesterday [2]. See [3,4] for a great introduction to category theory for programmers--we are all indebted to Milewski / Fong / Spivak / et al. for making this topic more accessible.

[1] https://www.inkandswitch.com/cambria.html

[2] https://news.ycombinator.com/item?id=24699615

[3] https://bartoszmilewski.com/2014/10/28/category-theory-for-p...

[4] http://brendanfong.com/programmingcats.html

Re: Generalizing 'jq' and Traversal Systems using optics and standard monads

#8

Optics, as the OP calls it, is definitely an underestimated problem of modern computing. So much work goes in most languages into processing nested data structures, especially in this day and age of heterogeneous APIs, and a lot of it is actually not that difficult to generalize conceptually into most languages. A few weeks ago I wrote a PoC in Python and it was remarkably easy to make a lens for traversing data stru…

I wonder how these "optics" compare to .NET's Language Integrated Queries (LINQ) (see https://en.wikipedia.org/wiki/Language_Integrated_Query )

LINQ is definitely a precursor, and has tons of fantastic ideas. It's unfortunate that Microsoft didn't continue to invest heavily into this as it's rarely mentioned nowadays.

Re: Generalizing 'jq' and Traversal Systems using optics and standard monads

#9
Why do you need all this theory when you can use simple imperative languages?

    for staff in company.staff:
      for pet in staff.pets:
        if pet.type == 'cat':
          print(pet.name + " belongs to " + staff.name)
I don't understand why functional languages are used at all.

Re: Generalizing 'jq' and Traversal Systems using optics and standard monads

#10
post #9

Why do you need all this theory when you can use simple imperative languages? for staff in company.staff: for pet in staff.pets: if pet.type == 'cat': print(pet.name + " belongs to " + staff.name) I don't understand why functional languages are used at all.

That's more difficult to type than something like

`.company.staff | .pets | flatten | filter .type == "cat" | log(...)`

Post reply on HN