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

What's the difference between `use` and `#before_compile`?
How to define functions based on attribute to elixir?
Remove a const qualifier from a variable in D
Meta-modeling crud FROM/JOIN/WHERE statement fragments for higher-level analysis
Elixir: eval'ing code in module context using Code.eval_quoted/3
What is Declarative Meta-Programming?
Reconsolidate protocols in Elixir 1.2 or higher
Inserting an AST into a quote do end block without unquoting it
Transform quoted elixir code to a code string
Pattern matching benchmarking : Compiletime lookup vs Runtime lookup in D
Meta construction capabilities?
How to manipulate the value of variables based on an array of symbols in Julia?
Multiple #:build macros
Julia automatically generate functions and export them
Dealing with end keyword in macros for array indices
Implementation of AnyMap and runtime overhead of `struct Port(u32);`

Categories

HOME
oracle12c
developer-tools
dns
pascal
jelastic
shiro
database-normalization
scripting
tail
regular-language
missing-data
jtextpane
pkcs#11
joomla3.0
mel
angular-dart
onedrive-api
eip
android-intent
azure-powershell
windows-xp
ksh
zxing
draft-js-plugins
opentk
fragment-backstack
nstimer
firebase-analytics
tpm
comparable
panoramas
record
left-join
deltaspike
quickfixj
hibernate-validator
papaparse
contacts
installshield-le
sql-delete
apiary.io
raft
zoomify
metawidget
dcmtk
sharp-snmp
http-request
double-click
tinymce-3
zenhub
edit
gpg-signature
pybrain
google-photos
juice-ui
cvxpy
taocp
angular2-testing
reporting-services-2012
installshield-2010
gogs
eigenvector
uiactionsheet
java-money
extjs-grid
liquid-layout
phpldapadmin
bcache
procedural-programming
device-tree
tinyxml
authlogic
xmltodict
scrypt
epiceditor
android-icons
nokia-s40
jquery-blockui
httponly
google-maps-mobile
expression-blend-4
mbeans
scrollbars
qtabbar
skype4java
wysiwym
java1.4

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