pandastrike / panda-river

Reactive programming in a functional style in JavaScript.
MIT License
1 stars 2 forks source link

hybrid map-tee function #10

Open dyoder opened 9 years ago

dyoder commented 9 years ago

I'm having trouble at the moment figuring out what abstraction this matches, but basically I keep running into situations where I need something like map, but which also keeps the original argument, like tee.

For example, Panda-9000 has a compileJade helper. This takes a path to a Jade file and returns the HTML. The problem is that we lose the original path in the process. The solution is just to return an array that contains both. Simple enough.

However, this comes up frequently enough, and the solution is awkward enough, that it seems like a variation on map is justified. I mean, what if the function was, say, to just read the file. Do I really want to have to write a wrapper around read that takes the path and returns both the path and content?

Instead, I'd rather be able to call mapTee or whatever it is and not have to write a wrapper function. Further, this feels like it's a thing. It's a very general pattern: convert

f(x) -> y

to

f(x) -> [ x, y ]

It seems like there is like a name for this operation. And maybe this isn't a variation on map or tee at all. Maybe it should be a function along the lines of spread that simply modifies a function?

In fact…should that be the case with tee? Should tee just take an arbitrary function? That is much more generally useful and we could still just write:

map tee f

Of course, that doesn't fit what tee does in Unix. But, again, these feel like they might actually both correspond to relatively common functional operations.

The names for those might be too obscure (like variadic versus spread), but on the other hand, I'm having trouble coming up with useful names.

dyoder commented 9 years ago

I just realized, we can handle both of these cases with the variant that returns an array, since you're of course free to just ignore the second argument.