method-combinators icon indicating copy to clipboard operation
method-combinators copied to clipboard

added sed

Open ludicast opened this issue 13 years ago • 0 comments

The sed combinator transforms a parameter at whatever index you input. Useful in situations analogous to Rails route helpers. user_path(user) is sometimes fed a user and sometimes fed a user id. In cases where it is fed a user's id, sed can transform that id into an actual user as the function parameter (or vice-versa). This prevents the function from needing to contain an if or switch statement that explicitly does the transformation.

So it world be like this

findUser = (sed (user)->
  if _.isObject(user)
    user
  else
    @lookupUserById user
)()
class RouteHelpers
  userPath: findUser (user)->

ludicast avatar Nov 20 '12 01:11 ludicast