Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Is there a good open-source example of this paradigm? I'm interested by the idea but struggling a bit to see how it would actually be implemented for something like, say, a regular REST/JSON API.


I can give you a link to a project I'm working on: https://github.com/stamp-protocol/core/tree/master/src

It's a cryptographic identity protocol. It's architected such that the identity is not a document, but rather a DAG of transactions that build the document (more here: https://stamp-protocol.github.io/).

This core library uses the "functional" api aspect I'm talking about. It lets you create and run DAG transactions, but doesn't save them or generate keys for you or anything like that. When you create a transaction, you have to pass in all the data required to complete it, and when it completes, it returns back a full transaction set (or an error).

There are some utility functions in there that are not entirely functional, but the gist of it is that 95% of the things you do have no side effects and have to be dealt with outside of the library itself.

As far as a REST API, you're dealing with a large number of inputs/outputs (side effects) so this paradigm might be limited. However, you can still think in terms of side effects (HTTP request, HTTP response, database, third-party HTTP calls) and compartmentalize your logic from those operations into functional units: if I `POST /users?name=andrew&likes=[dogs,programming]` then instead of going

    routes/user::save()
      -> models/user::save(params)
        -> models/db::run_query(qry, params)
you might do something like

    routes/user::save()
      -> models/user::create_user_instance(params)
      -> models/user::create_user_query(user{andrew, [dogs, programming])
      -> models/db::run_query(qry, params)
Here, you flatten out your call stack, functionalize your calls (create_user_instance and create_user_query will ALWAYS return the same values given the same params) and move your final database call up the stack (as high as you can afford to). Obviously calling the DB in a route is dumb, but hopefully you get the general idea.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: