How do you identify monadic design patterns?

A helpful rule of thumb is when you see values in a context; monads can be seen as layering “effects” on: Maybe: partiality (uses: computations that can fail) Either: short-circuiting errors (uses: error/exception handling) [] (the list monad): nondeterminism (uses: list generation, filtering, …) State: a single mutable reference (uses: state) Reader: a shared environment … Read more

Is Java 8 missing an OptionalBoolean?

This quote explains the considerations behind having primitive streams. I’m assuming the same applied to primitive Optionals. In short, primitive streams (and probably Optionals as well) were created for performance reasons. They didn’t create them for all 8 primitive types to reduce code duplication and interface pollution. Quoting the words of Brian Goetz in the … Read more

Using Eithers with Scala “for” syntax

It doesn’t work in scala 2.11 and earlier because Either is not a monad. Though there’s talk of right-biasing it, you can’t use it in a for-comprehension: you have to get a LeftProject or RightProjection, like below: for { foo <- Right[String,Int](1).right bar <- Left[String,Int](“nope”).right } yield (foo + bar) That returns Left(“nope”), by the … Read more

RxJava – fetch every item on the list

Here’s a small self contained example public class Example { public static class Item { int id; } public static void main(String[] args) { getIds() .flatMapIterable(ids -> ids) // Converts your list of ids into an Observable which emits every item in the list .flatMap(Example::getItemObservable) // Calls the method which returns a new Observable<Item> .subscribe(item … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)