Skip to content

Instantly share code, notes, and snippets.

View johnynek's full-sized avatar

P. Oscar Boykin johnynek

View GitHub Profile
@johnynek
johnynek / flatMapFromTailRec.scala
Created October 7, 2017 01:55
I hadn't realized (maybe the original paper mentioned) but tailRecM + map is enough to do flatMap (and of course logically flatMap + pure are enough to do tailRecM).
object Monad {
trait Monad[F[_]] {
def pure[A](a: A): F[A]
/**
* We can have a default implementation in terms of tailRecM
* and map
*/
def flatMap[A, B](fa: F[A])(fn: A => F[B]): F[B] = {
def step(first: Option[A]): F[Either[Option[A], B]] =
@johnynek
johnynek / nullable.scala
Created September 30, 2017 17:40
zero cost nullable type for scala
object N {
type Nullable[T <: AnyRef]
object Impl {
def apply[T <: AnyRef](t: T): Nullable[T] =
t.asInstanceOf[Nullable[T]]
def empty[T <: AnyRef]: Nullable[T] =
null.asInstanceOf[Nullable[T]]
@johnynek
johnynek / mauistuff.md
Last active July 22, 2021 19:51
Things to do in Maui

People often ask for my recommendations of what to do on Maui. Mostly the answer is get in the water, and use yelp before eating, but here are some more details.

You could skip this list and read this book maui revealed.

Hiking:

@johnynek
johnynek / scala.sh
Created March 23, 2017 20:19
The scala launcher script.
#!/usr/bin/env bash
#
##############################################################################
# Copyright 2002-2013 LAMP/EPFL
#
# This is free software; see the distribution for copying conditions.
# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.
##############################################################################

Keybase proof

I hereby claim:

  • I am johnynek on github.
  • I am posco (https://keybase.io/posco) on keybase.
  • I have a public key ASDrd8YEbN7xCqwZragaOoYgyQkf8RvefRSxGYCpHGmB_Ao

To claim this, I am signing this object:

// attempt at tailRecM for:
// https://gist.github.com/stew/780e8b6072c27514405911e324678d34
override def tailRecM[A, B](a: A)(f: A => G[F[scala.Either[A, B]]]): G[F[B]] = {
lazy val mapFn: F[Either[A, B]] => Eval[F[B]] = right(run)
def feval(a: A): G[Eval[F[B]]] =
G.map(f(a)) { fe => Eval.later(fe).flatMap(mapFn) }
lazy val run: Either[A, B] => G[Eval[F[B]]] = {
@johnynek
johnynek / tailRecMEval.scala
Created November 17, 2016 21:24
build tailrecM using eval and traverse.
object TailRecM {
def traverseTailRec[M[_]: Traverse: Monad, A, B](a: A)(fn: A => M[Either[A, B]]): M[B] = {
def loop(aa: A): Eval[M[B]] =
Eval.later(fn(aa))
.flatMap { meither =>
val mid: M[Eval[M[B]]] = Traverse[M].map(meither) {
case Right(b) => Eval.now(Monad[M].pure(b))
case Left(a) => loop(a)
}
@johnynek
johnynek / tailrecA.scala
Created November 15, 2016 17:23
An idea for an stack safe products with scala.
object TailRecA {
trait Applicative[F[_]] {
def pure[A](a: A): F[A]
def ap[A, B](a: F[A])(fn: F[A => B]): F[B]
def product[A, B](fa: F[A], fb: F[B]): F[(A, B)] =
ap(fa)(map(fb) { b =>
{ a: A => (a, b) }
})
@johnynek
johnynek / set_join.scala
Created November 7, 2016 21:19
Doing basic set operations in scalding
// how do do set operations in scalding:
case class DSet[T](toPipe: TypedPipe[T]) {
// returns the items in that that are in toPipe (not-de-duplicated
def contains(that: TypedPipe[T])(implicit ord: Ordering[T]): TypedPipe[T] =
toPipe.asKeys.sum // reduce all the values to a single value on the set
.join(that.asKeys.size)
.flatMap { case (t, (_, sz)) => Iterator.fill(sz)(t) }
// which items in that are not in toPipe, the union
package cats
package data
import scala.annotation.tailrec
sealed abstract class Continuation[O, +I] {
def map[I2](fn: I => I2): Continuation[O, I2] =
Continuation.Mapped(this, fn)
def flatMap[I2](fn: I => Continuation[O, I2]): Continuation[O, I2] =