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

Proposal: Terse Chained Indices #1395

Closed
ghost opened this issue Feb 8, 2024 · 19 comments
Closed

Proposal: Terse Chained Indices #1395

ghost opened this issue Feb 8, 2024 · 19 comments

Comments

@ghost
Copy link

ghost commented Feb 8, 2024

EDIT: @pepe has a better idea, see below.

> (def lookup {:a 1 :b {:c 3}})
{:a 1 :b {:c 3}}
> (lookup :b :c)
3

That's the idea. Nested tables are common as dirt in APIs, so can stand to be streamlined; dot notation chains naturally, but tuple notation is a bit awkward without tricks. This is technically "magic", but not significantly more so than what we already have.

@sogaiu
Copy link
Contributor

sogaiu commented Feb 8, 2024

Is this similar to (get-in lookup [:b :c])?

@ghost
Copy link
Author

ghost commented Feb 8, 2024 via email

@pepe
Copy link
Member

pepe commented Feb 8, 2024

I guess there will be a performance penalty on every lookup with just one key, as the in function (under the hood, if I am not mistaken) will always need to allocate variadic params tuple.

But I like the idea. What about . macro, which works as https://github.com/janet-lang/spork/blob/master/spork/misc.janet#L396 but with in so we can (. lookup :c :d)?

@ghost
Copy link
Author

ghost commented Feb 8, 2024 via email

@sogaiu
Copy link
Contributor

sogaiu commented Feb 8, 2024

I wonder if there isn't code out there that uses . for their own purposes...

I'm also not so in to additional shortcuts (including a lot of user-defined macros) because I think it tends to increase the learning burden of the reader (which in turn can lead to additional costs for investigation and maintenance). One of the reasons I like lisp-likes is so I don't have to remember / learn so many exceptions.

I'm probably in the minority though (as someone who's not in to things like (1 @[:a :b :c]) (^^;

@pepe
Copy link
Member

pepe commented Feb 8, 2024

I feel your pain, @sogaiu <3. But dot notation is evoked by this syntax, so even you will be OK.

@pepe
Copy link
Member

pepe commented Feb 8, 2024

If it ever gets considered, that's it.

@ghost
Copy link
Author

ghost commented Feb 8, 2024 via email

@sogaiu
Copy link
Contributor

sogaiu commented Feb 8, 2024

I'm not sure of a good way to express an appropriate search -- the grep example given lacks checking for defmacro, defmacro-, and other constructs. Also, people define their own defining constructs which may then be used to define further things...

In any case, if you want to do some spelunking, this list is close to what I know about. Likely there is more stuff out there (^^;


Re: (<int> <array>) - I think this kind of thing is not that odd if one allows keyword lookup like (:a {:a 1 :b 2}) (I'm not a huge fan of that either, but it's done in at least one other lisp-like IIUC).

@ghost
Copy link
Author

ghost commented Feb 8, 2024 via email

@iacore
Copy link
Contributor

iacore commented Feb 8, 2024

I use plain syntax because the term can be put inside (set).

(def lookup @{:a 1 :b @{:c 3}})
(set ((lookup :b) :c) 4)
lookup 
# => @{:a 1 :b @{:c 4}}

This macro generates the same plain syntax.

(defmacro .
  "Recursive macro (ds key)."
  [ds & keyz]
  (reduce (fn [t key] (tuple t key)) ds keyz))

@pepe
Copy link
Member

pepe commented Feb 8, 2024

@im-not-linja I am sorry to make you sad, even as I am not sure what you meant by the accusation. Can you please elaborate? Maybe DM Zulip so as not to pollute the discussion here.

@sogaiu
Copy link
Contributor

sogaiu commented Mar 1, 2024

FWIW, there was mention from someone on one of the janet-language matrix channels that they are using . in something they are working on.

@bakpakin
Copy link
Member

bakpakin commented Mar 13, 2024

Closing as I prefer the . macro. Chained lookup is useful, but there are deeper ramifications for modifying the behavior of in and get.

@ghost
Copy link
Author

ghost commented Mar 14, 2024 via email

@pepe
Copy link
Member

pepe commented Mar 14, 2024

@im-not-linja, I am still in limbo with all the puns I stole. I could indeed be a thief in some situations, but I am usually aware of it. Could you please elaborate?

@ghost
Copy link
Author

ghost commented Mar 14, 2024 via email

@pepe
Copy link
Member

pepe commented Mar 14, 2024

I had that feeling, but I wanted to be sure. Just so you know, I will deprecate all the standalone libraries and keep only gp (as the abbreviation of Good Place), so all the names will be free to grab :–).

@ghost
Copy link
Author

ghost commented Mar 14, 2024 via email

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

4 participants