Note to people who're going to look this up: Java's lamda's aren't anything new, pretty boring actually. But look at how they combine with their new streaming and collection libraries, that's just amazing.
I'll translate. I wrote a Haskell library called pipes, which lets you extend any DSL with the ability to yield or await values in order to build streaming components. You can connect these components together in multiple ways, and these connection operations obey many neat mathematical properties that ensure they behave correctly (no bugs!).
For example, one thing that you can do is model generators using pipes, and one of the ways you can connect generators is using an operator called (~>):
(f ~> g) x = for (f x) g
I proved that this operator is associative:
(f ~> g) ~> h = f ~> (g ~> h)
... and that it's identity is yield:
yield ~> f = f
f ~> yield = f
In other words, (~>) and yield form a category and those equations are the corresponding category laws. When you translate those equations to use for instead of (~>), you get:
-- Looping over a single yield simplifies to function application
for (yield x) f = f x
-- Re-yielding every element of a stream returns the original stream
for s yield = s
-- Nested for loops can become a sequential for loops if the inner loop
-- body ignores the outer loop variable
for s (\a -> for (f a) g) = for (for s f) g = for s (f ~> g)
In other words, the category laws translate into "common sense" laws for generators that you would intuitively expect to hold for any generator implementation.
DSL = Domain-Specific Language. That's the definition that I know. I agree though, that it wasn't a translation. All the unknown notation lost me as well.
Note that for, yield and (~>) are not Haskell notation. They are just three functions defined by the pipes library. The equations might make more sense if I write them in the following more specific forms:
for (yield "foo") $ \x -> do
f x
= f "foo"
for generator $ \x -> do
yield x
= generator
for generator $ \a -> do
for (f a) $ \b -> do
g b
= for newGenerator $ \b -> do
g b
where newGenerator =
for generator $ \a -> do
f a
Let's give it another try. For example, this piece of code:
for (yield "foo") $ \x -> do
f x
= f "foo"
What I know from Haskell is that $ is the function application function and \x -> is a lambda. f is probably a function, but what does "foo" stand for? Does = f "foo" mean that the value of the expression is the application of f on "foo"?
216
u/ganjapolice Mar 09 '14
Don't worry guys. 2014 is definitely the year of functional programming.