3 ms·
Going from java to ruby these functions were the most obvious improvement for my day to day coding.
by ollysb 14y ago
Going from java to ruby these functions were the most obvious improvement for my day to day coding.
- mrgriscom 14y agoI agree. These functions have completely changed how I approach algorithms and data manipulation. I was super-excited to learn Go, but it was a showstopper to discover that Go's lack of support for generics effectively made this pattern of programming impossible. Major disappointment.
- 0x0 14y agoGenuinely curious, what do generics have to do with this?
- gridaphobe 14y agoThe type of map is `(a -> b) -> [a] -> [b]`, which means that map takes a function from any type `a` to any type `b` and a list of `a`s, and returns a list of `b`s. Generics, aka parametric polymorphism, are required for this because the definition of map makes no assumptions on what types `a` and `b` will be instantiated to.
- DougWebb 14y agoWhat does "from any type 'a' to any type 'b'" mean? That sounds like a range of types, but I don't see how that makes sense. Did you just mean that the map converts from 'a' to 'b'?
- FreeFull 14y agoa and b are placeholders (type variables). They can be any type, for example Integer, or a Float. It doesn't matter. [a] is a list of a, [b] is a list of b. Map takes a function that takes an a and returns a b, and applies it to every element of [a] in order to produce the [b]. An example usage is map lessThanThree [1,2,3,4] In this case, map takes a function where a is an integer, and b is a boolean, then takes a list of integers and produces a list of booleans. The result is [True,True,False,False]
- jjaredsimpson 14y agoIt's also illuminating to note that haskell curries function application. This allows us to think of map as a function of one argument. map :: (a -> b) -> ([a] -> [b]) so instead of thinking that map takes a function and a list and applies that function to the list, we can think that map takes a function and "lifts" that functions argument and result types to the list type. This thinking leads us directly to Functors and fmap
- GlennS 14y agoI recently started a Java job after having avoided it in favour of better languages for some time. I've found the Google Guava library makes Java programming a lot more bearable: https://code.google.com/p/guava-libraries/ https://code.google.com/p/guava-libraries/ Thinks that will make your life easier * Collections2.filter() -> this is just filter() * Collections2.transform() -> this is map() * ImmutableMap.of() -> for when you need to create a quick lookup You still have to make an anonymous inner class every time you want to pass a function as an argument though.
- ollysb 14y agoYeah, I actually wrote some similar functions for java when I first started experimenting with ruby on the side. I agree it's a big improvement on for loops but there's still a lot of line noise; the anonymous class(as you mention) and having to passing the collection into a static method which is never great. Importing Collections2 statically does make it a bit more pleasant though.