Skip to content

Instantly share code, notes, and snippets.

View aitoroses's full-sized avatar

Aitor Oses aitoroses

View GitHub Profile
@aitoroses
aitoroses / Free.re
Created August 18, 2022 15:01
Free Monads in ReasonML
module type Functor = {
type t('a);
let map: (t('a), 'a => 'b) => t('b);
};
module type Monad = {
type t('a);
let return: 'a => t('a);
let flatMap: (t('a), 'a => t('b)) => t('b);
@aitoroses
aitoroses / di-in-fp.md
Created May 13, 2022 15:14 — forked from gvolpe/di-in-fp.md
Dependency Injection in Functional Programming

Dependency Injection in Functional Programming

There exist several DI frameworks / libraries in the Scala ecosystem. But the more functional code you write the more you'll realize there's no need to use any of them.

A few of the most claimed benefits are the following:

  • Dependency Injection.
  • Life cycle management.
  • Dependency graph rewriting.
@aitoroses
aitoroses / AppM.purs
Created April 12, 2022 08:30
Purescript Example: Application Monad (AppM) with parallelization
type Env
= { hello :: String }
newtype AppM a
= AppM (ReaderT Env Aff a)
runAppM ∷ Env → AppM ~> Aff
runAppM env (AppM m) = runReaderT m env
@aitoroses
aitoroses / background.js
Created April 4, 2022 08:41 — forked from danharper/background.js
Bare minimum Chrome extension to inject a JS file into the given page when you click on the browser action icon. The script then inserts a new div into the DOM.
// this is the background code...
// listen for our browerAction to be clicked
chrome.browserAction.onClicked.addListener(function (tab) {
// for the current tab, inject the "inject.js" file & execute it
chrome.tabs.executeScript(tab.ib, {
file: 'inject.js'
});
});
module Main where
import Prelude
import Control.Monad.Reader (Reader, runReader, ReaderT(..), runReaderT, ask, class MonadAsk)
import Effect (Effect)
import Effect.Class (class MonadEffect, liftEffect)
import Effect.Console (log)
---------------------------------------------------------
@aitoroses
aitoroses / AsyncEx.fs
Created March 14, 2021 20:15 — forked from panesofglass/AsyncEx.fs
Create an Observable from an F# Async.
module AsyncEx
open System
type Async<'a> with
member this.ToObservable() =
{ new IObservable<_> with
member x.Subscribe(o) =
if o = null then nullArg "observer"
let cts = new System.Threading.CancellationTokenSource()
let invoked = ref 0
@aitoroses
aitoroses / ReaderM.fs
Created February 27, 2021 11:56 — forked from CarstenKoenig/ReaderM.fs
Reader monad in F#
namespace ReaderM
type ReaderM<'d,'out> =
'd -> 'out
module Reader =
// basic operations
let run dep (rm : ReaderM<_,_>) =
@aitoroses
aitoroses / GADT.re
Created June 23, 2020 08:46
Using GADT's to parametrize functions
module Arg = {
type arg(_) =
| Arg(argument('a)): arg('a)
and argument('a) =
| Scalar({
name: option(string),
description: option(string),
validator: 'a => bool,
})
and arglist(_, _) =
@aitoroses
aitoroses / Eff.re
Created June 4, 2020 13:42
Tracking Effects in ReasonML
module Eff: {
type t('t, 'a);
let make: (unit => 'a) => t('t, 'a);
let run: t('t, 'a) => 'a;
let map: (t('t, 'a), 'a => 'b) => t('t, 'b);
let join: t('t, t('t, 'a)) => t('t, 'a);
let bind: (t('t, 'a), 'a => t('t, 'b)) => t('t, 'b);
} = {
type t('t, 'a) = unit => 'a;
let make: (unit => 'a) => t('t, 'a) = m => m;
@aitoroses
aitoroses / gist:512d36d4353cf53bc98b386c9a82c722
Created May 7, 2019 19:01 — forked from relrod/gist:dd748c9ee0b111c3bd47
Pure IO in OCaml via the Free monad
(* Purely functional I/O in Ocaml via the Free monad.
* by Ricky Elrod <[email protected]>.
*
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*