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

Add functions to change objects to be mutable and immutable #1092

Closed
Inc0n opened this issue Apr 6, 2023 · 10 comments
Closed

Add functions to change objects to be mutable and immutable #1092

Inc0n opened this issue Apr 6, 2023 · 10 comments

Comments

@Inc0n
Copy link

Inc0n commented Apr 6, 2023

This would be effectively transient and persistent! in Clojure.
persistent! would be freeze of Janet but with the deep freeze.

For example, let's define them as mutable and freeze (shallow version).
tuple and array (mutable [1 2]) = @[1 2]
struct and dict (mutable {1 2}) = @{1 2}
string and buffer ...

And freeze in the inverse operator of mutable

This would also imply that @ is made into a macro reader character

@bakpakin
Copy link
Member

bakpakin commented Apr 7, 2023

Reasonable idea. Perhaps for symmetry the function could be called thaw or unfreeze - maybe a bit too cutesy

@sogaiu
Copy link
Contributor

sogaiu commented Jun 5, 2023

@Inc0n #1142 added by @tionis has thaw in it now, do you think that addresses the original issue?

@Inc0n
Copy link
Author

Inc0n commented Jun 5, 2023

Hi, thanks for the new patch.

Originally I was trying to do this I believe (now with thaw in place, hopefully this would work now):

(defn cons [x xs]
  (freeze
   (array/push (thaw xs) x)))

For the reason that I did not enjoy the idea passing a mutable array across my functions, however, I also wanted linked list essentially...

@Inc0n Inc0n closed this as completed Jun 5, 2023
@CosmicToast
Copy link
Contributor

Couldn't you just do [;xs (freeze x)] instead?

@sogaiu
Copy link
Contributor

sogaiu commented Jun 6, 2023

Nice idea with the splice 👍

Perhaps though to emulate cons one might want the order to be [(freeze x) ;xs]?

Thinking on it a bit more, if we don't know the details of xs though (e.g. it had multiple levels with some mutable bits living below the "top"), would we not need to freeze xs?

$ janet
Janet 1.28.0-dev-528a5163 linux/x64/gcc - '(doc)' for help
repl:1:> (def xs [:a [:c @{:a 1}]])
(:a (:c @{:a 1}))
repl:2:> [:x ;xs]
(:x :a (:c @{:a 1}))

@Inc0n
Copy link
Author

Inc0n commented Jun 6, 2023

Couldn't you just do [;xs (freeze x)] instead?

For my problem, this is a much better solution.

And for the details of the xs in my case.
Really, I was trying to make alist happen (Or a list of structs). As data structure characteristics, I was looking for:

  • maintain insertion order, i.e. newest always at last or first, second newest always second or second to last
  • adding new element is done without mutating the last list

It's screaming linked-list isn't it :)

And would the corresponding accessors looking like the following, if I were to use [(freeze x) ;xs]?

(def car [[p & more]] p)
(def cdr [[p & more]] more)

@CosmicToast
Copy link
Contributor

And would the corresponding accessors ...

Yup.
Just be careful with first/last semantics, but 5 minutes in a REPL clears those up.

@sogaiu
Copy link
Contributor

sogaiu commented Jun 6, 2023

And would the corresponding accessors looking like the following, if I were to use [(freeze x) ;xs]?

Those seem like they could work, but...

[(freeze x) ;xs] looks like it would be a tuple of sorts.

Wouldn't using first for car and (drop 1 ...) for cdr also work? It seems possible that using [p & more] here might lead to unneeded processing.

Not sure I'm following along well enough (^^;

@Inc0n
Copy link
Author

Inc0n commented Jun 7, 2023

Yup.
Just be careful with first/last semantics, but 5 minutes in a REPL clears those up.

Thank you!

@Inc0n
Copy link
Author

Inc0n commented Jun 7, 2023

Wouldn't using first for car and (drop 1 ...) for cdr also work? It seems possible that using [p & more] here might lead to unneeded processing.

Good point! Thanks sogaiu :)

Edit: between this comment and the last, it did took around 5 mins in the repl to clear these out 😆

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