Olical / aniseed

Neovim configuration and plugins in Fennel (Lisp compiled to Lua)
https://discord.gg/wXAMr8F
The Unlicense
606 stars 28 forks source link

`assoc` and `update` are not side effect free #131

Closed D00mch closed 1 year ago

D00mch commented 1 year ago

Functions like assoc, assoc-in from aniseed.core mutate the initial object.

Example:

(module tests
  {autoload {: assoc : update : inc}})

(def m {:a 1 :b 2})
(def m2 (assoc m :a 2))
(def m3 (update m :b inc))

m ;=> {:a 2 :b 3}

Would it be better if these functions work like in Clojure?

Olical commented 1 year ago

This is by design for efficiency. If you would like true immutable functions you will need to use a library like luafun I think (which is bundled inside aniseed already, just unused.

It's a tradeoff, but an important one within this context. aniseed.core is Clojure-like, not entirely compliant and does not aim to be so.

The way to "fix" this would be to clone every table I ever modify which would get very costly very quickly.

On Mon, 19 Dec 2022 at 09:13, Artur Dumchev @.***> wrote:

Functions like assoc, assoc-in from aniseed.core mutate the initial object.

Example:

(module tests {autoload {: assoc : update : inc}})

(def m {:a 1 :b 2}) (def m2 (assoc m :a 2)) (def m3 (update m :b inc))

m ;=> {:a 2 :b 3}

Would it be better if these functions work like in Clojure?

— Reply to this email directly, view it on GitHub https://github.com/Olical/aniseed/issues/131, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACM6XNWUV4DOV4AC4LS3H3WOARK7ANCNFSM6AAAAAATDD6CFQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

D00mch commented 1 year ago

That totally makes sense. Did you consider naming these functions with an exclamation mark? The current API may be misleading because of the Clojure associations.

Olical commented 1 year ago

I thought about it and used it in a couple of places but it makes interop with Lua code weird since ! is not a valid character in Lua so you'd end up calling a.assoc_31 or something like that from Lua. And I didn't want to set that precedent really, I did in some places but I didn't want to make EVERY mutating function end in ! because that'd be almost every function in the entire library. I don't even do that in Clojure, let alone Lua :D

On Mon, 19 Dec 2022 at 10:23, Artur Dumchev @.***> wrote:

That totally makes sense. Did you consider naming these functions with an exclamation mark? The current API may be misleading because of the Clojure associations.

— Reply to this email directly, view it on GitHub https://github.com/Olical/aniseed/issues/131#issuecomment-1357422474, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACM6XIQNCEV4WKILNZXJZTWOAZRHANCNFSM6AAAAAATDD6CFQ . You are receiving this because you commented.Message ID: @.***>

D00mch commented 1 year ago

Ok, thank you for the quick response! I will either use luafun (by the way, how do I require the bundled one?) or make a copy of assoc/assoc-in/update in my local utils.

Olical commented 1 year ago

It's here https://github.com/Olical/aniseed/blob/master/lua/aniseed/deps/fun.lua so should be aniseed.deps.fun, I haven't used it yet though, it's just there as a temp thing just in case I needed it but now I'm keeping it there since it might come in handy for people that need a lot more than aniseed.core.

On Mon, 19 Dec 2022 at 10:34, Artur Dumchev @.***> wrote:

Ok, thank you for the quick response! I will either use luafun (by the way, how do I require the bundled one?) or make a copy of assoc/assoc-in/update in my local utils.

— Reply to this email directly, view it on GitHub https://github.com/Olical/aniseed/issues/131#issuecomment-1357435363, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACM6XJKMSPZNJKFJEEAODTWOA2ZLANCNFSM6AAAAAATDD6CFQ . You are receiving this because you commented.Message ID: @.***>