AndreaSomePostfix · 2022-03-28 21:46
lebensterben · 2022-03-28 21:52
dash looks cool but it can be slow
7890yuiop · 2022-03-28 23:32
> Instead you can assign function symbols with fset!
Or `defalias`
> The last thing I need to discover is how to attach some doc-string to that function...
With `defalias`
> Something for another day!
Or the same day! (Or another day. Time zones and all...)
dpoggio · 2022-03-29 02:49
Should people use `defalias` a bit more and the world would be better.
7890yuiop · 2022-03-29 03:20
Everyone is using it constantly via the `defun` macro :)
takutekato · 2022-03-29 08:10
Can you elaborate more about this?
Is it the overhead of stuffs like `-filter` over `cl-remove-if-not`/`seq-filter`.
Or the `defalias f -compose` style above will result in slower compiled code over `defun`?
arthurno1 · 2022-03-29 10:10
> Indeed, setq assigns values to variable symbols. These symbols cannot be treated as functions.
>Instead you can assign function symbols with fset!
*setq* assigns rather a symbol's value as a variable. There are no really "variable symbols" or "function symbols", there are just symbols that can act av values, functions, or both at the same time. As the documentation says, symbols are just named objects that can store some data (name, value, function object and other properties in symbol's property list).
Elisp is a so called [lisp-2](https://en.wikipedia.org/wiki/Common_Lisp#The_function_namespace), which means in practice that you can have a variable and function with the same name. It is implemented by having a slot for a variable value and a slot for a function in a symbol, and some other slots. *fset* sets the function slot of a symbol, while *set* sets the variable value slot of a symbol. *setq* macro came probably because someone was lazty to type (set 'symbolname some-value), and thought it was easier to type (setq symbolname some-value).
I don't know if it was just an imprecise wording, "variable symbols", or you are just unfamiliar with how symbols work in Emacs lisp. I hope you don't mind me clarifying that.
> The last thing I need to discover is how to attach some doc-string to that function...
(put 'symbol-name 'function-documentation "Func doc string")
Example:
(defun myfunc ())
(documentation 'myfunc)
> nil
(put 'myfunc 'function-documentation "Func that does nothing.")
(documentation 'myfunc)
> "Func that does nothing."
Check documentation for [symbols](https://www.gnu.org/software/emacs/manual/html_node/elisp/Symbols.html), [symbol property list](https://www.gnu.org/software/emacs/manual/html_node/elisp/Symbol-Properties.html) and [standard properties](https://www.gnu.org/software/emacs/manual/html_node/elisp/Standard-Properties.html). Doc for s[ymbol-type](https://www.gnu.org/software/emacs/manual/html_node/elisp/Symbol-Type.html) is relevant too.
> (defun extract-text-from-yt-video (transcript-plist)
> "Make a transcript string out of youtube TRANSCRIPT-PLIST."
> (--> transcript-plist
> (--map (plist-get it :text) it)
> (s-join "\n" it)))
You are hiding the argument in a property list, instead of stating it explicitly. Anyone using that function, and probably even you in 6 months to a year or so, will be scratching head and wondering what is going on and where that value comes from, instead of simply seeing the parameter and knowing what's happening.
Also, the hard-coded:
> (defun text-to-file (text)
> (write-region text nil "/tmp/yt-transcript.txt"))
What happens if you wish to use another text file, maybe in another location, or have two instances running at the same time, i.e. you are getting transcriptions from two different videos at the same time? I understand you are not going to use it that way, but if it was a bit more useful application, maybe it would not be a stretch to have two calls of a function running simultaneously.
But to not be just negative to your idea, someone will probably come in and say I don't like it just because I don't like it :), I think you could exploit the fact that Emacs Lisp is a lisp-2, and do something similar to what minor modes do. Just as an idea, you could create a small code gen, probably a macro, that creates a uniquely named variable to manipulate your text file (or use the symbol's property list as you already do), and an equally named function, that uses that variable value to write to the file, or get the url, or whatever that function has to do. It means you would create a symbol per resource and that resource will have an associated getter/setter named as the resource itself. It is not very efficient in terms of memory, but you could also clean those symbols when you are done with the computation. With other words, make a closure. Would be similar to let-over-lambda style. I am not sure if it would always be "point-free", but it would be more functional than hard-coded data.
arthurno1 · 2022-03-29 10:46
Function calls are not free. This is not something unique to dash. Mapping functions in Emacs are also somewhat slower than looping primitives. Typically, what you are doing with mapping functions is calling a function in a loop, which in emacs lisp generates many function calls, unlike a loop where you work directly with data. That is of course dependent on use-case and data at hand. If you are interested you can read more at:
https://nullprogram.com/blog/2017/01/30/
https://www.wilfred.me.uk/blog/2017/07/29/optimising-dash-el/
Now, those things also change, so take everything of this with a grain of salt, and most importantly, benchmark your code first.
Native compiler also helps, I am not sure, but I think it can inline stuff more, but I am not really so familiar with the details. Maybe someone else here knows more about how much optimization is done in regard to mapping?
AndreaSomePostfix · 2022-03-29 11:26
amazing thanks! I learned something new thank you!
AndreaSomePostfix · 2022-03-29 11:29
Thanks a lot! I use these things all the time but my mental model was wrong XD
Your explanation is amazing: I will have to make an Org Roam note out of it and space learn it :) Thanks a lot!
arthurno1 · 2022-03-29 11:30
Thank you, nice if it helps!
AndreaSomePostfix · 2022-03-29 11:34
Also I like and I agree with your comments about the code: I left it hard coded because I don't plan to touch that code again. The ideas is to store a transcript and load it on my ereader for a quick scan. The aim was just to share I found out how to write a fun in point-free style XD
I think somebody already started playing around with that snippet ([http://angg.twu.net/2020-list-packages-eev-nav.html#f8](http://angg.twu.net/2020-list-packages-eev-nav.html#f8)) and hopefully they will make it better if they need to.