Skip to content

Instantly share code, notes, and snippets.

@gluc
Created August 18, 2016 17:02
Show Gist options
  • Save gluc/bcfcfa0e52ccc1ec8e6eb56c863923f8 to your computer and use it in GitHub Desktop.
Save gluc/bcfcfa0e52ccc1ec8e6eb56c863923f8 to your computer and use it in GitHub Desktop.
library(yaml)
library(data.tree)
yaml <- "
functions:
currbalfun: |
function(node) {
Aggregate(deal, 'currbal', function(bal) sum(bal, na.rm = TRUE))
}
TC_1:
principaldesc: PT
interestdesc: FIX
coupon: 6.50
factor: 1
AF:
cusip: 3136AS4X1
principaldesc: PT
interestdesc: FLT
coupon: 0.95
origbal: 38400349
factor: 1
currbal: 38400349
index: LIBOR1BBA
cap: 6.50
floor: 0.50
multiplier: 1
AS:
cusip: 3136AS4Y9
principaldesc: NTL
interestdesc: INVIO
coupon: 5.55
origbal: 38400349
factor: 1
currbal: 38400349
index: LIBOR1BBA
cap: 6.00
floor: 0
multiplier: -1
TC_2:
principaldec: PT
interestdesc: FIX
coupon: 6.50
AB:
cusip: 3136AS4Z6
principaldesc: PACAD
interestdesc: FIX
coupon: 3.00
origbal: 63959530
factor: 1
currbal: 63959530
index: none
cap: 0
floor: 0
multiplier: 0
AV:
cusip: 3136AS5A0
principaldesc: PACAD
interestdesc: FIX
coupon: 3.0
origbal: 3456470
factor: 1
currbal: 3456470
index: none
cap: 0
floor: 0
multiplier: 0
AZ:
cusip: 3136AS5B8
principaldesc: PACAD
interestdesc: FIX
coupon: 3.0
origbal: 8584874
factor: 1
currbal: 8584874
index: none
cap: 0
floor: 0
multiplier: 0
ZA:
cusip: 3136AS5C6
principaldesc: SUP
interestdesc: ACC
coupon: 3.00
origbal: 20000000
factor: 1
currbal: 20000000
index: none
cap: 0
floor: 0
multiplier: 0
"
lol <- yaml.load(yaml)
deal <- as.Node(lol, nodeName = "Deal")
evaluator <- function(fctnode) {
for (fnct in fctnode$fields) {
f <- eval(parse(text = fctnode[[fnct]]))
# warp the function and call using node
f2 <- function() {
f(fctnode$parent)
}
fctnode$parent[[fnct]] <- f2
}
}
deal$Do(fun = evaluator,
filterFun = function(node) node$name == "functions")
do.call(print, c(deal, deal$fieldsAll))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment