Skip to content

Instantly share code, notes, and snippets.

View semmel's full-sized avatar

Matthias Seemann semmel

View GitHub Profile
@semmel
semmel / StateMachineImplWithSumTypes.md
Created November 18, 2023 19:23
State Machine with Sum/Union Type Libraries for JavaScript
Lib Creation TypeCheck cata Serialisable
[periodo,paldepind/union-type][union-type-gh] Type - [^1]
[JAForbes/sum-type][sum-type-gh]
[fantasyland/daggy][daggy-gh] tagged, taggedSum ✓ [^2]

FSM

action :: State → Args → State

@semmel
semmel / TypeTraversals.md
Last active September 4, 2023 14:49
Rearrange (Applicative or Functor) Types with Ramda

An Applicative type (e.g. a Maybe) is also a Functor with an ap method. Thus, for the observations in the table it can be used everywhere as an example.

Purpose Function Signature Example
swap types inside out sequence(TypeRep f) t (f a) → f (t a)[^1] sequence(M)([Just(1)]) // -> Just [1]
apply effect + wrap inside out traverse(TypeRep f) (a → f b) → t a → f (t b)[^1] traverse(M)(reciprocal, [2]) // -> Just [0.5]
swap data portion type inside out lens(identity) Functor f ⇒ s → f s yLens(identity)({x: 1, y: M.Just(2)}) // -> Just {x:1,y:2}
apply effect to data portion + wrap inside out lens Functor f ⇒ (a → f a) → s a → f (s a) yLens(reciprocal)({x: 1,y: 2}) // -> Just {x:1, y: 0.5}
@semmel
semmel / RamdaAPIByPurpose.md
Last active August 29, 2023 13:29
Ramda API for purpose

Operations[^1] on Collections with Ramda

Purpose List Object Signature(s)
update/insert individual update, assoc*, append, prepend assoc* Idx → a → {k: v} → {k: v}
insert range/collection concat merge* [a] → [a] → [a];{k: v} → {k: v} → {k: v}
retrieve by key/index nth, path, prop prop, path Idx → {a} → a
retrieve many by key/index props
@semmel
semmel / Useful Combinators in Ramda.md
Last active June 18, 2024 17:32
Function Combinators with Ramda

Function Combinators with Ramda

Original Idea comes from [Avaq/combinators.js][avaq].

Name # [Haskell][] [Ramda][] [Crocks][] Functional Signature Functor m ⇒ Function f, g, h Evaluation
identity I id identity identity a → a
constant K const always constant a → b → a
eager application¹ A ($) call
@semmel
semmel / NonInterleavingAsyncSocketWrite.hpp
Last active July 6, 2020 20:23
Synchronize asynchronous writes to a boost.asio socket.
/* Just my current implementation of Sam Miller's answer on Stack Overflow
* @see https://stackoverflow.com/questions/7754695/boost-asio-async-write-how-to-not-interleaving-async-write-calls
*/
/*
* File: NonInterleavingAsyncSocketWrite.hpp
* Author: Matthias Seemann <seemann@visisoft.de>
* @see https://stackoverflow.com/questions/7754695/boost-asio-async-write-how-to-not-interleaving-async-write-calls
*
* When using boost::asio::async_write:
@semmel
semmel / lamda2stdFunc.cpp
Last active January 14, 2017 20:33
Convert a Lambda to std::function e.g. for currying
#include <iostream>
#include <functional>
#include <memory>
#include <iterator>
#include <algorithm>
using namespace std;
template<typename T>
struct memfun_type
@semmel
semmel / hello-unique_ptr.cpp
Last active November 21, 2016 20:57
Gist created by fiddle.jyt.io
// Jyt is a REPL for C++
// You can write code interactively
// Highlight some code and press alt-enter to execute it
// For example:
auto x = "hello world";
// Now you can query the value in the terminal on the right
// e.g. "x"