The Promise monad in JavaScript

To define a monad in JavaScript, we have to set two operators: bind and unit (in Haskell these two are >>= and return). Defining fail will be useful too.

// unit :: a -> Promise a
unit = x => new Promise(resolve => resolve(x))
 
// fail :: String -> Promise a
fail = str => new Promise((resolve, reject) => reject(str))
 
// bind :: Promise a -> (a -> Promise b) -> Promise b
bind = (p, np) => p.then(x => np(x), fail)

Let’s add also an operator for joining multiple operations into one:

// pipe :: [a -> Promise a] -> a -> Promise a
pipe = ps => x => ps.reduce(bind, unit(x))

..and some definitions for testing:

// add10 :: Int -> Promise Int
add10 = x => unit(x + 10)
 
// add40 :: Int -> Promise Int
add40 = pipe([add10, add10, add10, add10])
 
// fadd40 :: Int -> Promise Int
fadd40 = pipe([add10, add10, _ => fail("oops"), add10])

And now we can test our Promise monad:

add40(8).then(x => console.log(x))
-------> 48

fadd40(8).then(x => console.log(x), err => console.log("err " + err))
-------> “err oops”

The above code was tested on Firefox.

PrivMX secure web mail

Two years ago, when we were looking at our prototype web application which encrypted messages, we decided that we would try to make it available to the public. Since then, we have had a lot of other work on our clients’ projects, but finally we have made it — PrivMX WebMail is available for download at privmx.com, since October 17th. We invite you to test the app — it is free and can be easily installed on your own web page. Web end-to-end encryption used in PrivMX WebMail allows also to create and use secure web forms.

Linked.PM – encrypted messages as clickable links and QR codes

We’ve published a small web application that encrypts messages and converts them into links and QR codes. After clicking the link and entering password you (or any recipient) can read the text hidden in that link. The program does not send messages to any servers. We invite you to test and look at the source code.

Continue reading “Linked.PM – encrypted messages as clickable links and QR codes”