Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

function composition samples


SourceForge.net Logo     CTTL on    
    SourceForge    
    Download    
    Latest    
    Documentation    
    Index    
    Library    
    News    
    CVS    
    Repository    
   Other    
   Links    

higher-order function

composition of functions
#include "cttl/cttl.h"
#include "lambda/lambda.h"

using namespace cttl;

int F( int x_ )
{
    return x_ * 2;
}

int G( int x_ )
{
    return x_ * 3;
}

int main(int argc, char* argv[])
{
    int var = 0;

    (
        scalar( &var )^F^G = 5

    ).evaluate();

    assert( var == 30 );

    return 0;
}

value translator for literal constant

#include "cttl/cttl.h"
#include "lambda/lambda.h"

using namespace cttl;

int main(int argc, char* argv[])
{
    double var = 0.0;
    (
        ++( scalar( &var )^atof^"3.14159" )
    ).evaluate();
    assert( var == 3.14159 );
    return 0;
}

value translator for CTTL substring

#include "cttl/cttl.h"
#include "lambda/lambda.h"

using namespace cttl;

int main(int argc, char* argv[])
{
    input<> inp( "456" );
    const_edge<> universe( new_edge( inp ) );
    const_edge<>& edge_ref = universe;
    int var = 0;
    (
        ++( scalar( &var )^atoi^edge_ref )
    ).evaluate();
    assert( var == 456 );
    return 0;
}

            

value translator to integral type

Value translator implements algorithm that converts input value to integral type by inserting the value into a sequence container, and returning its array index as the result

#define CTTL_TRACE_RULES // automatically turns lambda tracing on

#include "cttl/cttl.h"
#include "lambda/lambda.h"

using namespace cttl;

int main(int argc, char* argv[])
{
    input<> inp( "XYZ" );
    lambda< edge <> >::scalar universe( new_edge( inp ) );
    int var = 0;
    std::vector< std::string > vect;
    (
        ++( scalar( &var )^vect^universe.top() ),
        CTTL_LAMBDA_ASSERT( scalar( &vect )[ 0 ] == scalar( std::string( "XYZ" ) ) ),
        CTTL_LAMBDA_ASSERT( scalar( &var ) == 0 ),
        alias::text( &universe, std::string( "ABC" ) ),
        CTTL_LAMBDA_ASSERT( alias::text( universe ) == scalar( std::string( "ABC" ) ) ),
        ++( scalar( &var )^vect^universe.top() ),
        CTTL_LAMBDA_ASSERT( scalar( &vect )[ 1 ] == scalar( std::string( "ABC" ) ) ),
        CTTL_LAMBDA_ASSERT( scalar( &var ) == 1 )
    ).evaluate();
    assert( var == 1 );
    assert( vect[ 0 ] == "XYZ" );
    assert( vect[ 1 ] == "ABC" );
    return 0;
}

dictionary translator for sequence and associative container

#define CTTL_TRACE_RULES // automatically turns lambda tracing on

#include "cttl/cttl.h"
#include "lambda/lambda.h"

using namespace cttl;

int main(int argc, char* argv[])
{
    input<> inp( "zero one" );
    const_edge< policy_space<> > universe( new_edge( inp ) );

    // User can construct a dictionary using default
    // constructors for sequence and associative container:
    std::pair<
        std::vector< std::string >,
        std::map< std::string, int
        >
    > symbol_table_a;

    // Alternatively, parts of the dictionary can be
    // individually constructed:
    std::vector< std::string > symbol_vector;
    std::map< std::string, int > symbol_map;

    // A pair of pointers represents a dictionary:
    std::pair<
        std::vector< std::string >*,
        std::map< std::string, int >*
    > symbol_table_b( &symbol_vector, &symbol_map );

    size_t result = 
        (
            *(
                entity( isalpha )
                &
                *(
                    ++( scalar( 0 )^symbol_table_a^universe ),
                    ++( scalar( 0 )^symbol_table_b^universe )
                )
            )
        ).match( universe );

    assert( result != std::string::npos );
    assert( symbol_table_a.first[ 0 ] == "zero" );
    assert( symbol_table_a.second.find( "zero" ) != symbol_table_a.second.end() );
    assert( symbol_table_a.second.find( "zero" )->second == 0 );

    assert( symbol_vector[ 1 ] == "one" );
    assert( symbol_map.find( "one" ) != symbol_map.end() );
    assert( symbol_map.find( "one" )->second == 1 );

    return 0;
}

closure translator

struct digit_parser {
    std::vector< std::string > vect_digit_names;
    lambda< char >::scalar sdigit;

    digit_parser()
        : vect_digit_names( 2 )
    {
        vect_digit_names[ 0 ] = "zero";
        vect_digit_names[ 1 ] = "one";
    }

    std::string get_digit_name( char digit_ ) const
    {
        assert( digit_ == 0 || digit_ == 1 );
        return vect_digit_names[ digit_ ];
    }

    template< typename UniverseT >
    size_t grammar( UniverseT& edge_ )
    {
        return (
            *(
                first( isdigit )
                &
                *(
                    // calculate digit that got parsed:
                    sdigit = scalar( edge_.first )[ 0 ] - '0',
                    // replace digit name:
                    ++(
                        scalar( &edge_ )
                        ^
                        CTTL_MEMBER_ACTION(
                            *this,
                            std::mem_fun( &digit_parser::get_digit_name ),
                            sdigit.top()
                        )
                    )
                )
            )
        ).match( edge_ );
    }

};

value translator for std::stack

#include "cttl/cttl.h"
#include "lambda/lambda.h"

using namespace cttl;

int main(int argc, char* argv[])
{
    lambda< double >::stack dstack;
    (
        ++( dstack^atof^"7.77" ),
        CTTL_LAMBDA_ASSERT( +dstack == size_t( 1 ) ),
        CTTL_LAMBDA_ASSERT( *dstack == scalar( 7.77 ) )
    ).evaluate();

    return 0;
}

value translation, data type conversion

#include "cttl/cttl.h"
#include "lambda/lambda.h"
#include "utils/itos.h"

using namespace cttl;

int main(int argc, char* argv[])
{
    lambda< std::string >::scalar str;
    (
        (str^itos) = 123,   // convert integer to string
        CTTL_LAMBDA_ASSERT( str == scalar( std::string( "123" ) ) )
    ).evaluate();

    return 0;
}

closure with explicit template parameters

Sometimes template parameters for std::mem_fun must be provided explicitly. This situation occurs when class has overloaded functions with different return types.

Also note, that alias::offset( ) cannot be used for inode_writer class, because alias::offset( ) expects size_t as a return type.

#define CTTL_TRACE_RULES // turns lambda tracing on

#include "cttl/cttl.h"
#include "lambda/lambda.h"
#include "utils/inode.h"

using namespace cttl;

int main(int argc, char* argv[])
{
    std::vector< int > vec( 1, 0 ); // one elem with zero value
    lambda< inode_writer<> >::scalar iwriter = inode_writer<>( vec );
    (
        // closure equivalent of iwriter = size_t( 0 ):
        *scalar(
            CTTL_MEMBER_ACTION(
                &iwriter.top(),
                ( std::mem_fun< void, inode_writer<>, size_t >( &inode_writer<>::offset ) ),
                size_t( 0 )
            )
        ),
        CTTL_LAMBDA_ASSERT( alias::offset( iwriter ) == size_t( 0 ) )

    ).evaluate();

    return 0;
}



Copyright © 1997-2006 Igor Kholodov mailto:cttl@users.sourceforge.net.

Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.


Generated on Thu Nov 2 17:48:53 2006 for CTTL Lambda Expression by  doxygen 1.3.9.1