The only unfortunate thing is in order to support methodMissing it has to be done on instance methods so the words closure hides that fact
classWordMath{Map<String,Integer>NAMES_TO_VALUE=["one":1,"two":2,"three":3,"four":4,"five":5,"six":6,"seven":7,"eight":8,"nine":9,"ten":10,"eleven":11,"twelve":12,"thirteen":13,"fourteen":14,"fifteen":15,"sixteen":16,"seventeen":17,"eighteen":18,"nineteen":19,"twenty":20,"thirty":30,"fourty":40,"fifty":50,"sixty":60,"seventy":70,"eighty":80,"ninety":90,"hundred":100,"thousand":1_000,"million":1_000_000,"billion":1_000_000_000]staticIntegerwords(Closurec){c.delegate=newWordMath()c()}defplus(Integerv){return{v2->v2+v}}defminus(Integerv){return{v2->v2-v}}deftimes(Integerv){return{v2->v2*v}}defdividedBy(Integerv){return{v2->v2/v}}defmethodMissing(Stringname,args){Integervalue=parseValue(name)if(args.size()>0){args[0](value)}else{returnvalue}}IntegerparseValue(Stringname){Integervalue=0Integerbuffer=0name.replaceAll(/([A-Z])/,' $1').toLowerCase().split(" ").each{Stringnumber->Integerv=NAMES_TO_VALUE[number]if(v){if(v<100){buffer+=v}else{buffer*=vif(v>999){value+=bufferbuffer=0}}}else{println("Unknown number ${number}!")}}returnvalue+buffer}}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
A groovy version that will do it out to billions. For example:
The only unfortunate thing is in order to support methodMissing it has to be done on instance methods so the
words
closure hides that fact