Giter VIP home page Giter VIP logo

bwireman / delay Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 0.0 310 KB

A dead simple data-structure for delaying side effects. Written in the excellent gleam language. Available on the Beam & in the browser!

Home Page: https://hexdocs.pm/delay/index.html

License: MIT License

Gleam 56.05% Shell 19.39% TypeScript 11.01% Python 9.26% JavaScript 4.29%
delay elixir erlang gleam javascript monad nodejs sideeffects

delay's Introduction

Delay ⌚

A dead simple data-structure for delaying side effects ⌚! Written in the excellent gleam ✨ language. Supporting both Erlang & Javascript targets

test commits mit PRs Welcome 1.1.0 gleam erlang hex.pm downloads gleam js npm

Basic Usage

import gleam/io
import delay

let d = delay.delay_effect(fn() {
  io.println("Hello")
  Ok(1)
}) |> delay.map(fn(x) {
  io.println("World")
  Ok(x + 1)
})

let res = delay.run(d)
// Hello
// World
// res = Ok(2)

More info

The result of delay_effect is really just a first class function with a nice API wrapper. It isn't executed until put through one of run/1, drain/1, fallthrough/1 or one of the other functions in order to execute a delayed effect. And can be called as many times as you want.

import gleam/io
import delay

let d = delay.delay_effect(fn() {
  io.println("Hello")
  Error("bummer")
}) |> delay.map(fn(x) {
  io.println("World")
  Ok(x + 1)
})

let res = delay.run(d)
// Hello
// res = Error("bummer")

If one of the functions in the chain fails, the rest will short circuit and the error will be returned.

Effects can be retried as well via retry/3

// using the same effect `d` from above

let res = delay.retry(d, 3, 200)
|> delay.run()
// Hello
// Hello
// Hello
// Hello
// res = Error("bummer")

Usage within Javascript 🌸 directly

If you want to use this library from javascript alone, but aren't ready to embrace gleam, you can install it from npm! Docs can be found here

npm i delay-gleam
import { delay_effect, map, run } from "delay-gleam"
import { ok, error, get } from "delay-gleam/extras"

d = delay_effect(() => error(console.log("123")))
d = map(d, (_) => ok(console.log("456")))
get(run(d))
// 123

Extras

Helper functions for using this library directly in javascript can be found here

FAQ

Doesn't the concept of a delayed side effect kind of lose value in the world of actor model concurrency and zero shared memory?!

A little

Then why did you write this?

For fun

Is gleam ✨ actually excellent?

So far

delay's People

Contributors

bwireman avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.