Fork me on GitHub
#emacs
<
2017-11-28
>
vemv15:11:20

dumb q, how do I mutate (in-place) a list value? set/setq don't seem apt for the job as one cannot (set (list 2) (list 3))

a1315:11:07

(progn
  (defvar mylist '(2))
  (setq mylist '(3))
  (print mylist))

a1315:11:13

works for me

a1315:11:31

btw, what are you going to mutate by (set (list 2) (list 3)) ?

a1315:11:21

how about doing (set 2 3) ?

vemv15:11:30

I know I know

vemv15:11:54

being in Clojurians Slack I know the benefits of immutability etc 🙂

vemv15:11:42

but elisp lists are mutable right? your recipe isn't what I'm looking for unfortunately

a1315:11:03

list SYMBOLS are mutable

a1315:11:09

list is a list

a1315:11:34

as I said before, you can try (set 2 3) with the same success

a1315:11:07

binding a value to another value doesn't make sense at all

a1315:11:12

btw, what are you trying to do?

vemv15:11:06

I think it can make sense? consider a javascript array you can mutate its length from 10 to 0 and back right?

a1315:11:26

I don't know JS 🙂

vemv15:11:35

and references to that array will see the latest mutated value, accordingly

a1315:11:51

>you can mutate its length from 10 to 0 and back right? you can emulate this in elisp by cadr-ing 🙂, but your HAVE to have a symbol which you are trying to bind to a new value (with a reduced len)

a1315:11:53

And I still can't understand, how (set (list 2) (list 3)) should work?

a1315:11:47

ok, if you don't like to create new variables, you can use something like

a1315:11:09

(defun myset (a b)
  (setq a b))

a1315:11:04

you can do this in elisp too

vemv15:11:04

ok, so I'm asking if there's a readily available function that provides a behavior equivalent to "mutate this whole list setting it to some-other-list"

vemv15:11:11

by means of push/pop, or otherwise

a1315:11:02

(defvar a '(1 2 3))

(defun mutate-it (coll)
  (setq coll (cdr coll))
  (setq coll (cdr coll))
  (setq coll (cdr coll))
  (push "foo" coll)
  (push "bar" coll)
  (push "baz" coll))

(mutate-it a)

a1315:11:25

the literate translation

vemv15:11:14

doesn't work

vemv15:11:30

you cannot use setq

a1315:11:48

(setq a (mutate-it a))

vemv15:11:05

it's not the JS behavior

vemv15:11:26

see last line of my screenshot, I just a

a1315:11:41

JS has bad behaviour, lol

a1315:11:48

also, you can use defmacro and do anything you want to

dpsutton16:11:15

you have to push onto a variable

dpsutton16:11:24

(push list-i-want-to-mutate 4)

dpsutton16:11:44

otherwise just use cons (cons 3 (list 2))

vemv16:11:26

thanks for the pointer

vemv16:11:30

got it finally!

vemv17:11:28

^ that works, but in certain usages I need to (-clone ...) (dash.el library) things first. couldn't figure out exactly why

vemv17:11:37

which is ironical, yep

vemv17:11:00

(defun vemv/bounded-list/insert-at-head! (x bounded-list bound)
  (mutate-to bounded-list (cons x (-clone bounded-list)))
  (mutate-to bounded-list (-take bound (-clone bounded-list)))
  bounded-list)

a1318:11:38

javascript

a1318:11:55

thank you