metaprogramming


Dealing with end keyword in macros for array indices


Suppose I have an array of a composite type as follows:
type myType
a::Int
b::Float
end
myArray=myType[]
For obvious reasons, I would like to be able to use simple indexing to access the fields of composite types as follows:
aVals=myArray[1:3].a
The following macro can successfully accomplish this type of indexing, as long as I have a numeric iterable for the array:
macro getArray(exp)
iter=eval(exp.args[1].args[2])
exp.args[1].args[2]=:i;
:[$(esc(exp)) for $(esc(:i)) in $iter]
end
How can I write a similar macro that is also capable of dealing with array indices with the end keyword, i.e.:
aVals=#getArray myArray[1:end].a
The following macro solves not only the indexing problem but also sets the correct output type:
macro getArray(exp)
quote
ftype=typeof($(esc(exp.args[1]))[1].($(esc(exp.args[2]))));
ftype[item.($(exp.args[2])) for item in $(esc(exp.args[1]))]
end
end

Related Links

Can I add numbers with the C/C++ preprocessor?
Meta Programming, whats it good for?
Using metamorphic code to reduce boilerplate
Typed metaprogramming languages [closed]
What to read as a good intro and quickstart to aspect-oriented programming and metaprogramming?
Examples of what D’s templates can be used for
Using Polymorphic Code for Legitimate Purposes?
A guide to Boo's metaprogramming and extensibility features?
JetBrains Meta Programming System
What does word “meta” mean? [closed]
Purdue Compiler Construction Tool Set (PCCTS) : website, tutorial, book?
Are there machine (and human) readable specification languages for algorithm collections and meta-libraries?
Can you nest C preprocessor directives?
Infix format for Nemerle macro
Graphically Modeling Metaprogramming
What exactly is metaprogramming?

Categories

HOME
shell
sonarqube
kentico
facebook-oauth
jpa
spark-streaming
mstest
package
vert.x
barcode
extjs4.2
openstreetmap
gson
alamofire
rtf
spring-social
libc
symbol
bitnami
http-post
coin3d
event-sourcing
atlassian-stash
dimensional-modeling
vuejs
asp.net-mvc-viewmodel
identity-management
sse
topology
windows-xp
googlebot
ember-cli
inno-download-plugin
draft-js-plugins
windows-server-2003
cppreference
user-defined-functions
corenlp-server
database-comparison
spring-security4
wacom
vue-chartjs
joomla2.5
reactjs.net
html-encode
batching
video-capture
xajax
docfx
dm-script
cd
apache-stanbol
datacachefactory
logback-groovy
angular-http
signal-strength
mesosphere
ovf
xbmc
convex-optimization
pair-programming
qtranslate
opengl-4
headless
ngmaterial
flac
cmp
javafx-css
polyline
meteor-packages
custom-lists
resourcemanager
tooleap
dimension
eigenvector
concept-insights
xenomai
microsoft-reporting
vacuum
node-serialport
osc
xmltype
java-security
cosine-similarity
primitive-types
rickshaw
linkedin-jsapi
universal-image-loader
trdion2011
ss7
cuba
mintty
authlogic
decoupling
high-resolution
jeromq
kaleidoscope
jquery-1.9
font-awesome-4.0.0
mantle
chart-director
dopostback
veracity
listitem
garbage
google-maps-mobile
tournament
w3c-geolocation
quotation-marks
codebase
easyb
binomial-heap
isapi-extension
sqlobject

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App