These are code examples for the “Functional event sourcing example in Kotlin” article:
All gists:
-
Functional event sourcing example in Kotlin
These are code examples for the “Functional event sourcing example in Kotlin” article:
All gists:
Functional event sourcing example in Kotlin
import arrow.continuations.Effect | |
import arrow.continuations.generic.DelimitedScope | |
import arrow.core.Either | |
import arrow.core.right | |
import com.squareup.sqldelight.TransactionWithReturn | |
import kotlin.coroutines.RestrictsSuspension | |
/** | |
* Either-Syntax for SqlDelight transactions. | |
* |
This is a list of patterns. I'm collecting this list with an idea to write a book similar to the GoF book.
My patterns:
Lately I have been busy reading some new books on Domain Driven Design (DDD) and software architecture -- including a short yet eye-opening one in Python and a great one in F#. At the same time, it seems that more people in the Functional Programming world are looking at more formal approaches to modelling -- some examples here. This has brought some thought from the background of my brain about how we should model, organize, and architect software using the lessons we've learnt from functional programming.
Before moving on, let me be clear about this being just a dump of some thoughts, not always well-defined, definite
import Data.Function | |
import Data.List | |
import Data.Maybe | |
import qualified Data.Set as Set | |
import qualified Data.Map as Map | |
-- Longest increasing subsequence | |
-- O(nlogn) | |
lis :: Ord a => [a] -> [a] | |
lis = buildResult . snd . mapAccumL takeMax (Set.empty, Nothing) |
-- An example multihandler. | |
-- Pipe messages out of the first argument computation into the second. | |
pipe : Request (Send m) () -> Request (Receive m) a ->{Abort} a | |
pipe sender receiver = | |
case receiver of | |
{ Receive.receive -> kr } -> | |
case sender of | |
{ Send.send m -> ks } -> pipe (step ks) (step '(kr m)) | |
{ _ } -> Abort.abort | |
{ a } -> a |
This tutorial explains how Unison handles 'effectful' computations, like storing state or performing I/O, using abilities. It assumes you haven't come across abilities before, and covers everything from the ground up.
This is an unofficial tutorial, written before the one on unisonweb.org/docs. The approach taken here is slow and methodical. Your first stop should be the official tutorial, if you haven't seen it already.
This doc is a Unison transcript - the source is here.
Terminology note: other languages with ability systems typically call them 'effect handlers' or 'algebraic effects', but many of the ideas are the same.
Copyright © 2020, Philippe Bastiani.
These are additional notes to this GIST : For-Comprehension Free Monads in Kotlin - Mini Howto.
Disclaimer: This GIST assumes that you are roughly familiar with the concept of Free monad.
Λrrow 0.10 (now deprecated)
Spoilers for Advent of Code 2019 follow.
Fuel required to launch a given module is based on its mass. Specifically, to find the fuel required for a module, take its mass, divide by three, round down, and subtract 2.
This describes a simple function. There seems to be an oversight in the problem statement that modules with very low mass have a negative fuel requirement. I'm going to assume that's not right, and that instead of integer subtraction, we want natural number subtraction (sometimes called "monus"). In Unison, we can use the Nat
type instead of integers, so we don't have to consider negatives. The subtraction operation is called drop
: