Chapter 06: Example Application

The only "new" concept here is the prop function. As far as I can tell, fp-ts does not provide an equivalent function. A more powerful library called monocle-ts is recommended.

I've provided my own implementation below that should work for the context of this book:

const prop = curry((property, object) => object[property]);

This implementation has some limitations. Typescript will not allow you to partially apply this function.

const fooProp = prop('foo');
//! 'string' is not assignable to parameter of type 'never'.

As far as I know, this is a limitation of the type system.

A translation of the example from the book using fp-ts:

const Impure = {
  getJSON: curry((callback, url) => $.getJSON(url, callback)),
  setHtml: curry((sel, html) => $(sel).html(html)),
  trace: curry((tag, x) => { console.log(tag, x); return x; }),
};

const host = 'api.flickr.com';
const path = '/services/feeds/photos_public.gne';
const query = t => `?tags=${t}&format=json&jsoncallback=?`;
const url = t => `https://${host}${path}${query(t)}`;

const mediaUrl = compose(prop('m'), prop('media'));
const mediaUrls = compose(map(mediaUrl), prop('items'));

const img = src => $('<img />', { src });

const images = compose(map(img), mediaUrls);
const render = compose(Impure.setHtml('#js-main'), images);
const app = compose(Impure.getJSON(render), url);

Open in CodeSandbox.io!

Last updated