lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] master 4c4a0e4 4/8: Add a PETE length-of function


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 4c4a0e4 4/8: Add a PETE length-of function
Date: Sat, 20 Mar 2021 09:55:30 -0400 (EDT)

branch: master
commit 4c4a0e44470f0ef393308313208dc353e5e73e66
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>

    Add a PETE length-of function
    
    Alternatively, lmi::ssize() could be specialized for this, but that
    would be freakish.
---
 et_vector_test.cpp             | 4 ++++
 tools/pete-2.1.1/et_vector.hpp | 8 ++++++++
 2 files changed, 12 insertions(+)

diff --git a/et_vector_test.cpp b/et_vector_test.cpp
index 57bfd18..4e67020 100644
--- a/et_vector_test.cpp
+++ b/et_vector_test.cpp
@@ -40,6 +40,10 @@ int test_main(int, char*[])
     std::vector<double> v1 = {2.0, 3.0, 4.0};
     LMI_TEST_EQUAL(3, forEach(v0, LengthLeaf(), MaxCombine()));
     LMI_TEST_EQUAL(3, forEach(v0 / v1 + v0 * v1, LengthLeaf(), MaxCombine()));
+    // Rho(std::vector<T> const&) could be supported, of course, but
+    // it seems better to restrict Rho() to 'Expression' instances.
+//  LMI_TEST_EQUAL(3, Rho(v0));
+    LMI_TEST_EQUAL(3, Rho(v0 / v1 + v0 * v1));
     }
 
     // Test non-conformable assignment.
diff --git a/tools/pete-2.1.1/et_vector.hpp b/tools/pete-2.1.1/et_vector.hpp
index 75123c5..3bb6f05 100644
--- a/tools/pete-2.1.1/et_vector.hpp
+++ b/tools/pete-2.1.1/et_vector.hpp
@@ -167,6 +167,14 @@ struct Combine2<int, int, Op, MaxCombine>
     }
 };
 
+/// Like APL's monadic 'rho': return argument's length.
+
+template<typename T>
+inline int Rho(Expression<T> const& t)
+{
+    return forEach(t, LengthLeaf(), MaxCombine());
+}
+
 /// All PETE assignment operators call evaluate().
 
 template<typename T, typename Op, typename U>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]