Code Monkey home page Code Monkey logo

Comments (12)

CFiggers avatar CFiggers commented on July 17, 2024 1

I'm sure if there was anything really bothering me I could find a way to fix it with macros, at least to my own satisfaction. 🙂 No need to change the language, unless more people than just me would find something of that kind useful.

More on the topic of this issue, I do think it would be helpful to clarify the docs around &keys syntax.

from janet-lang.org.

sogaiu avatar sogaiu commented on July 17, 2024

Thanks for elaborating. Perhaps the text could expand on things a bit more. May be a PR would be accepted toward that end.

Note though that currently the following text exists at the bottom of the destructuring page:

Destructuring works in many places in Janet, including let expressions, function parameters, and var.

from janet-lang.org.

amano-kenji avatar amano-kenji commented on July 17, 2024

I didn't recognize that there was struct destructuring after &keys....
I thought it was a special syntax...

from janet-lang.org.

CFiggers avatar CFiggers commented on July 17, 2024

I thought the same thing!

I saw your original issue over in janet-lang/janet (agree that this is a better place for it). Before that I didn't realize that [&keys foo] was valid syntax.

This is a useful thing to know, because sometimes I need to pass the whole struct received after &keys to another function. But if I've destructured it in the main function's params, then there's no symbol bound to the whole struct and I have to manually reconstruct it in the arguments of the sub function:

(defn foo [&keys {:a apples :b bananas}]
  (print apples)
  (print bananas)
  (count-calories {:a apples :b bananas}))

This small example is already annoying, but it's much worse the more keys are in the struct.

That kinda turned me off from using &keys at all almost ever:

(defn foo [fruitmap]
  (print (fruitmap :a))
  (print (fruitmap :b))
  (count-calories fruitmap))

But it turns out, this is valid:

(defn foo [&keys fruitmap]
  (print (fruitmap :a))
  (print (fruitmap :b))
  (count-calories fruitmap))

So &keys might get more use in my Janet code moving forward.

What would be really nice is if I could have the best of both worlds:

(defn foo [&keys {:a apples :b bananas} :as fruitmap]
  (print apples)
  (print bananas)
  (count-calories fruitmap))

But I don't think Janet's defn/approach to destructuring in general currently supports this.

from janet-lang.org.

sogaiu avatar sogaiu commented on July 17, 2024

Yeah an :as sort of thing could be nice.

Not sure of the specifics though.

For reference, the fennel folks seem to do this sort of thing (inspired by Clojure perhaps?):

{:expr expr#
 :res result#
 :debugger-id ,(tostring debugger-id)
 :id id#
 &as data#}

Ignore the # and , stuff -- it's from within a macro definition (^^;

from janet-lang.org.

pepe avatar pepe commented on July 17, 2024

I see :as as Clojurism :-), as it brings another layer of magic into the restructuring. All the puns intended :-D.

from janet-lang.org.

sogaiu avatar sogaiu commented on July 17, 2024

Not sure there is really a nice solution, but IIUC ATM there is a trade-off one must make between:

  • documenting keys using destructuring in the function signature
  • use of passed struct within the function body

I think you can't have your cake and eat it too...though may be this cake is not tasty enough (^^;


Back to the topic (as CFiggers mentioned), perhaps an additional example in the docs (plus some text) that demonstrates the point would be worthwhile.

from janet-lang.org.

amano-kenji avatar amano-kenji commented on July 17, 2024

Haskell provides an enhanced destructuring syntax.

For example, in haskell, something like this is possible

(defn
  [arg1 &keys attrs@{:abc abc :bcd bcd}]
  (pp attrs)
  (pp abc)
  ...)

from janet-lang.org.

sogaiu avatar sogaiu commented on July 17, 2024

Regarding possible changes to the documentation, perhaps an additional example can be added to the end of the keyword-style arguments section along with some explanatory text.

Here's a first draft:

Note that what follows `&keys` does not have to be a 
struct and can instead be a single symbol.  The
symbol can then be used as a name within the function
body for a struct containing the passed keys and values:

(defn feline-counter
  [&keys animals]
  (if-let [n-furries (get animals :cat)]
    (printf "Awww, there are %d cats!" n-furries)
    (print "Shucks, where are my friends?")))

(feline-counter :ant 1 :bee 2 :cat 3)

from janet-lang.org.

amano-kenji avatar amano-kenji commented on July 17, 2024

That's what I meant.

from janet-lang.org.

sogaiu avatar sogaiu commented on July 17, 2024

Thanks for clarifying.

I submitted PR #214.

from janet-lang.org.

sogaiu avatar sogaiu commented on July 17, 2024

Looks like this issue may have been address by the merging of #214. Perhaps it can be closed.

from janet-lang.org.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.