46 Matching Annotations
 Mar 2021

trailblazer.to trailblazer.to

endpoint Diagram::Operation::Create do ctx, ** redirect_to diagram_path(ctx[:diagram].id) end.Or do ctx, ** render :form end


trailblazer.to trailblazer.to

The flow pipetree is a mix of the Either monad and “Railwayoriented programming”, but not entirely the same.

 Feb 2021

github.com github.com

It is based on the idea that each validation is encapsulated by a simple, stateless predicate that receives some input and returns either true or false.

It will return the result as a Dry::Monads::Result object which can be either successful or failure.


github.com github.com

Or you can use Maybe container! It consists of Some and Nothing types, representing existing state and empty (instead of None) state respectively.


sobolevn.me sobolevn.me

Let’s start with the same number dividing example, which returns 0 when the error happens. Maybe instead we can indicate that the result was not successful without any explicit numerical value?

Now you can easily spot them! The rule is: if you see a Result it means that this function can throw an exception. And you even know its type in advance.

You can use container values, that wraps actual success or error value into a thin wrapper with utility methods to work with this value. That’s exactly why we have created @drypython/returns project. So you can make your functions return something meaningful, typed, and safe.


www.morozov.is www.morozov.is

This is how the same example would look like using raw monads:

Do notation provides an alternative to bind, which also flattens the code.

bind applies unwrapped Success value to the block, which should return a Result object. Noop on Failure


Railway Oriented Programming comes from functional programming, so it is tightly related to the usual FP concepts like monads, composition, and many others.


en.wikipedia.org en.wikipedia.org

Though rarer in computer science, one can use category theory directly, which defines a monad as a functor with two additional natural transformations. So to begin, a structure requires a higherorder function (or "functional") named map to qualify as a functor:
rare in computer science using category theory directly in computer science What other areas of math can be used / are rare to use directly in computer science?

can transform monadic values m a applying f to the unwrapped value a

In fact, the Product comonad is just the dual of the Writer monad and effectively the same as the Reader monad (both discussed below)

procedure to wrap values of any basic type within the monad (yielding a monadic value)

A combinator, typically called bind (as in binding a variable) and represented with an infix operator >>=, that unwraps a monadic variable, then inserts it into a monadic function/expression, resulting in a new monadic value:(mx >>= f) : (M T, T → M U) → M U

A type constructor M that builds up a monadic type M T

A type converter, often called unit or return, that embeds an object x in the monad:.mwparseroutput .blockindent{paddingleft:3em;paddingright:0;overflow:hidden}unit(x) : T → M T

allows monads to simplify a wide range of problems

another to compose functions that output monadic values (called monadic functions)

Monads achieve this by providing their own data type (a particular type for each type of monad), which represents a specific form of computation

In functional programming, a monad is an abstraction that allows structuring programs generically

Supporting languages may use monads to abstract away boilerplate code needed by the program logic.

Whatever language or default programming paradigm a developer uses, following the monad pattern brings many of the benefits of purely functional programming.

By reifying a specific kind of computation, a monad not only encapsulates the tedious details of that computational pattern, but it does so in a declarative way, improving the code's clarity.

Maybe T can be understood as a "wrapping" type, wrapping the type T into a new type with builtin exception handling

Since monads make semantics explicit for a kind of computation, they can also be used to implement convenient language features.

Research beginning in the late 1980s and early 1990s established that monads could bring seemingly disparate computerscience problems under a unified, functional model.

handling potential undefined values (with the Maybe monad)
Tags
 monad: Maybe
 monad: data type
 simplify
 semantics
 monad: wrapping value (nomadic value)
 dual (math)
 _any_
 monad: unwrapping
 representing a kind of computation (monad)
 functor
 defining in terms of _
 functional programming
 annotation meta: may need new tag
 reifying
 programming paradigm
 monad
 code/program structure
 category theory
 higherorder function
 use case
 monad: monadic functions (composition)
 clarity
 declarative
 error/exception handling
 advantages/merits/pros
 monad: monadic value (wrapping a value within the monad)
 wrapping type
 unification/cohesiveness
 abstractions
 generic programming
 reduce the amount of boilerplate/duplication
 encapsulation
Annotators
URL


dryrb.org dryrb.org

To get the final value you can use value_or which is a safe way to unwrap a nilable value.

In other words, once you've used Maybe you cannot hit nil with a missing method. This is remarkable because even &. doesn't save you from omitting  "No state" at the end of the computation. Basically, that's what they call "Type Safety".


Monads provide an elegant way of handling errors, exceptions and chaining functions so that the code is much more understandable and has all the error handling, without all the ifs and elses.
Tags
 remarkable
 monad: Maybe
 good explanation
 monad
 type safety
 good point
 optional chaining/safe navigation operator
 drymonads (Ruby)
 elegant solution
 nullable type/value
 error/exception handling
 monad: unwrapping
 chaining
 make it impossible to get wrong/incorrect
 ruby library
 understandable
 too many ifs: bad
Annotators
URL


jrsinclair.com jrsinclair.com


In this article, we’ll take a look at using the ‘Either monad’ as an alternative to try...catch.

And a word of warning. If you haven’t come across things like monads before, they might seem really… different. Working with tools like these takes a mind shift. And that can be hard work to start with.


functionalprogramming.medium.com functionalprogramming.medium.com

mmhaskell.com mmhaskell.com

The IO monad wraps computations in the following context: "This computation can read information from or write information to the terminal, file system, operating system, and/or network". If you want to get user input, print a message to the user, read information from a file, or make a network call, you'll need to do so within the IO Monad. These are "side effects". We cannot perform them from "pure" Haskell code.

A Monad wraps a value or a computation with a particular context. A monad must define both a means of wrapping normal values in the context, and a way of combining computations within the context.



wiki.haskell.org wiki.haskell.org


rosettacode.org rosettacode.org
Tags
Annotators
URL


blog.logrocket.com blog.logrocket.com

This style of error handling uses monadic behavior — a substitute way of handling errors.

 Jan 2020