Understanding map, filter, and fold
dev.gd
dev.gd
Even though I have relatively little front-end experience, and use primarily python/ruby- I only started to use these with Javascript (via the underscore library).
The only thing I didn't quite agree with is " ... the syntax is obvious enough for most people to understand even if they don't quite grok Haskell".
Whilst the early examples were manageable, I was struggling with the syntax later on. The syntax looks clean, so it seems like Haskell is a neat language, but it wasn't enough to make it easy for me to fully 'get it'. Perhaps I'm too locked in a ruby/python mindset.
https://developer.mozilla.org/en-US/docs/JavaScript/Referenc...
https://developer.mozilla.org/en-US/docs/JavaScript/Referenc...
https://developer.mozilla.org/en-US/docs/JavaScript/Referenc...
What's with the space shuttle / F22 hovering beneath the text right in the middle of the screen? :/
I use those methods all the time when I have to do C# code.
I do wonder why these operations lack a standard name. Still, I think map-reduce sounds a lot better than map-inject or Select-Aggregate or map-fold.
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.
[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]
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
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.
Meijer, E., Fokkinga, M., & Paterson, R. (1991). Functional programming with bananas, lenses, envelopes and barbed wire, 124–144.
Hutton, G. (1999). A tutorial on the universality and expressiveness of fold. Journal of Functional Programming, 9(4), 355–372.
Gibbons, J., Hutton, G., & Altenkirch, T. (2001). When is a function a fold or an unfold? Electronic Notes in Theoretical Computer Science, 44(1), 146–160.
(The third one is for theoretical interest mostly)
It's better to think of them as left- and right-associative.
Defining any' as
any' f xs = foldl (\acc x -> f x || acc) False xs
Then trying to evaluate any' even [1..] runs forever, but if we modify it slightly to use foldr instead as
any' f xs = foldr (\x acc -> f x || acc) False xs
Then evaluating any' even [1..] terminates basically immediately with True.
I'll update the post with a correction, and eventually write a "how I learned to stop worrying and love the difference between foldl and foldr" post. :-)
I had previously learned these as map and reduce/inject (thank you, ruby). It's only this past few months when I took an algorithms course on Coursera that I realized how much of a pain it was to do some of this stuff in other langauges (like Java). To me it was just natural to write functions that operated on an array of "stuff", rather than arrays of ints or strings, etc... The course literally had us building identical functions for handling different data types and then finally solving the problem via generics and the use of an ugly typecast.
The nice thing about map, as opposed to fold, is that you don't need to know the result of the ith step to do the i+1th.
What I was trying to say is that map is not universal, in that all function evaluations can be performed in parallel, because you can't use the result of the ith to compute the i+1th. In this respect map is in fact a restriction of fold.
BTW, Hutton's book on Haskell is just so excellent.
var i = 5;
var result = input.select(item => {
var val = item.v + i;
i = val;
return val;
});
This would be an awful programming practice, but it serves as an example that map isn't necessarily parrallelizable.Article implies: (++) :: [a] -> a -> [a]
http://www.haskell.org/ghc/docs/latest/html/libraries/base/D...
The ruby I'm using right now (1.8.7) does not understand "filter", but it does have the equivalent called "select".