all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Philip Kaludercic <philipk@posteo.net>
To: Joost Kremers <joostkremers@fastmail.fm>
Cc: emacs-devel <emacs-devel@gnu.org>
Subject: Re: [ELPA] New package: csv2ledger
Date: Mon, 20 May 2024 12:26:56 +0000	[thread overview]
Message-ID: <87zfsklldb.fsf@posteo.net> (raw)
In-Reply-To: <874jasn0c7.fsf@posteo.net> (Philip Kaludercic's message of "Mon,  20 May 2024 12:18:16 +0000")


[...]

>> I can remove dash if you prefer, though. There are two dash
>> functions I use that
>> don't seem to have a direct built-in equivalent (as far as I can tell):
>> `-compose` and `-zip-pair`. I'm sure I can deal with them.
>
> -Compose seems like it is easy to replace, but it might be an idea to
> add something to the core as well.  One should try to improve the output
> of the byte-code though:
>
> (disassemble (byte-compile (lambda (x) (funcall (-compose 'foo 'bar 'baz) x))))
> byte code:
>   doc:   ...
>   args: (arg1)
> 0	constant  <byte-code-function>
>       doc:   ...
>       args: (&rest rest)
>     0	    constant  apply
>     1	    constant  baz
>     2	    stack-ref 2
>     3	    call      2
>     4	    constant  (bar foo)
>     5	    constant  0
>     6	    constant  nil
>     7	    dup	      
>     8:1	    stack-ref 3
>     9	    goto-if-nil 2
>     12	    stack-ref 3
>     13	    dup	      
>     14	    cdr	      
>     15	    stack-set 5
>     17	    discardN-preserve-tos 2
>     19	    car-safe  
>     20	    stack-ref 1
>     21	    dup	      
>     22	    add1      
>     23	    stack-set 3
>     25	    stack-ref 1
>     26	    stack-ref 5
>     27	    call      1
>     28	    stack-set 5
>     30	    goto      1
>     33:2    discardN  4
>     35	    return    
>
> 1	stack-ref 1
> 2	call	  1
> 3	return	  
>
> compared to the direct:
>
> (disassemble (byte-compile (lambda (x) (foo (bar (baz x))))))
> byte code:
>   doc:   ...
>   args: (arg1)
> 0	constant  foo
> 1	constant  bar
> 2	constant  baz
> 3	stack-ref 3
> 4	call	  1
> 5	call	  1
> 6	call	  1
> 7	return	  

Correction, the first disassembly should be

(disassemble (byte-compile (-compose 'foo 'bar 'baz)))

byte code:
  doc:   ...
  args: (&rest rest)
0	constant  apply
1	constant  baz
2	stack-ref 2
3	call	  2
4	constant  (bar foo)
5	constant  0
6	constant  nil
7	dup	  
8:1	stack-ref 3
9	goto-if-nil 2
12	stack-ref 3
13	dup	  
14	cdr	  
15	stack-set 5
17	discardN-preserve-tos 2
19	car-safe  
20	stack-ref 1
21	dup	  
22	add1	  
23	stack-set 3
25	stack-ref 1
26	stack-ref 5
27	call	  1
28	stack-set 5
30	goto	  1
33:2	discardN  4
35	return	  

avoiding the unnecessary lambda.

It appears even a naive approach to implement the function oneself gives
better results:

(defun compose (&rest fns)
  (lambda (arg)
    (dotimes (fn (nreverse fns))
      (setq arg (funcall fn arg)))
    arg))

(disassemble (byte-compile (compose 'foo 'bar 'baz)))

byte code:
  doc:   ...
  args: (arg1)
0	constant  (foo bar baz)
1	nreverse  
2	constant  0
3:1	dup	  
4	stack-ref 2
5	lss	  
6	goto-if-nil 2
9	dup	  
10	stack-ref 3
11	call	  1
12	stack-set 3
14	add1	  
15	goto	  1
18:2	discardN  2
20	return	  

[...]



  reply	other threads:[~2024-05-20 12:26 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-17 14:24 [ELPA] New package: csv2ledger Joost Kremers
2024-05-20  7:02 ` Philip Kaludercic
2024-05-20 12:06   ` Joost Kremers
2024-05-20 12:18     ` Philip Kaludercic
2024-05-20 12:26       ` Philip Kaludercic [this message]
2024-05-20 14:27       ` Joost Kremers
2024-05-29  8:01         ` Joost Kremers
2024-06-02 19:49           ` Philip Kaludercic
2024-06-02 21:14             ` Joost Kremers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87zfsklldb.fsf@posteo.net \
    --to=philipk@posteo.net \
    --cc=emacs-devel@gnu.org \
    --cc=joostkremers@fastmail.fm \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.