December 08, 2019

let%Everything ReasonML

let%Anything is a really cool module for ReasonML,

Which allows you to create a module and use as with let%Module, lets take a look how to use.

Since buckleScript have upgraded to 7++ by default and I am using bs-let for demo:

Create a module and method that you like, with let_.

Here I create a very basic module of MapEmpty with let_, that takes a parameter and returns a callback which will be used as promise.

module MapEmpty = {
  let let_ = (params, cb) => params->Belt.Option.getWithDefault("")->cb;
};

So we can use this module as %Module

 {
  let a = None;

  let%MapEmpty b = a;

  Js.log(b);
};

You can find very cool introduction on EggHead of how you can use with async/await to replace promise in ReasonML.

Although EggHead is using promise from got module, I like to use bs-fetch so that you don't have to write binding. Lets install bs-fetch

yarn add bs-fetch

And remember to add bs-fetch to your bsconfig file

Now we want to create a Fetch module that will call a ajax and return the json data

Module Fetch = {
  let perform = url =>
    Js.Promise.(
      Fetch.fetch(url)
      |> then_(Fetch.Response.json)
      |> then_(json => json |> resolve)
    );
}

and create the async method

Module Async = {
  let let_ = (prop, cb) => Js.Promise.then_(cb, prop);
}

so now you can use it like the way you want like Javascript async/await

{
  let%Async res = Promise.perform("http://www.your-json.com/xxx.json");

  Js.log(res); // here you will received the json
  Js.Promise.resolve();
};

Top 6 Most Recommended Developer Books

The Pragmatic Programmer

by Dave Thomas, Andy Hunt

Check out this book on Amazon)}

The Pragmatic Programmer is one of those rare tech audiobooks you'll listen, re-listen, and listen to again over the years. Whether you're new to the field or an experienced practitioner, you'll come away with fresh insights each and every time. Dave Thomas and Andy Hunt wrote the first edition of this influential book in 1999 to help their clients create better software and rediscover the joy of coding. These lessons have helped a generation of programmers examine the very essence of software development, independent of any particular language, framework, or methodology, and the Pragmatic philosophy has spawned hundreds of books, screencasts, and audio books, as well as thousands of careers and success stories. Now, 20 years later, this new edition re-examines what it means to be a modern programmer. Topics range from personal responsibility and career development to architectural techniques for keeping your code flexible and easy to adapt and reuse.

Published: 2019

Genre: Programming

Cover of The Pragmatic Programmer

The Pragmatic Programmer

Cover of Clean Code: A Handbook of Agile Software Craftsmanship

Clean Code: A Handbook of Agile Software Craftsmanship

Cover of Working Effectively with Legacy Code

Working Effectively with Legacy Code

Cover of Introduction to Algorithms

Introduction to Algorithms

Cover of Eloquent JavaScript

Eloquent JavaScript

Cover of The Road to React

The Road to React