[Opa] Match magic?

Adam Koprowski Adam.Koprowski at mlstate.com
Sun Jan 22 15:05:04 UTC 2012


  Tristan,

  I'm afraid I don't have any magic to show, but the good thing about
higher-order programming is that you can capture this kind of patterns and
write some scaffolding to handle it in a neat way. I'm sure there are many
possible ways to do that, I quickly come up with the following, which has
the advantage of removing the nesting of matches (main reason why your code
is a bit hard to read, as far as I'm concerned). The <&> function below
essentially composes functions in case of success and propagates errors.
Hope that helps,
  Best,
  Adam

function `<&>`(v, f) {
  match (v) {
    case {success: value}: f(value)
    case {failure: err}: {failure: err}
  }
}

function process(v) {
  match (v) {
    case {success: value}: value
    case {failure: err}: err
  }
}

function connect(data) {
  redirect_page = <></>
  response =
    match (WebUtils.web_response_of_code(200)) {
      case {some: code}: {success: code}
      default: {failure: Resource.default_redirection_page("/signup")}
    }
  function try_get_token(code) {
    match (get_token_raw(data, redirect)) {
      case {~token}: {success: ~{token, code}}
      default: {failure: Resource.redirection_page("Failed to Connect",
redirect_page, code, 0, "/signup")}
    }
  }
  function try_get_name(~{token, code}) {
    match (get_name(token)) {
      case {some: name}: {success: ~{name, code}}
      default: {failure: Resource.redirection_page("Failed to Connect",
redirect_page, code, 0, "/signup")}
    }
  }
  function try_check_user(~{name, code}) {
    useref = User_data.mk_ref(name);
    user = User_data.get(useref);
    match (user) {
      case {some: u}:
        if (u.is_oauth) {
          UserContext.change(function(_) {
            { logged : User_data.mk_ref(name) }
          }, state)
        }
      default:
        create(name, "", true)
        UserContext.change(function(_) {
          { logged :User_data.mk_ref(name) }
          }, state)
    }
    {success: Resource.redirection_page("Successfully Connected",
redirect_page, code, 0, "/todos")}
  }
  process(response <&> try_get_token <&> try_get_name <&> try_check_user)
}


On Fri, Jan 20, 2012 at 21:58, Tristan Sloughter <
tristan.sloughter at gmail.com> wrote:

> There has to be a way to not this function be so hideous, right? I know I
> could break it out into a function for each step to make it a little bit
> nicer but I feel like there has to bea  way to say call this 1 default for
> all matches or something like that...
>
> Ideas?
>
>     function connect(data) {
>         redirect_page = <></>
>         match (WebUtils.web_response_of_code(200)) {
>             case {some : code}:
>             match (FBA.get_token_raw(data, redirect)) {
>                 case {~token}:
>                 match (get_name(token)) {
>                     case {some: name}:
>                     useref = User_data.mk_ref(name);
>                     user = User_data.get(useref);
>                     match (user) {
>                         case { some : u }:
>                             if (u.is_oauth) {
>                               UserContext.change(function(_) {
>                                   { logged : User_data.mk_ref(name) }
>                                 }, state)
>                              }
>                         default:
>                             create(name, "", true)
>                             UserContext.change(function(_) {
>                                 { logged :User_data.mk_ref(name) }
>                                }, state)
>                         }
>                         Resource.redirection_page("Successfully
> Connected", redirect_page, code, 0, "/todos")
>                       default:
>                         Resource.redirection_page("Failed to Connect",
> redirect_page, code, 0, "/signup")
>                   }
>                   default:
>                     Resource.redirection_page("Failed to Connect",
> redirect_page, code, 0, "/signup")
>                }
>           default:
>               Resource.default_redirection_page("/signup")
>         }
>     }
>
>
> _______________________________________________
> Opa mailing list
> Opa at lists.owasp.org
> https://lists.owasp.org/mailman/listinfo/opa
>
>


-- 
*Adam Koprowski                [http://adam-koprowski.net]
Opa Tech Evangelist @ MLstate [http://opalang.org | http://mlstate.com]*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.owasp.org/pipermail/opa/attachments/20120122/a4b5ba02/attachment.html>


More information about the Opa mailing list