T
trans. (T. Onoma)
Here's a coding challenge you.
The R language has the following nifty features for working with arrays. To
what degree can you get Ruby to do the same?
The solution that is closest/nicest gets forever embedded in my growing
library of Ruby enhancements, and it's author's name wrapped in shiny
asterisks and number signs! Joy, joy!
Listing 1. Elementwise operations in R
Or you can operate selectively only on elements with certain indices, by using
an "index array":
Listing 2. Using index arrays to select elements
Or, maybe best of all, you can use a syntax much akin to list comprehensions
in Haskell or Python, and only operate on elements that have a desired
property:
Listing 3. Using predicates to select elements
---
Note, I've gotten pretty far on my own solution, but can't seem to get passed
a certain point --the Functor class I presented a week or so ago has proven
useful.
Finally special thanks to Martin DeMello who got me hell bent on this
T.
The R language has the following nifty features for working with arrays. To
what degree can you get Ruby to do the same?
The solution that is closest/nicest gets forever embedded in my growing
library of Ruby enhancements, and it's author's name wrapped in shiny
asterisks and number signs! Joy, joy!
Listing 1. Elementwise operations in R
[1] 6 7 8 9 10 11 12 13 14 15a = 1:10 # Range of numbers 1 through 10
b = a+5 # Add 5 to each element
b # Show b vector
Or you can operate selectively only on elements with certain indices, by using
an "index array":
Listing 2. Using index arrays to select elements
[1] 60 70 80 9 100 11 120 13 14 15c = b # Make a (lazy) copy of b
pos = c(1,2,3,5,7) # Change the prime indices
c[pos] = c[pos]*10 # Reassign to indexed positions
c # Show c vector
Or, maybe best of all, you can use a syntax much akin to list comprehensions
in Haskell or Python, and only operate on elements that have a desired
property:
Listing 3. Using predicates to select elements
[1] -1 -1 -1 9 -1 11 -1 13 -1 15d = c
d[d %% 2 == 0] = -1 # Reassign -1 to all even elements
d
---
Note, I've gotten pretty far on my own solution, but can't seem to get passed
a certain point --the Functor class I presented a week or so ago has proven
useful.
Finally special thanks to Martin DeMello who got me hell bent on this
T.