metaprogramming

Mysterious oneliner template code, any one ?

I was reading this page : C++ Tip: How To Get Array Length. The writer presented a piece of code to know the size of static arrays. template<typename T, int size> int GetArrLength(T(&)[size]){return size;} // what does '(&)' mean ? . . . int arr[17]; int arrSize = GetArrLength(arr); // arrSize = 17 Could anyone please shed the light o...

How could metaprogramming be used to reduce redundancy in this Ruby code?

class Device def initialize(device_id, data_resource) @id = device_id @data_resource = data_resource end def display_device mode = @data_resource.get_display_device_mode(@id) presets = @data_resource.get_display_device_presets(@id) summary = "display_device: #{mode} ($#{presets})" return "* #{summary}" if p...

Is Template Metaprogramming faster than the equivalent C code ?

Is Template Metaprogramming faster than the equivalent C code ? ( I'm talking about the runtime performance) :) ...

What's the purpose of singleton_class.send & singleton_class.class_eval in this metaprogramming example?

class Product < ActiveRecord::Base set_table_name 'produce' end module ActiveRecord class Base def self.set_table_name name define_attr_method :table_name, name end def self.define_attr_method(name, value) singleton_class.send :alias_method, "original_#{name}", name singleton_class.class_eval do define_method(name) do value en...

What is the purpose of lambda in this example?

lambda is used in this example in both the compose and the hydrate methods. What does lambda do here? def compose *lambdas if lambdas.empty? lambda { nil } elsif lambdas.size == 1 lambdas.first else lambda do |n| lambdas.first.call(compose(*lambdas[1..-1]).call(n)) end end end def hydrate(modulus, printabl...

What's the idiomatic way to traverse a boost::mpl::list?

Edit: I've edited the sample to better resemble the problem I have, now the function depends on a regular parameter (and not only on template parameters) which means that the computations can't be made at compile time. I wrote some code with a hand written typelist and now we've started using boost and I'm trying to move it to the mp...

Is it possible to run code after each line in Ruby?

I understand that it is possible to decorate methods with before and after hooks in ruby, but is it possible to do it for each line of a given method? For example, I have an automation test and I want to verify that after each step there no error shown on the page. The error is shown as a red div and is not visible to Ruby as raise or a...

C++ Template Metaprogramming - Is it possible to output the generated code?

I would like to debug some templated code to understand it better. Unfortunately I'm new to template metaprogramming and it IS hard for me to get in. When I try to output the preprocessed source files I get 125 000 lines of code :/ So is there a way I can see the generated Code? (The library I'm using is SeqAn) ...

Template Metaprogramming - i still don't get it :(

Hi folks ... i have a problem .. i don't understand Template Metaprogramming. The Problem is : i read a lot . But it does not make much sense to me :/ Fact nr.1 : Template Metaprogramming is faster template <int N> struct Factorial { enum { value = N * Factorial<N - 1>::value }; }; template <> struct Factorial<0> { enum...

How can I add a field dynamically to a Java class using Groovy?

In Ruby I can add instance variables to a class by opening it, and doing something like this : class Whatever def add_x @x = 20 end end and this would add me an instance variable by the name of x. How can I do the same thing in Groovy? ...

How can I intercept execution of all the methods in a Java application using Groovy?

Is it possible to intercept all the methods called in a application? I'd like to do something with them, and then let them execute. I tried to override this behaviour in Object.metaClass.invokeMethod, but it doesn't seem to work. Is this doable? ...

What will I learn from Metaprogramming Ruby?

Will I learn anything in Metaprogramming Ruby that is not covered in any of: Programming Ruby, The Ruby Cookbook, The Ruby Programming Language or The Ruby Way, all of which have at least a chapter's material covering metaprogramming in Ruby? It also has about a third of the book devoted to Rails, which I'm not particularly interested i...

Pythonic Comparison Functions

For the sake of simplicity, let's say I have a Person class in Python. This class has fields for firstname, lastname, and dob. class Person: def __init__(self, firstname, lastname, dob): self.firstname = firstname; self.lastname = lastname; self.dob = dob; In some situations I want to sort lists of Persons by lastname f...

generic non-invasive cache wrapper

I'm trying create a class which adds functionality to a generic class, without directly interfacing with the wrapped class. A good example of this would be a smart pointer. Specifically, I'd like to create a wrapper which caches all the i/o for one (or any?) method invoked through the wrapper. Ideally, the cache wrapper have the follo...

Help Meta-programming in Ruby: Overwriting Activerecord Setters

I have no idea how to "metatize" this method, but I know it should be simple. The method looks like this def check_sent=(value) Date.parse(value) rescue @dates_bad = true self.write_attribute(:check_sent, value) end This is for an ActiveRecord subclass. I would like to have these methods for all fields that I specify....

JetBrains Meta Programming System

Does anyone have any experience with the JetBrains Meta Programming System? Is MPS better than, say, developing a DSL in Ruby? ...

How should I compare a c++ metaprogram with an C code ? (runtime )

Hi there , I have ported a C program to a C++ Template Meta program .Now i want to compare the runtime . Since there is almost no runtime in the C++ program , how should i compare these 2 programs. Can i compare C runtime with C++ compile time ? or is it just not comparable ? ...

How to REDIRECT a web Page for every ten seconds

In simple there is any way to redirect a page for every ten seconds in PHP ...

How to detect the end of a method chain in Ruby

I have a Flickr interface that I wrote a while ago and part of it bothers me and I'd like to make it nicer. The way it works is I use method missing to construct the url parameters for the flickr call from the methods called on the flickr object, eg. @flickr.groups.pools.getPhotos(:user_id => "12656878@N06", :group_id => "99404851@N00")...

ruby class extensions

I would like to write a method in ruby that takes a class with certain methods and modifies its behavior by adding methods or changing how existing methods work. I would like to do this in a way that doesn't modify the base class so basically I want a function that takes a class and returns a new modified class without harming the initia...