Re: Make ly:make-unpure-pure-container accept a single callback (issue 7382046)

2013-02-21 Thread dak

Reviewers: MikeSol,

Message:
On 2013/02/21 19:35:35, MikeSol wrote:

LGTM, with one suggestion: the difference between unpure and pure

functions in

LilyPond is that unpure functions accept n arguments whereas pure

functions

accept n+2. This shortcut you're proposing only works when n=1, which

you

document, but it'd be nice if it worked for an arbitrary number of

input

arguments.  It'd only be possible to do this, though, if there were a

way in C++

to determine the number of arguments a Scheme function took.


Nonsense.  That's what the rest argument is for.  I'll submit an amended
version.

Description:
Make ly:make-unpure-pure-container accept a single callback

Like with fixed values, this gets duplicated for the pure value as
well, but converted into a three-argument callback.

Please review this at https://codereview.appspot.com/7382046/

Affected files:
  M lily/unpure-pure-container.cc


Index: lily/unpure-pure-container.cc
diff --git a/lily/unpure-pure-container.cc b/lily/unpure-pure-container.cc
index  
097cb9d80c46fc1f2baea71b6de1424a284265d9..2a762b7876ece461f164827bd75dd51c1ecdd883  
100644

--- a/lily/unpure-pure-container.cc
+++ b/lily/unpure-pure-container.cc
@@ -22,6 +22,9 @@
 #include "grob.hh"

 static scm_t_bits unpure_pure_container_tag;
+static scm_t_bits unpure_pure_call_tag;
+// Used for rerouting a function of (grob start end) to one of
+// (grob)

 bool
 is_unpure_pure_container (SCM s)
@@ -33,14 +36,21 @@ SCM
 unpure_pure_container_unpure_part (SCM smob)
 {
   LY_ASSERT_TYPE (is_unpure_pure_container, smob, 1);
-  return (SCM) SCM_CELL_WORD_1 (smob);
+  return SCM_SMOB_OBJECT (smob);
 }

 SCM
 unpure_pure_container_pure_part (SCM smob)
 {
   LY_ASSERT_TYPE (is_unpure_pure_container, smob, 1);
-  return (SCM) SCM_CELL_WORD_2 (smob);
+  SCM res = SCM_SMOB_OBJECT_2 (smob);
+
+  if (!SCM_UNBNDP (res))
+return res;
+
+  SCM_NEWSMOB (res, unpure_pure_call_tag,
+   SCM_UNPACK (unpure_pure_container_unpure_part (smob)));
+  return res;
 }

 LY_DEFINE (ly_unpure_pure_container_p, "ly:unpure-pure-container?",
@@ -54,11 +64,13 @@ LY_DEFINE  
(ly_make_unpure_pure_container, "ly:make-unpure-pure-container",

1, 1, 0, (SCM unpure, SCM pure),
"Make an unpure-pure container.  @var{unpure} should be an  
unpure"
" expression, and @var{pure} should be a pure expression.  If  
@var{pure}"

-   " is ommitted, the value of @var{unpure} will be used twice.")
+   " is omitted, the value of @var{unpure} will be used twice,"
+   " except that a callback with one argument is converted into"
+   " one with three arguments for the sake of pure calculations.")
 {
   SCM z;

-  if (pure == SCM_UNDEFINED)
+  if (SCM_UNBNDP (pure) && !ly_is_procedure (unpure))
 pure = unpure;

   SCM_NEWSMOB2 (z, unpure_pure_container_tag, SCM_UNPACK (unpure),  
SCM_UNPACK (pure));

@@ -100,11 +112,22 @@ pure_mark (SCM pure)
   return pure;
 }

+SCM
+apply_unpure_pure (SCM clo, SCM grob, SCM /* start */, SCM /* end */)
+{
+  return scm_call_1 (SCM_SMOB_OBJECT (clo), grob);
+}
+
+
 void init_unpure_pure_container ()
 {
   unpure_pure_container_tag = scm_make_smob_type ("unpure-pure-container",  
0);

   scm_set_smob_mark (unpure_pure_container_tag, pure_mark);
   scm_set_smob_print (unpure_pure_container_tag,  
print_unpure_pure_container);

+  unpure_pure_call_tag = scm_make_smob_type ("unpure-pure-call", 0);
+  scm_set_smob_mark (unpure_pure_call_tag, scm_markcdr);
+  scm_set_smob_apply (unpure_pure_call_tag,
+  (SCM (*)()) apply_unpure_pure, 3, 0, 0);
 };

 ADD_SCM_INIT_FUNC (unpure_pure_container, init_unpure_pure_container);



___
lilypond-devel mailing list
lilypond-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/lilypond-devel


Make ly:make-unpure-pure-container accept a single callback (issue 7382046)

2013-02-21 Thread mtsolo

LGTM, with one suggestion: the difference between unpure and pure
functions in LilyPond is that unpure functions accept n arguments
whereas pure functions accept n+2. This shortcut you're proposing only
works when n=1, which you document, but it'd be nice if it worked for an
arbitrary number of input arguments.  It'd only be possible to do this,
though, if there were a way in C++ to determine the number of arguments
a Scheme function took.

https://codereview.appspot.com/7382046/

___
lilypond-devel mailing list
lilypond-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/lilypond-devel