Minimal Scala monad containing either good values or error messages? -


In Scala, I'm thinking of a simple mood result which contains any Good value, or alternatively a error message is my implementation.

I want to ask: Have I done something in a very complicated manner? Or even mistakes?

Can it be simplified (but not to maintain readability)? For example, do I need to use intangible classes and companionship, or can it be easier to keep everyone in the general category? Result [U] = Results: [U] = This match {good case (X ) = & Gt; F (x) case e: error = & gt; E} DIF map [U] (F: T => U): Results [u] = flatmap {(x: t) => The result (F (x))}} Case class increases [T] (X: T) results [T] Case class error (E: string) increases result [Nothing] Object result {DEF applied [T] (X: t): result [t] = good (x)}

Now if I, for example

  val x = good (5 Def f1 (v: int): result [int] = good (v + 1) def ef (v: int): result [int] = error ("af")  

General methods:

  x flatMap f1 flatMap f1 // = & gt; Good (7) x flatmap FA flatMap f1 // = & gt; Error (Foo)  

To understand more:

 For  (a <- x; b & lt; -f1 (a); C & lt; - F1 (B)) yield c = // = & gt; Scalable  \ /  is familiar with the monad, but it is for simple cases when installation and importing scalas seem a little bit. 

I like the abstract class and I think you can leave the return types to lose any readability for flatmap and map .

I like the companion object because it tells your call what it is for the unit function.


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -