omarsebres

Mainly Programming blog for experience sharing.

Stream#headOption

Posted by OmarAbdelrahman on August 11, 2018

A stream is basically a lazy evaluated list, it only evaluates the element when it’s asked.

Given a stream of elements of type A and some helper functions.
Implement how to get Some of the first(head) element if it exists or None if it doesn’t exist.

Implement headOption first to have an idea how you might implement headOptionWithFold

The basic idea is we want to evaluate the first element only and avoid evaluating the rest of the list.
Screen Shot 2018-08-11 at 18.16.01

Leave a comment